-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathUnityGroup.php
More file actions
501 lines (466 loc) · 17.1 KB
/
UnityGroup.php
File metadata and controls
501 lines (466 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
namespace UnityWebPortal\lib;
use Exception;
use UnityWebPortal\lib\exceptions\EntryNotFoundException;
enum UnityGroupUserRemovedReason: string
{
case RemovedByAdmin = "RemovedByAdmin";
case RemovedByOwner = "RemovedByOwner";
case RemovedSelf = "RemovedSelf";
}
/**
* Class that represents a single PI group in the Unity HPC Platform.
*/
class UnityGroup extends PosixGroup
{
public const string PI_PREFIX = "pi_";
public string $gid;
private UnityLDAP $LDAP;
private UnitySQL $SQL;
private UnityMailer $MAILER;
public function __construct(string $gid, UnityLDAP $LDAP, UnitySQL $SQL, UnityMailer $MAILER)
{
parent::__construct($LDAP->getPIGroupEntry(trim($gid)));
$this->gid = $gid;
$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
}
public function __toString(): string
{
return $this->gid;
}
public function requestGroup(?bool $send_mail_to_admins = null, bool $send_mail = true): void
{
$send_mail_to_admins ??= CONFIG["mail"]["send_pimesg_to_admins"];
if ($this->exists() && !$this->getIsDisabled()) {
return;
}
$context = [
"user" => $this->getOwner()->uid,
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullName(),
"email" => $this->getOwner()->getMail(),
];
$this->SQL->addRequest($this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI);
if ($send_mail) {
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_request");
if ($send_mail_to_admins) {
$this->MAILER->sendMail("admin", "group_request_admin", $context);
}
$this->MAILER->sendMail("pi_approve", "group_request_admin", $context);
}
}
/**
* email all members that group is now disabled, remove all members, log, set attribute
* the owner must manually remove all members first, but admins don't have this requirement
*/
public function disable(bool $send_mail = true): void
{
if ($this->getIsDisabled()) {
throw new Exception("cannot disable an already disabled group");
}
$this->SQL->addLog("disable_pi_group", $this->gid);
$memberuids = $this->getMemberUIDs();
if ($send_mail) {
$member_attributes = $this->LDAP->getUsersAttributes($memberuids, ["mail"]);
$member_mails = array_map(fn($x) => (string) $x["mail"][0], $member_attributes);
if (count($member_mails) > 0) {
$this->MAILER->sendMail($member_mails, "group_disabled", [
"group_name" => $this->gid,
]);
}
}
$this->setIsDisabled(true);
if (count($memberuids) > 0) {
$this->entry->setAttribute("memberuid", []);
}
// TODO optimize
// UnityUser::__construct() makes one LDAP query for each user
// updateIsQualified() makes one LDAP query for each member
// if user is no longer in any PI group, disqualify them
foreach ($memberuids as $uid) {
$user = new UnityUser($uid, $this->LDAP, $this->SQL, $this->MAILER);
$user->updateIsQualified($send_mail);
}
}
private function reenable(bool $send_mail = true): void
{
if (!$this->getIsDisabled()) {
throw new Exception("cannot re-enable a group that is not disabled");
}
$this->SQL->addLog("reenabled_pi_group", $this->gid);
if ($send_mail) {
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_reenabled",
["group_name" => $this->gid],
);
}
$this->setIsDisabled(false);
$owner_uid = $this->getOwner()->uid;
if (!$this->memberUIDExists($owner_uid)) {
$this->addMemberUID($owner_uid);
}
$this->getOwner()->updateIsQualified($send_mail);
}
/**
* This method will create the group (this is what is executed when an admin approved the group)
*/
public function approveGroup(bool $send_mail = true): void
{
$uid = $this->getOwner()->uid;
$request = $this->SQL->getRequest($uid, UnitySQL::REQUEST_BECOME_PI);
assert($this->getOwner()->exists());
if (!$this->entry->exists()) {
$this->init();
} elseif ($this->getIsDisabled()) {
$this->reenable();
} else {
throw new Exception("cannot approve group that already exists and is not disabled");
}
$this->SQL->removeRequest($this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI);
$this->SQL->addLog("approved_group", $this->getOwner()->uid);
if ($send_mail) {
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_created");
}
// having your own group makes you qualified
$this->getOwner()->updateIsQualified($send_mail);
}
/**
* This method is executed when an admin denys the PI group request
*/
public function denyGroup(bool $send_mail = true): void
{
$request = $this->SQL->getRequest($this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI);
$this->SQL->removeRequest($this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI);
if ($this->exists()) {
return;
}
$this->SQL->addLog("denied_group", $this->getOwner()->uid);
if ($send_mail) {
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_denied");
}
}
public function cancelGroupRequest(bool $send_mail = true): void
{
if (!$this->SQL->requestExists($this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI)) {
return;
}
$this->SQL->removeRequest($this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI);
if ($send_mail) {
$this->MAILER->sendMail("pi_approve", "group_request_cancelled", [
"uid" => $this->getOwner()->uid,
]);
}
}
public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true): void
{
if (!$this->requestExists($user)) {
return;
}
$this->SQL->removeRequest($user->uid, $this->gid);
if ($send_mail) {
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_join_request_cancelled",
["uid" => $user->uid],
);
}
}
/**
* This method is executed when a user is approved to join the group
* (either by admin or the group owner)
*/
public function approveUser(UnityUser $new_user, bool $send_mail = true): void
{
$request = $this->SQL->getRequest($new_user->uid, $this->gid);
assert($new_user->exists());
$this->addMemberUID($new_user->uid);
$this->SQL->removeRequest($new_user->uid, $this->gid);
$this->SQL->addLog(
"approved_user",
_json_encode(["user" => $new_user->uid, "group" => $this->gid]),
);
if ($send_mail) {
$this->MAILER->sendMail($new_user->getMail(), "group_user_added", [
"group" => $this->gid,
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_added_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullname(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
$new_user->updateIsQualified($send_mail); // being in a group makes you qualified
}
public function denyUser(UnityUser $new_user, bool $send_mail = true): void
{
$request = $this->SQL->getRequest($new_user->uid, $this->gid);
// remove request, this will fail silently if the request doesn't exist
$this->SQL->removeRequest($new_user->uid, $this->gid);
$this->SQL->addLog(
"denied_user",
_json_encode(["user" => $new_user->uid, "group" => $this->gid]),
);
if ($send_mail) {
$this->MAILER->sendMail($new_user->getMail(), "group_user_denied", [
"group" => $this->gid,
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_denied_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
}
public function removeUser(
UnityUser $new_user,
UnityGroupUserRemovedReason|UnityUserDisabledReason $why,
bool $send_mail = true,
): void {
if (!$this->memberUIDExists($new_user->uid)) {
return;
}
if ($new_user->uid == $this->getOwner()->uid) {
throw new Exception("Cannot delete group owner from group. Disable group instead");
}
$this->removeMemberUID($new_user->uid);
$this->SQL->addLog(
"removed_user",
_json_encode(["user" => $new_user->uid, "group" => $this->gid]),
);
if ($send_mail) {
$this->MAILER->sendMail($new_user->getMail(), "group_user_removed", [
"group" => $this->gid,
"why" => $why,
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_removed_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
"why" => $why,
],
);
}
// if user is no longer in any PI group, disqualify them
$new_user->updateIsQualified($send_mail);
}
public function newUserRequest(UnityUser $new_user, bool $send_mail = true): void
{
if ($this->memberUIDExists($new_user->uid)) {
UnityHTTPD::errorLog("warning", "user '$new_user' already in group");
return;
}
if ($this->requestExists($new_user)) {
UnityHTTPD::errorLog("warning", "user '$new_user' already requested group membership");
return;
}
$this->addRequest($new_user->uid);
if ($send_mail) {
$this->MAILER->sendMail($new_user->getMail(), "group_user_request", [
"group" => $this->gid,
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_request_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullname(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
}
/** @return array{0: UnityUser, 1: string}[] */
public function getRequests(): array
{
$requests = $this->SQL->getRequests($this->gid);
$out = [];
foreach ($requests as $request) {
$user = new UnityUser($request["uid"], $this->LDAP, $this->SQL, $this->MAILER);
array_push($out, [$user, $request["timestamp"]]);
}
return $out;
}
/** @return UnityUser[] */
public function getGroupMembers(): array
{
$members = $this->getMemberUIDs();
$out = [];
foreach ($members as $member) {
$user_obj = new UnityUser($member, $this->LDAP, $this->SQL, $this->MAILER);
array_push($out, $user_obj);
}
return $out;
}
public function requestExists(UnityUser $user): bool
{
$requesters = $this->getRequests();
if (count($requesters) > 0) {
foreach ($requesters as $requester) {
if ($requester[0]->uid == $user->uid) {
return true;
}
}
}
return false;
}
private function init(): void
{
$owner = $this->getOwner();
assert(!$this->entry->exists());
$nextGID = $this->LDAP->getNextPIGIDNumber();
$this->entry->create([
"objectclass" => ["xGroup", "posixGroup", "top"],
"gidnumber" => strval($nextGID),
"memberuid" => [$owner->uid],
"owneruid" => $owner->uid,
]);
// TODO if we ever make this project based,
// we need to update the cache here with the memberuid
}
private function addRequest(string $uid): void
{
$this->SQL->addRequest($uid, $this->gid);
}
public function getOwner(): UnityUser
{
return new UnityUser(
self::GID2OwnerUID($this->gid),
$this->LDAP,
$this->SQL,
$this->MAILER,
);
}
public static function ownerUID2GID(string $uid): string
{
return self::PI_PREFIX . $uid;
}
public static function GID2OwnerUID(string $gid): string
{
if (substr($gid, 0, strlen(self::PI_PREFIX)) != self::PI_PREFIX) {
throw new Exception("PI group GID doesn't have the correct prefix.");
}
return substr($gid, strlen(self::PI_PREFIX));
}
/**
* @param string[] $attributes
* @param array<string, mixed[]> $default_values
* @return array<string, mixed[]>
*/
public function getGroupMembersAttributes(array $attributes, array $default_values = []): array
{
return $this->LDAP->getUsersAttributes(
$this->getMemberUIDs(),
$attributes,
$default_values,
);
}
public function getIsDisabled(): bool
{
$value = $this->entry->getAttribute("isDisabled");
switch (count($value)) {
case 0:
return false;
case 1:
switch ($value[0]) {
case "TRUE":
return true;
case "FALSE":
return false;
default:
throw new \RuntimeException(
sprintf(
"unexpected value for isDisabled: '%s'. expected 'TRUE' or 'FALSE'",
$value[0],
),
);
}
default:
throw new \RuntimeException(
sprintf(
"expected value of length 0 or 1, found value %s of length %s",
_json_encode($value),
count($value),
),
);
}
}
private function setIsDisabled(bool $new_value): void
{
$this->entry->setAttribute("isDisabled", $new_value ? "TRUE" : "FALSE");
}
public function addManagerUID(string $uid): void
{
$new_manager = new UnityUser($uid, $this->LDAP, $this->SQL, $this->MAILER);
if (!$new_manager->exists()) {
throw new EntryNotFoundException("user '$uid' does not exist!");
}
$member_uids = $this->getMemberUIDs();
if (!in_array($uid, $member_uids)) {
throw new Exception("user '$uid' is not a group member!");
}
$this->entry->appendAttribute("managerUid", $uid);
}
public function removeManagerUID(string $uid): void
{
$this->entry->removeAttributeEntryByValue("managerUid", $uid);
}
public function managerUIDExists(string $uid): bool
{
return in_array($uid, $this->entry->getAttribute("managerUid"));
}
/** @return string[] */
public function getManagerUIDs(): array
{
return $this->entry->getAttribute("managerUid");
}
public function removeMemberUID(string $uid): void
{
if ($this->managerUIDExists($uid)) {
$this->removeManagerUID($uid);
}
parent::removeMemberUID($uid);
}
public function addPlusAddressToMail(string $mail): string
{
$owner = $this->getOwner();
$suffix = "_" . $owner->getOrg();
assert(str_ends_with($owner->uid, $suffix));
$short_name = substr($owner->uid, 0, -strlen($suffix));
$parts = explode("@", $mail, 2);
return sprintf("%s+%s@%s", $parts[0], $short_name, $parts[1]);
}
/** @return string[] */
public function getOwnerMailAndPlusAddressedManagerMails(): array
{
$mails = [$this->getOwner()->getMail()];
foreach ($this->getManagerUIDs() as $manager_uid) {
$manager = new UnityUser($manager_uid, $this->LDAP, $this->SQL, $this->MAILER);
array_push($mails, $this->addPlusAddressToMail($manager->getMail()));
}
$mails = array_unique($mails);
sort($mails);
return $mails;
}
}