Skip to content

Commit 86ca88e

Browse files
authored
add users to LDAP immediately (#349)
1 parent 7070ef8 commit 86ca88e

26 files changed

+531
-801
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: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -65,43 +65,28 @@ public function exists(): bool
6565
return $this->entry->exists();
6666
}
6767

68-
public function requestGroup(
69-
string $firstname,
70-
string $lastname,
71-
string $email,
72-
string $org,
73-
bool $send_mail_to_admins,
74-
bool $send_mail = true,
75-
): void {
68+
public function requestGroup(bool $send_mail_to_admins, bool $send_mail = true): void
69+
{
7670
if ($this->exists()) {
7771
return;
7872
}
7973
if ($this->SQL->accDeletionRequestExists($this->getOwner()->uid)) {
8074
return;
8175
}
82-
$this->SQL->addRequest($this->getOwner()->uid, $firstname, $lastname, $email, $org);
76+
$context = [
77+
"user" => $this->getOwner()->uid,
78+
"org" => $this->getOwner()->getOrg(),
79+
"name" => $this->getOwner()->getFullName(),
80+
"email" => $this->getOwner()->getMail(),
81+
];
82+
$this->SQL->addRequest($this->getOwner()->uid);
8383
if ($send_mail) {
84-
$this->MAILER->sendMail($email, "group_request");
85-
$this->WEBHOOK->sendWebhook("group_request_admin", [
86-
"user" => $this->getOwner()->uid,
87-
"org" => $org,
88-
"name" => "$firstname $lastname",
89-
"email" => $email,
90-
]);
84+
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_request");
85+
$this->WEBHOOK->sendWebhook("group_request_admin", $context);
9186
if ($send_mail_to_admins) {
92-
$this->MAILER->sendMail("admin", "group_request_admin", [
93-
"user" => $this->getOwner()->uid,
94-
"org" => $org,
95-
"name" => "$firstname $lastname",
96-
"email" => $email,
97-
]);
87+
$this->MAILER->sendMail("admin", "group_request_admin", $context);
9888
}
99-
$this->MAILER->sendMail("pi_approve", "group_request_admin", [
100-
"user" => $this->getOwner()->uid,
101-
"org" => $org,
102-
"name" => "$firstname $lastname",
103-
"email" => $email,
104-
]);
89+
$this->MAILER->sendMail("pi_approve", "group_request_admin", $context);
10590
}
10691
}
10792

@@ -115,15 +100,7 @@ public function approveGroup(?UnityUser $operator = null, bool $send_mail = true
115100
if ($this->exists()) {
116101
return;
117102
}
118-
if (!$this->getOwner()->exists()) {
119-
$this->getOwner()->init(
120-
$request["firstname"],
121-
$request["lastname"],
122-
$request["email"],
123-
$request["org"],
124-
$send_mail,
125-
);
126-
}
103+
\ensure($this->getOwner()->exists());
127104
$this->init();
128105
$this->SQL->removeRequest($this->getOwner()->uid);
129106
$operator = is_null($operator) ? $this->getOwner()->uid : $operator->uid;
@@ -134,8 +111,9 @@ public function approveGroup(?UnityUser $operator = null, bool $send_mail = true
134111
$this->getOwner()->uid,
135112
);
136113
if ($send_mail) {
137-
$this->MAILER->sendMail($request["email"], "group_created");
114+
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_created");
138115
}
116+
$this->getOwner()->setIsQualified(true); // having your own group makes you qualified
139117
}
140118

141119
/**
@@ -156,7 +134,7 @@ public function denyGroup(?UnityUser $operator = null, bool $send_mail = true):
156134
$this->getOwner()->uid,
157135
);
158136
if ($send_mail) {
159-
$this->MAILER->sendMail($request["email"], "group_denied");
137+
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_denied");
160138
}
161139
}
162140

@@ -237,14 +215,7 @@ public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true):
237215
public function approveUser(UnityUser $new_user, bool $send_mail = true): void
238216
{
239217
$request = $this->SQL->getRequest($new_user->uid, $this->gid);
240-
if (!$new_user->exists()) {
241-
$new_user->init(
242-
$request["firstname"],
243-
$request["lastname"],
244-
$request["email"],
245-
$request["org"],
246-
);
247-
}
218+
\ensure($new_user->exists());
248219
$this->addUserToGroup($new_user);
249220
$this->SQL->removeRequest($new_user->uid, $this->gid);
250221
if ($send_mail) {
@@ -254,11 +225,12 @@ public function approveUser(UnityUser $new_user, bool $send_mail = true): void
254225
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_added_owner", [
255226
"group" => $this->gid,
256227
"user" => $new_user->uid,
257-
"name" => $request["firstname"] . " " . $request["lastname"],
258-
"email" => $request["email"],
259-
"org" => $request["org"],
228+
"name" => $new_user->getFullname(),
229+
"email" => $new_user->getMail(),
230+
"org" => $new_user->getOrg(),
260231
]);
261232
}
233+
$new_user->setIsQualified(true); // being in a group makes you qualified
262234
}
263235

264236
public function denyUser(UnityUser $new_user, bool $send_mail = true): void
@@ -267,7 +239,7 @@ public function denyUser(UnityUser $new_user, bool $send_mail = true): void
267239
// remove request, this will fail silently if the request doesn't exist
268240
$this->SQL->removeRequest($new_user->uid, $this->gid);
269241
if ($send_mail) {
270-
$this->MAILER->sendMail($request["email"], "group_user_denied", [
242+
$this->MAILER->sendMail($new_user->getMail(), "group_user_denied", [
271243
"group" => $this->gid,
272244
]);
273245
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_denied_owner", [
@@ -304,14 +276,8 @@ public function removeUser(UnityUser $new_user, bool $send_mail = true): void
304276
}
305277
}
306278

307-
public function newUserRequest(
308-
UnityUser $new_user,
309-
string $firstname,
310-
string $lastname,
311-
string $email,
312-
string $org,
313-
bool $send_mail = true,
314-
): void {
279+
public function newUserRequest(UnityUser $new_user, bool $send_mail = true): void
280+
{
315281
if ($this->memberExists($new_user)) {
316282
UnityHTTPD::errorLog("warning", "user '$new_user' already in group");
317283
return;
@@ -324,17 +290,17 @@ public function newUserRequest(
324290
throw new Exception("user '$new_user' requested account deletion");
325291
return;
326292
}
327-
$this->addRequest($new_user->uid, $firstname, $lastname, $email, $org);
293+
$this->addRequest($new_user->uid);
328294
if ($send_mail) {
329-
$this->MAILER->sendMail($email, "group_user_request", [
295+
$this->MAILER->sendMail($new_user->getMail(), "group_user_request", [
330296
"group" => $this->gid,
331297
]);
332298
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_request_owner", [
333299
"group" => $this->gid,
334300
"user" => $new_user->uid,
335-
"name" => "$firstname $lastname",
336-
"email" => $email,
337-
"org" => $org,
301+
"name" => $new_user->getFullname(),
302+
"email" => $new_user->getMail(),
303+
"org" => $new_user->getOrg(),
338304
]);
339305
}
340306
}
@@ -352,14 +318,7 @@ public function getRequests(): array
352318
$this->REDIS,
353319
$this->WEBHOOK,
354320
);
355-
array_push($out, [
356-
$user,
357-
$request["timestamp"],
358-
$request["firstname"],
359-
$request["lastname"],
360-
$request["email"],
361-
$request["org"],
362-
]);
321+
array_push($out, [$user, $request["timestamp"]]);
363322
}
364323
return $out;
365324
}
@@ -471,14 +430,9 @@ public function memberExists(UnityUser $user): bool
471430
return in_array($user->uid, $this->getGroupMemberUIDs());
472431
}
473432

474-
private function addRequest(
475-
string $uid,
476-
string $firstname,
477-
string $lastname,
478-
string $email,
479-
string $org,
480-
): void {
481-
$this->SQL->addRequest($uid, $firstname, $lastname, $email, $org, $this->gid);
433+
private function addRequest(string $uid): void
434+
{
435+
$this->SQL->addRequest($uid, $this->gid);
482436
}
483437

484438
public function getOwner(): UnityUser

resources/lib/UnitySQL.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,17 @@ public function getConn(): PDO
3434
//
3535
// requests table methods
3636
//
37-
public function addRequest(
38-
string $requestor,
39-
string $firstname,
40-
string $lastname,
41-
string $email,
42-
string $org,
43-
string $dest = self::REQUEST_BECOME_PI,
44-
): void {
37+
public function addRequest(string $requestor, string $dest = self::REQUEST_BECOME_PI): void
38+
{
4539
if ($this->requestExists($requestor, $dest)) {
4640
return;
4741
}
4842

4943
$stmt = $this->conn->prepare(
50-
"INSERT INTO " .
51-
self::TABLE_REQS .
52-
" " .
53-
"(uid, firstname, lastname, email, org, request_for) VALUES " .
54-
"(:uid, :firstname, :lastname, :email, :org, :request_for)",
44+
"INSERT INTO " . self::TABLE_REQS . " (uid, request_for) VALUES (:uid, :request_for)",
5545
);
5646
$stmt->bindParam(":uid", $requestor);
5747
$stmt->bindParam(":request_for", $dest);
58-
$stmt->bindParam(":firstname", $firstname);
59-
$stmt->bindParam(":lastname", $lastname);
60-
$stmt->bindParam(":email", $email);
61-
$stmt->bindParam(":org", $org);
62-
6348
$stmt->execute();
6449
}
6550

resources/lib/UnityUser.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +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-
$default_value_getter = [$this->LDAP, "getSortedQualifiedUsersForRedis"];
112-
$this->REDIS->appendCacheArray(
113-
"sorted_qualified_users",
114-
"",
115-
$this->uid,
116-
$default_value_getter,
117-
);
118-
119108
$this->SQL->addLog($this->uid, $_SERVER["REMOTE_ADDR"], "user_added", $this->uid);
109+
}
120110

121-
if ($send_mail) {
122-
$this->MAILER->sendMail($this->getMail(), "user_created", [
123-
"user" => $this->uid,
124-
"org" => $this->getOrg(),
125-
]);
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+
}
126156
}
127157
}
128158

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)