Skip to content

Commit 23d7753

Browse files
committed
add user to LDAP immediately
1 parent 50912ac commit 23d7753

25 files changed

+680
-786
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Notable users:
6363
- `[email protected]` - admin, PI
6464
- `[email protected]` - not admin, not PI
6565
- `[email protected]` - does not yet have an account
66+
- `[email protected]` - regsitered but not qualified (not a PI or in a PI group)
6667

6768
### Changes to Dev Environment
6869

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ rm "$prod" && ln -s "$old" "$prod"
117117
### 1.3 -> 1.4
118118

119119
- the `[ldap]user_group` option has been renamed to `[ldap]qualified_user_group`
120+
- the `user_created ` mail template has been renamed to `user_qualified`
121+
- the `user_dequalified` mail template has been added
120122

121123
### 1.2 -> 1.3
122124

resources/lib/UnityGroup.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function approveGroup(?UnityUser $operator = null, bool $send_mail = true
100100
if ($this->exists()) {
101101
return;
102102
}
103-
\ensure(!$this->getOwner()->exists());
103+
\ensure($this->getOwner()->exists());
104104
$this->init();
105105
$this->SQL->removeRequest($this->getOwner()->uid);
106106
$operator = is_null($operator) ? $this->getOwner()->uid : $operator->uid;
@@ -113,6 +113,7 @@ public function approveGroup(?UnityUser $operator = null, bool $send_mail = true
113113
if ($send_mail) {
114114
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_created");
115115
}
116+
$this->getOwner()->setIsQualified(true); // having your own group makes you qualified
116117
}
117118

118119
/**
@@ -183,10 +184,17 @@ public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true):
183184
// // now we delete the ldap entry
184185
// $this->entry->ensureExists();
185186
// $this->entry->delete();
186-
// $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);
187189
// foreach ($users as $user) {
188-
// $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+
// );
189196
// }
197+
// // FIXME group not removed from user's groups array
190198

191199
// // send email to every user of the now deleted PI group
192200
// if ($send_mail) {
@@ -207,7 +215,7 @@ public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true):
207215
public function approveUser(UnityUser $new_user, bool $send_mail = true): void
208216
{
209217
$request = $this->SQL->getRequest($new_user->uid, $this->gid);
210-
\ensure(!$new_user->exists());
218+
\ensure($new_user->exists());
211219
$this->addUserToGroup($new_user);
212220
$this->SQL->removeRequest($new_user->uid, $this->gid);
213221
if ($send_mail) {
@@ -222,6 +230,7 @@ public function approveUser(UnityUser $new_user, bool $send_mail = true): void
222230
"org" => $new_user->getOrg(),
223231
]);
224232
}
233+
$new_user->setIsQualified(true); // being in a group makes you qualified
225234
}
226235

227236
public function denyUser(UnityUser $new_user, bool $send_mail = true): void
@@ -374,7 +383,8 @@ private function init(): void
374383
$this->entry->setAttribute("gidnumber", strval($nextGID));
375384
$this->entry->setAttribute("memberuid", [$owner->uid]);
376385
$this->entry->write();
377-
$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);
378388
// TODO if we ever make this project based,
379389
// we need to update the cache here with the memberuid
380390
}
@@ -383,16 +393,36 @@ private function addUserToGroup(UnityUser $new_user): void
383393
{
384394
$this->entry->appendAttribute("memberuid", $new_user->uid);
385395
$this->entry->write();
386-
$this->REDIS->appendCacheArray($this->gid, "members", $new_user->uid);
387-
$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+
);
388408
}
389409

390410
private function removeUserFromGroup(UnityUser $old_user): void
391411
{
392412
$this->entry->removeAttributeEntryByValue("memberuid", $old_user->uid);
393413
$this->entry->write();
394-
$this->REDIS->removeCacheArray($this->gid, "members", $old_user->uid);
395-
$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+
);
396426
}
397427

398428
public function memberExists(UnityUser $user): bool

resources/lib/UnityLDAP.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,27 @@ public function getUidFromEmail(string $email): LDAPEntry
458458
}
459459
throw new exceptions\EntryNotFoundException($email);
460460
}
461+
462+
public function getSortedQualifiedUsersForRedis(): array
463+
{
464+
$qualified_users = $this->getQualifiedUsersUIDs();
465+
sort($qualified_users);
466+
return $qualified_users;
467+
}
468+
469+
public function getSortedOrgsForRedis(): array
470+
{
471+
$attributes = $this->getAllOrgGroupsAttributes(["cn"]);
472+
$groups = array_map(fn($x) => $x["cn"][0], $attributes);
473+
sort($groups);
474+
return $groups;
475+
}
476+
477+
public function getSortedGroupsForRedis(): array
478+
{
479+
$attributes = $this->getAllPIGroupsAttributes(["cn"]);
480+
$groups = array_map(fn($x) => $x["cn"][0], $attributes);
481+
sort($groups);
482+
return $groups;
483+
}
461484
}

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: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,54 @@ public function init(
105105
$org->addUser($this);
106106
}
107107

108-
$this->LDAP->getQualifiedUserGroup()->appendAttribute("memberuid", $this->uid);
109-
$this->LDAP->getQualifiedUserGroup()->write();
110-
111-
$this->REDIS->appendCacheArray("sorted_qualified_users", "", $this->uid);
112-
113108
$this->SQL->addLog($this->uid, $_SERVER["REMOTE_ADDR"], "user_added", $this->uid);
109+
}
114110

115-
if ($send_mail) {
116-
$this->MAILER->sendMail($this->getMail(), "user_created", [
117-
"user" => $this->uid,
118-
"org" => $this->getOrg(),
119-
]);
111+
public function isQualified(): bool
112+
{
113+
return $this->LDAP->getQualifiedUserGroup()->attributeValueExists("memberUid", $this->uid);
114+
}
115+
116+
public function setIsQualified(bool $newIsQualified, bool $doSendMail = true): void
117+
{
118+
$oldIsQualified = $this->isQualified();
119+
if ($oldIsQualified == $newIsQualified) {
120+
return;
121+
}
122+
if ($newIsQualified) {
123+
$this->LDAP->getQualifiedUserGroup()->appendAttribute("memberuid", $this->uid);
124+
$this->LDAP->getQualifiedUserGroup()->write();
125+
$default_value_getter = [$this->LDAP, "getSortedQualifiedUsersForRedis"];
126+
$this->REDIS->appendCacheArray(
127+
"sorted_qualified_users",
128+
"",
129+
$this->uid,
130+
$default_value_getter,
131+
);
132+
if ($doSendMail) {
133+
$this->MAILER->sendMail($this->getMail(), "user_qualified", [
134+
"user" => $this->uid,
135+
"org" => $this->getOrg(),
136+
]);
137+
}
138+
} else {
139+
$this->LDAP
140+
->getQualifiedUserGroup()
141+
->removeAttributeEntryByValue("memberuid", $this->uid);
142+
$this->LDAP->getQualifiedUserGroup()->write();
143+
$default_value_getter = [$this->LDAP, "getSortedQualifiedUsersForRedis"];
144+
$this->REDIS->removeCacheArray(
145+
"sorted_qualified_users",
146+
"",
147+
$this->uid,
148+
$default_value_getter,
149+
);
150+
if ($doSendMail) {
151+
$this->MAILER->sendMail($this->getMail(), "user_dequalified", [
152+
"user" => $this->uid,
153+
"org" => $this->getOrg(),
154+
]);
155+
}
120156
}
121157
}
122158

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// this template is sent when a user account is no longer qualified
4+
$this->Subject = "User Deactivated"; ?>
5+
6+
<p>Hello,</p>
7+
8+
<p>Your account on the Unity cluster has been deactivated.</p>
9+
10+
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
// this template is sent when a user account gets created
4-
$this->Subject = "User Created"; ?>
3+
// this template is sent when a user account becomes qualified
4+
$this->Subject = "User Activated"; ?>
55

66
<p>Hello,</p>
77

0 commit comments

Comments
 (0)