-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnityGroup.php
558 lines (473 loc) · 16.3 KB
/
UnityGroup.php
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
namespace UnityWebPortal\lib;
use Exception;
use PHPOpenLDAPer\LdapEntry;
/**
* Class that represents a single PI group in the Unity Cluster.
*/
class UnityGroup
{
public const PI_PREFIX = "pi_";
private $pi_uid;
// Services
private $LDAP;
private $SQL;
private $MAILER;
private $WEBHOOK;
private $REDIS;
/**
* Constructor for the object
*
* @param string $pi_uid PI UID in the format <PI_PREFIX><OWNER_UID>
* @param LDAP $LDAP LDAP Connection
* @param SQL $SQL SQL Connection
*/
public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
{
$this->pi_uid = $pi_uid;
$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
$this->REDIS = $REDIS;
$this->WEBHOOK = $WEBHOOK;
}
public function equals($other_group)
{
if (!is_a($other_group, self::class)) {
throw new Exception("Unable to check equality because the parameter is not a " . self::class . " object");
}
return $this->getPIUID() == $other_group->getPIUID();
}
/**
* Returns this group's PI UID
*
* @return string PI UID of the group
*/
public function getPIUID()
{
return $this->pi_uid;
}
/**
* Checks if the current PI is an approved and existent group
*
* @return bool true if yes, false if no
*/
public function exists()
{
return $this->GetLDAPEntry()->exists();
}
//
// Portal-facing methods, these are the methods called by scripts in webroot
//
public function requestGroup($send_mail_to_admins, $send_mail = true)
{
// check for edge cases...
if ($this->exists()) {
return;
}
// check if account deletion request already exists
if ($this->SQL->accDeletionRequestExists($this->getOwner()->getUID())) {
return;
}
$this->SQL->addRequest($this->getOwner()->getUID());
if ($send_mail) {
// send email to requestor
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_request"
);
$this->WEBHOOK->sendWebhook(
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
)
);
if ($send_mail_to_admins) {
$this->MAILER->sendMail(
"admin",
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
)
);
}
$this->MAILER->sendMail(
"pi_approve",
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
)
);
}
}
/**
* This method will create the group (this is what is executed when an admin approved the group)
*/
public function approveGroup($operator = null, $send_mail = true)
{
// check for edge cases...
if ($this->exists()) {
return;
}
// check if owner exists
if (!$this->getOwner()->exists()) {
$this->getOwner()->init();
}
// initialize ldap objects, if this fails the script will crash, but nothing will persistently break
$this->init();
// remove the request from the sql table
// this will silently fail if the request doesn't exist
$this->SQL->removeRequest($this->getOwner()->getUID());
$operator = is_null($operator) ? $this->getOwner()->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"approved_group",
$this->getOwner()->getUID()
);
// send email to the newly approved PI
if ($send_mail) {
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_created"
);
}
}
/**
* This method is executed when an admin denys the PI group request
*/
public function denyGroup($operator = null, $send_mail = true)
{
// remove request - this will fail silently if the request doesn't exist
$this->SQL->removeRequest($this->getOwner()->getUID());
if ($this->exists()) {
return;
}
$operator = is_null($operator) ? $this->getOwner()->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"denied_group",
$this->getOwner()->getUID()
);
// send email to the requestor
if ($send_mail) {
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_denied"
);
}
}
/**
* This method will delete the group, either by admin action or PI action
*/
public function removeGroup($send_mail = true)
{
// remove any pending requests
// this will silently fail if the request doesn't exist (which is what we want)
$this->SQL->removeRequests($this->pi_uid);
// we don't need to do anything extra if the group is already deleted
if (!$this->exists()) {
return;
}
// first, we must record the users in the group currently
$users = $this->getGroupMembers();
// now we delete the ldap entry
$ldapPiGroupEntry = $this->GetLDAPEntry();
if ($ldapPiGroupEntry->exists()) {
if (!$ldapPiGroupEntry->delete()) {
throw new Exception("Unable to delete PI ldap group");
}
$this->REDIS->removeCacheArray("sorted_groups", "", $this->getPIUID());
foreach ($users as $user) {
$this->REDIS->removeCacheArray($user->getUID(), "groups", $this->getPIUID());
}
}
// send email to every user of the now deleted PI group
if ($send_mail) {
foreach ($users as $user) {
$this->MAILER->sendMail(
$user->getMail(),
"group_disband",
array("group_name" => $this->pi_uid)
);
}
}
}
/**
* This method is executed when a user is approved to join the group (either by admin or the group owner)
*/
public function approveUser($new_user, $send_mail = true)
{
// check if user exists
if (!$new_user->exists()) {
$new_user->init();
}
// add user to the LDAP object
$this->addUserToGroup($new_user);
// remove request, this will fail silently if the request doesn't exist
$this->removeRequest($new_user->getUID());
// send email to the requestor
if ($send_mail) {
// send email to the user
$this->MAILER->sendMail(
$new_user->getMail(),
"group_user_added",
array("group" => $this->pi_uid)
);
// send email to the PI
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_user_added_owner",
array(
"group" => $this->pi_uid,
"user" => $new_user->getUID(),
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg()
)
);
}
}
public function denyUser($new_user, $send_mail = true)
{
if (!$this->requestExists($new_user)) {
return;
}
// remove request, this will fail silently if the request doesn't exist
$this->removeRequest($new_user->getUID());
if ($send_mail) {
// send email to the user
$this->MAILER->sendMail(
$new_user->getMail(),
"group_user_denied",
array("group" => $this->pi_uid)
);
// send email to the PI
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_user_denied_owner",
array(
"group" => $this->pi_uid,
"user" => $new_user->getUID(),
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg()
)
);
}
}
public function removeUser($new_user, $send_mail = true)
{
if (!$this->userExists($new_user)) {
return;
}
if ($new_user->getUID() == $this->getOwner()->getUID()) {
throw new Exception("Cannot delete group owner from group. Disband group instead");
}
// remove request, this will fail silently if the request doesn't exist
$this->removeUserFromGroup($new_user);
if ($send_mail) {
// send email to the user
$this->MAILER->sendMail(
$new_user->getMail(),
"group_user_removed",
array("group" => $this->pi_uid)
);
// send email to the PI
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_user_removed_owner",
array(
"group" => $this->pi_uid,
"user" => $new_user->getUID(),
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg()
)
);
}
}
public function newUserRequest($new_user, $send_mail = true)
{
if ($this->userExists($new_user)) {
return;
}
if ($this->requestExists($new_user)) {
return;
}
// check if account deletion request already exists
if ($this->SQL->accDeletionRequestExists($new_user->getUID())) {
return;
}
$this->addRequest($new_user->getUID());
if ($send_mail) {
// send email to user
$this->MAILER->sendMail(
$new_user->getMail(),
"group_user_request",
array("group" => $this->pi_uid)
);
// send email to PI
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
"group_user_request_owner",
array(
"group" => $this->pi_uid,
"user" => $new_user->getUID(),
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg()
)
);
}
}
public function getRequests()
{
$requests = $this->SQL->getRequests($this->pi_uid);
$out = array();
foreach ($requests as $request) {
$user = new UnityUser(
$request["uid"],
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS,
$this->WEBHOOK
);
array_push($out, [$user, $request["timestamp"]]);
}
return $out;
}
public function getGroupMembers($ignorecache = false)
{
$memberuids = $this->getGroupMemberUIDs($ignorecache);
$out = array();
foreach ($memberuids as $uid) {
$user_obj = new UnityUser($uid, $this->LDAP, $this->SQL, $this->MAILER, $this->REDIS, $this->WEBHOOK);
array_push($out, $user_obj);
}
return $out;
}
public function getGroupMemberUIDs($ignorecache = false): array
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getPIUID(), "members");
if (!is_null($cached_val)) {
return $cached_val;
}
}
$entry = $this->getLDAPEntry();
$members = $entry->getAttribute("memberuid") ?? [];
sort($members);
$this->REDIS->setCache($this->getPIUID(), "members", $members);
return $members;
}
public function requestExists($user)
{
$requesters = $this->getRequests();
if (count($requesters) > 0) {
foreach ($requesters as $requester) {
if ($requester[0]->getUID() == $user->getUID()) {
return true;
}
}
}
return false;
}
//
// Private functions called by functions above
//
private function init()
{
// make this user a PI
$owner = $this->getOwner();
// (1) Create LDAP PI group
$ldapPiGroupEntry = $this->GetLDAPEntry();
if (!$ldapPiGroupEntry->exists()) {
$nextGID = $this->LDAP->getNextPiGIDNumber($this->SQL);
$ldapPiGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$ldapPiGroupEntry->setAttribute("gidnumber", strval($nextGID));
$ldapPiGroupEntry->setAttribute("memberuid", array($owner->getUID()));
if (!$ldapPiGroupEntry->write()) {
throw new Exception("Failed to create POSIX group for " . $owner->getUID()); // this shouldn't execute
}
}
$this->REDIS->appendCacheArray("sorted_groups", "", $this->getPIUID());
// TODO if we ever make this project based, we need to update the cache here with the memberuid
}
private function addUserToGroup($new_user)
{
// Add to LDAP Group
$pi_group = $this->GetLDAPEntry();
$pi_group->appendAttribute("memberuid", $new_user->getUID());
if (!$pi_group->write()) {
throw new Exception("Unable to write PI group");
}
$this->REDIS->appendCacheArray($this->getPIUID(), "members", $new_user->getUID());
$this->REDIS->appendCacheArray($new_user->getUID(), "groups", $this->getPIUID());
}
private function removeUserFromGroup($old_user)
{
// Remove from LDAP Group
$pi_group = $this->GetLDAPEntry();
$pi_group->removeAttributeEntryByValue("memberuid", $old_user->getUID());
if (!$pi_group->write()) {
throw new Exception("Unable to write PI group");
}
$this->REDIS->removeCacheArray($this->getPIUID(), "members", $old_user->getUID());
$this->REDIS->removeCacheArray($old_user->getUID(), "groups", $this->getPIUID());
}
public function userExists(UnityUser $user, $ignorecache = false): bool
{
$members = $this->getGroupMemberUIDs($ignorecache);
return in_array($user->getUID(), $members);
}
private function addRequest($uid)
{
$this->SQL->addRequest($uid, $this->pi_uid);
}
private function removeRequest($uid)
{
$this->SQL->removeRequest($uid, $this->pi_uid);
}
//
// Public helper functions
//
public function getOwner()
{
return new UnityUser(
self::getUIDfromPIUID($this->pi_uid),
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS,
$this->WEBHOOK
);
}
public function getLDAPEntry(): LdapEntry
{
return $this->LDAP->getPIGroupEntry($this->pi_uid);
}
public static function getPIUIDfromUID($uid)
{
return self::PI_PREFIX . $uid;
}
public static function getUIDfromPIUID($pi_uid)
{
if (substr($pi_uid, 0, strlen(self::PI_PREFIX)) == self::PI_PREFIX) {
return substr($pi_uid, strlen(self::PI_PREFIX));
} else {
throw new Exception("PI netid doesn't have the correct prefix.");
}
}
}