Skip to content

Commit 11ced11

Browse files
committed
initialize cache before adding/removing to array
1 parent 3cb221c commit 11ced11

File tree

7 files changed

+115
-40
lines changed

7 files changed

+115
-40
lines changed

resources/lib/UnityGroup.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,17 @@ public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true):
184184
// // now we delete the ldap entry
185185
// $this->entry->ensureExists();
186186
// $this->entry->delete();
187-
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->gid);
187+
// $default_value_getter = [$this->LDAP, "getSortedGroupsForRedis"];
188+
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->gid, $default_value_getter);
188189
// foreach ($users as $user) {
189-
// $this->REDIS->removeCacheArray($user->uid, "groups", $this->gid);
190+
// $this->REDIS->removeCacheArray(
191+
// $user->uid,
192+
// "groups",
193+
// $this->gid,
194+
// fn() => $this->getGroupMemberUIDs(true),
195+
// );
190196
// }
197+
// // FIXME group not removed from user's groups array
191198

192199
// // send email to every user of the now deleted PI group
193200
// if ($send_mail) {
@@ -376,7 +383,8 @@ private function init(): void
376383
$this->entry->setAttribute("gidnumber", strval($nextGID));
377384
$this->entry->setAttribute("memberuid", [$owner->uid]);
378385
$this->entry->write();
379-
$this->REDIS->appendCacheArray("sorted_groups", "", $this->gid);
386+
$default_value_getter = [$this->LDAP, "getSortedGroupsForRedis"];
387+
$this->REDIS->appendCacheArray("sorted_groups", "", $this->gid, $default_value_getter);
380388
// TODO if we ever make this project based,
381389
// we need to update the cache here with the memberuid
382390
}
@@ -385,16 +393,36 @@ private function addUserToGroup(UnityUser $new_user): void
385393
{
386394
$this->entry->appendAttribute("memberuid", $new_user->uid);
387395
$this->entry->write();
388-
$this->REDIS->appendCacheArray($this->gid, "members", $new_user->uid);
389-
$this->REDIS->appendCacheArray($new_user->uid, "groups", $this->gid);
396+
$this->REDIS->appendCacheArray(
397+
$this->gid,
398+
"members",
399+
$new_user->uid,
400+
fn() => $this->getGroupMemberUIDs(true),
401+
);
402+
$this->REDIS->appendCacheArray(
403+
$new_user->uid,
404+
"groups",
405+
$this->gid,
406+
fn() => $this->LDAP->getPIGroupGIDsWithMemberUID($new_user->uid),
407+
);
390408
}
391409

392410
private function removeUserFromGroup(UnityUser $old_user): void
393411
{
394412
$this->entry->removeAttributeEntryByValue("memberuid", $old_user->uid);
395413
$this->entry->write();
396-
$this->REDIS->removeCacheArray($this->gid, "members", $old_user->uid);
397-
$this->REDIS->removeCacheArray($old_user->uid, "groups", $this->gid);
414+
$this->REDIS->removeCacheArray(
415+
$this->gid,
416+
"members",
417+
$old_user->uid,
418+
fn() => $this->getGroupMemberUIDs(true),
419+
);
420+
$this->REDIS->removeCacheArray(
421+
$old_user->uid,
422+
"groups",
423+
$this->gid,
424+
fn() => $this->LDAP->getPIGroupGIDsWithMemberUID($old_user->uid),
425+
);
398426
}
399427

400428
public function memberExists(UnityUser $user): bool

resources/lib/UnityLDAP.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,27 @@ public function getUidFromEmail(string $email): LDAPEntry
441441
}
442442
throw new exceptions\EntryNotFoundException($email);
443443
}
444+
445+
public function getSortedQualifiedUsersForRedis(): array
446+
{
447+
$qualified_users = $this->getQualifiedUsersUIDs();
448+
sort($qualified_users);
449+
return $qualified_users;
450+
}
451+
452+
public function getSortedOrgsForRedis(): array
453+
{
454+
$attributes = $this->getAllOrgGroupsAttributes(["gid"]);
455+
$groups = array_map(fn($x) => $x["gid"][0], $attributes);
456+
sort($groups);
457+
return $groups;
458+
}
459+
460+
public function getSortedGroupsForRedis(): array
461+
{
462+
$attributes = $this->getAllPIGroupsAttributes(["gid"]);
463+
$groups = array_map(fn($x) => $x["gid"][0], $attributes);
464+
sort($groups);
465+
return $groups;
466+
}
444467
}

resources/lib/UnityOrg.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public function init(): void
3939
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
4040
$this->entry->setAttribute("gidnumber", strval($nextGID));
4141
$this->entry->write();
42-
$this->REDIS->appendCacheArray("sorted_orgs", "", $this->gid);
42+
$default_value_getter = [$this->LDAP, "getSortedOrgsForRedis"];
43+
$this->REDIS->appendCacheArray("sorted_orgs", "", $this->gid, $default_value_getter);
4344
}
4445

4546
public function exists(): bool
@@ -94,13 +95,23 @@ public function addUser(UnityUser $user): void
9495
{
9596
$this->entry->appendAttribute("memberuid", $user->uid);
9697
$this->entry->write();
97-
$this->REDIS->appendCacheArray($this->gid, "members", $user->uid);
98+
$this->REDIS->appendCacheArray(
99+
$this->gid,
100+
"members",
101+
$user->uid,
102+
fn() => $this->getOrgMemberUIDs(true),
103+
);
98104
}
99105

100106
public function removeUser(UnityUser $user): void
101107
{
102108
$this->entry->removeAttributeEntryByValue("memberuid", $user->uid);
103109
$this->entry->write();
104-
$this->REDIS->removeCacheArray($this->gid, "members", $user->uid);
110+
$this->REDIS->removeCacheArray(
111+
$this->gid,
112+
"members",
113+
$user->uid,
114+
fn() => $this->getOrgMemberUIDs(true),
115+
);
105116
}
106117
}

resources/lib/UnityRedis.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,19 @@ public function getCache(string $object, string $key): mixed
6161
return null;
6262
}
6363

64-
public function appendCacheArray(string $object, string $key, mixed $value): void
65-
{
64+
public function appendCacheArray(
65+
string $object,
66+
string $key,
67+
mixed $value,
68+
callable $default_value_getter,
69+
): void {
6670
if (!$this->enabled) {
6771
return;
6872
}
6973

7074
$cached_val = $this->getCache($object, $key);
7175
if (is_null($cached_val)) {
72-
$this->setCache($object, $key, [$value]);
76+
$this->setCache($object, $key, $default_value_getter());
7377
} else {
7478
if (!is_array($cached_val)) {
7579
throw new Exception("This cache value is not an array");
@@ -82,15 +86,19 @@ public function appendCacheArray(string $object, string $key, mixed $value): voi
8286
}
8387

8488
// TODO return void
85-
public function removeCacheArray(string $object, string $key, mixed $value)
86-
{
89+
public function removeCacheArray(
90+
string $object,
91+
string $key,
92+
mixed $value,
93+
callable $default_value_getter,
94+
) {
8795
if (!$this->enabled) {
8896
return null;
8997
}
9098

9199
$cached_val = $this->getCache($object, $key);
92100
if (is_null($cached_val)) {
93-
$this->setCache($object, $key, []);
101+
$this->setCache($object, $key, $default_value_getter());
94102
} else {
95103
if (!is_array($cached_val)) {
96104
throw new Exception("This cache value is not an array");

resources/lib/UnityUser.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ public function setIsQualified(bool $newIsQualified, bool $doSendMail = true): v
122122
if ($newIsQualified) {
123123
$this->LDAP->getQualifiedUserGroup()->appendAttribute("memberuid", $this->uid);
124124
$this->LDAP->getQualifiedUserGroup()->write();
125-
$this->REDIS->appendCacheArray("sorted_users", "", $this->uid);
125+
$default_value_getter = [$this->LDAP, "getSortedQualifiedUsersForRedis"];
126+
$this->REDIS->appendCacheArray(
127+
"sorted_qualified_users",
128+
"",
129+
$this->uid,
130+
$default_value_getter,
131+
);
126132
if ($doSendMail) {
127133
$this->MAILER->sendMail($this->getMail(), "user_qualified", [
128134
"user" => $this->uid,
@@ -134,7 +140,13 @@ public function setIsQualified(bool $newIsQualified, bool $doSendMail = true): v
134140
->getQualifiedUserGroup()
135141
->removeAttributeEntryByValue("memberuid", $this->uid);
136142
$this->LDAP->getQualifiedUserGroup()->write();
137-
$this->REDIS->removeCacheArray("sorted_users", "", $this->uid);
143+
$default_value_getter = [$this->LDAP, "getSortedQualifiedUsersForRedis"];
144+
$this->REDIS->removeCacheArray(
145+
"sorted_qualified_users",
146+
"",
147+
$this->uid,
148+
$default_value_getter,
149+
);
138150
if ($doSendMail) {
139151
$this->MAILER->sendMail($this->getMail(), "user_dequalified", [
140152
"user" => $this->uid,

test/phpunit-bootstrap.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ function ensureUserDoesNotExist()
193193
$qualified_users_group->write();
194194
ensure(!in_array($USER->uid, $qualified_users_group->getAttribute("memberuid")));
195195
}
196-
$REDIS->removeCacheArray("sorted_qualified_users", "", $USER->uid);
196+
$default_value_getter = [$LDAP, "getSortedQualifiedUsersForRedis"];
197+
$REDIS->removeCacheArray("sorted_qualified_users", "", $USER->uid, $default_value_getter);
197198
}
198199

199200
function ensureOrgGroupDoesNotExist()
@@ -204,7 +205,8 @@ function ensureOrgGroupDoesNotExist()
204205
$org_group->delete();
205206
ensure(!$org_group->exists());
206207
}
207-
$REDIS->removeCacheArray("sorted_orgs", "", $SSO["org"]);
208+
$default_value_getter = [$LDAP, "getSortedOrgsForRedis"];
209+
$REDIS->removeCacheArray("sorted_orgs", "", $SSO["org"], $default_value_getter);
208210
}
209211

210212
function ensureUserNotInPIGroup(UnityGroup $pi_group)
@@ -214,7 +216,12 @@ function ensureUserNotInPIGroup(UnityGroup $pi_group)
214216
$pi_group->removeUser($USER);
215217
ensure(!$pi_group->memberExists($USER));
216218
}
217-
$REDIS->removeCacheArray($pi_group->gid, "members", $USER->uid);
219+
$REDIS->removeCacheArray(
220+
$pi_group->gid,
221+
"members",
222+
$USER->uid,
223+
fn() => $pi_group->getGroupMemberUIDs(true),
224+
);
218225
}
219226

220227
function ensurePIGroupDoesNotExist()
@@ -225,7 +232,8 @@ function ensurePIGroupDoesNotExist()
225232
$LDAP->getPIGroupEntry($gid)->delete();
226233
ensure(!$USER->getPIGroup()->exists());
227234
}
228-
$REDIS->removeCacheArray("sorted_groups", "", $gid);
235+
$default_value_getter = [$LDAP, "getSortedGroupsForRedis"];
236+
$REDIS->removeCacheArray("sorted_groups", "", $gid, $default_value_getter);
229237
}
230238

231239
function getNormalUser()

workers/update-ldap-cache.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
echo "waiting for LDAP search (users)...\n";
3939
$users = $LDAP->search("objectClass=posixAccount", CONFIG["ldap"]["basedn"], []);
4040
echo "response received.\n";
41-
$user_CNs = $LDAP->getQualifiedUserGroup()->getAttribute("memberuid");
42-
sort($user_CNs);
43-
$REDIS->setCache("sorted_qualified_users", "", $user_CNs);
41+
$REDIS->setCache("sorted_qualified_users", "", $LDAP->getSortedQualifiedUsersForRedis());
4442
foreach ($users as $user) {
4543
$uid = $user->getAttribute("cn")[0];
4644
if (!in_array($uid, $user_CNs)) {
@@ -59,13 +57,7 @@
5957
echo "waiting for LDAP search (org groups)...\n";
6058
$org_groups = $org_group_ou->getChildrenArray(["cn", "memberuid"], true);
6159
echo "response received.\n";
62-
// phpcs:disable
63-
$org_group_CNs = array_map(function ($x) {
64-
return $x["cn"][0];
65-
}, $org_groups);
66-
// phpcs:enable
67-
sort($org_group_CNs);
68-
$REDIS->setCache("sorted_orgs", "", $org_group_CNs);
60+
$REDIS->setCache("sorted_orgs", "", $LDAP->getSortedOrgsForRedis());
6961
foreach ($org_groups as $org_group) {
7062
$gid = $org_group["cn"][0];
7163
$REDIS->setCache($gid, "members", $org_group["memberuid"] ?? []);
@@ -75,14 +67,7 @@
7567
echo "waiting for LDAP search (pi groups)...\n";
7668
$pi_groups = $pi_group_ou->getChildrenArray(["cn", "memberuid"], true);
7769
echo "response received.\n";
78-
// phpcs:disable
79-
$pi_group_CNs = array_map(function ($x) {
80-
return $x["cn"][0];
81-
}, $pi_groups);
82-
// phpcs:enable
83-
sort($pi_group_CNs);
84-
// FIXME should be sorted_pi_groups
85-
$REDIS->setCache("sorted_groups", "", $pi_group_CNs);
70+
$REDIS->setCache("sorted_groups", "", $LDAP->getSortedGroupsForRedis());
8671

8772
$user_pi_group_member_of = [];
8873
foreach ($user_CNs as $uid) {

0 commit comments

Comments
 (0)