-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnityUser.php
700 lines (591 loc) · 19 KB
/
UnityUser.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<?php
namespace UnityWebPortal\lib;
use PHPOpenLDAPer\LDAPEntry;
use Exception;
class UnityUser
{
private const HOME_DIR = "/home/";
private $uid;
// service stack
private $LDAP;
private $SQL;
private $MAILER;
private $REDIS;
private $WEBHOOK;
public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
{
$this->uid = $uid;
$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
$this->REDIS = $REDIS;
$this->WEBHOOK = $WEBHOOK;
}
public function equals($other_user)
{
if (!is_a($other_user, self::class)) {
throw new Exception("Unable to check equality because the parameter is not a " . self::class . " object");
}
return $this->getUID() == $other_user->getUID();
}
/**
* This is the method that is run when a new account is created
*
* @param string $firstname First name of new account
* @param string $lastname Last name of new account
* @param string $email email of new account
* @param bool $isPI boolean value for if the user checked the "I am a PI box"
* @return void
*/
public function init($send_mail = true)
{
//
// Create LDAP group
//
$ldapGroupEntry = $this->getLDAPGroup();
$id = $this->LDAP->getUnassignedID($this->getUID(), $this->SQL);
if (!$ldapGroupEntry->exists()) {
$ldapGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$ldapGroupEntry->setAttribute("gidnumber", strval($id));
if (!$ldapGroupEntry->write()) {
throw new Exception("Failed to create POSIX group for $this->uid");
}
}
//
// Create LDAP user
//
$ldapUserEntry = $this->getLDAPUser();
if (!$ldapUserEntry->exists()) {
$ldapUserEntry->setAttribute("objectclass", UnityLDAP::POSIX_ACCOUNT_CLASS);
$ldapUserEntry->setAttribute("uid", $this->uid);
$ldapUserEntry->setAttribute("givenname", $this->getFirstname());
$ldapUserEntry->setAttribute("sn", $this->getLastname());
$ldapUserEntry->setAttribute("mail", $this->getMail());
$ldapUserEntry->setAttribute("o", $this->getOrg());
$ldapUserEntry->setAttribute("homedirectory", self::HOME_DIR . $this->uid);
$ldapUserEntry->setAttribute("loginshell", $this->LDAP->getDefUserShell());
$ldapUserEntry->setAttribute("uidnumber", strval($id));
$ldapUserEntry->setAttribute("gidnumber", strval($id));
if (!$ldapUserEntry->write()) {
$ldapGroupEntry->delete(); // Cleanup previous group
throw new Exception("Failed to create POSIX user for $this->uid");
}
}
// update cache
//$this->REDIS->setCache($this->uid, "firstname", $this->getFirstname());
//$this->REDIS->setCache($this->uid, "lastname", $this->getLastname());
//$this->REDIS->setCache($this->uid, "mail", $this->getMail());
//$this->REDIS->setCache($this->uid, "org", $this->getOrg());
$this->REDIS->setCache($this->uid, "homedir", self::HOME_DIR . $this->uid);
$this->REDIS->setCache($this->uid, "loginshell", $this->LDAP->getDefUserShell());
$this->REDIS->setCache($this->uid, "sshkeys", array());
//
// add to org group
//
$orgEntry = $this->getOrgGroup();
// create organization if it doesn't exist
if (!$orgEntry->exists()) {
$orgEntry->init();
}
if (!$orgEntry->userExists($this)) {
$orgEntry->addUser($this);
}
// add user to cache
$this->REDIS->appendCacheArray("sorted_users", "", $this->getUID());
//
// add to audit log
//
$this->SQL->addLog(
$this->getUID(),
$_SERVER['REMOTE_ADDR'],
"user_added",
$this->getUID()
);
//
// send email to user
//
if ($send_mail) {
$this->MAILER->sendMail(
$this->getMail(),
"user_created",
array("user" => $this->uid, "org" => $this->getOrg())
);
}
}
/**
* Returns the ldap account entry corresponding to the user
*
* @return ldapEntry posix account
*/
public function getLDAPUser(): LdapEntry
{
return $this->LDAP->getUserEntry($this->uid);
}
/**
* Returns the ldap group entry corresponding to the user
*
* @return ldapEntry posix group
*/
public function getLDAPGroup(): LdapEntry
{
return $this->LDAP->getGroupEntry($this->uid);
}
public function exists()
{
return $this->getLDAPUser()->exists() && $this->getLDAPGroup()->exists();
}
//
// User Attribute Functions
//
/**
* Get method for NetID
*
* @return string Net ID of user
*/
public function getUID()
{
return $this->uid;
}
public function setOrg($org)
{
$ldap_user = $this->getLDAPUser();
$ldap_user->setAttribute("o", $org);
if (!$ldap_user->write()) {
throw new Exception("Error updating LDAP entry $this->uid");
}
$this->REDIS->setCache($this->uid, "org", $org);
}
public function getOrg($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "org");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$org = $this->getLDAPUser()->getAttribute("o")[0];
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "org", $org);
}
return $this->getLDAPUser()->getAttribute("o")[0];
}
return null;
}
/**
* Sets the firstname of the account and the corresponding ldap entry if it exists
*
* @param string $firstname
*/
public function setFirstname($firstname, $operator = null)
{
$ldap_user = $this->getLDAPUser();
$ldap_user->setAttribute("givenname", $firstname);
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"firstname_changed",
$this->getUID()
);
if (!$ldap_user->write()) {
throw new Exception("Error updating LDAP entry $this->uid");
}
$this->REDIS->setCache($this->uid, "firstname", $firstname);
}
/**
* Gets the firstname of the account
*
* @return string firstname
*/
public function getFirstname($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "firstname");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$firstname = $this->getLDAPUser()->getAttribute("givenname")[0];
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "firstname", $firstname);
}
return $firstname;
}
return null;
}
/**
* Sets the lastname of the account and the corresponding ldap entry if it exists
*
* @param string $lastname
*/
public function setLastname($lastname, $operator = null)
{
$ldap_user = $this->getLDAPUser();
$ldap_user->setAttribute("sn", $lastname);
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"lastname_changed",
$this->getUID()
);
if (!$this->getLDAPUser()->write()) {
throw new Exception("Error updating LDAP entry $this->uid");
}
$this->REDIS->setCache($this->uid, "lastname", $lastname);
}
/**
* Get method for the lastname on the account
*
* @return string lastname
*/
public function getLastname($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "lastname");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$lastname = $this->getLDAPUser()->getAttribute("sn")[0];
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "lastname", $lastname);
}
return $lastname;
}
return null;
}
public function getFullname()
{
return $this->getFirstname() . " " . $this->getLastname();
}
/**
* Sets the mail in the account and the ldap entry
*
* @param string $mail
*/
public function setMail($email, $operator = null)
{
$ldap_user = $this->getLDAPUser();
$ldap_user->setAttribute("mail", $email);
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"email_changed",
$this->getUID()
);
if (!$this->getLDAPUser()->write()) {
throw new Exception("Error updating LDAP entry $this->uid");
}
$this->REDIS->setCache($this->uid, "mail", $email);
}
/**
* Method to get the mail instance var
*
* @return string email address
*/
public function getMail($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "mail");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$mail = $this->getLDAPUser()->getAttribute("mail")[0];
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "mail", $mail);
}
return $mail;
}
return null;
}
/**
* Sets the SSH keys on the account and the corresponding entry
*
* @param array $keys String array of openssh-style ssh public keys
*/
public function setSSHKeys($keys, $operator = null, $send_mail = true)
{
$ldapUser = $this->getLDAPUser();
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
$keys_filt = array_values(array_unique($keys));
if ($ldapUser->exists()) {
$ldapUser->setAttribute("sshpublickey", $keys_filt);
if (!$ldapUser->write()) {
throw new Exception("Failed to modify SSH keys for $this->uid");
}
}
$this->REDIS->setCache($this->uid, "sshkeys", $keys_filt);
//
// add audit log
//
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"sshkey_modify",
$this->getUID()
);
if ($send_mail) {
$this->MAILER->sendMail(
$this->getMail(),
"user_sshkey",
array("keys" => $this->getSSHKeys())
);
}
}
/**
* Returns the SSH keys attached to the account
*
* @return array String array of ssh keys
*/
public function getSSHKeys($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "sshkeys");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$ldapUser = $this->getLDAPUser();
$result = $ldapUser->getAttribute("sshpublickey");
if (is_null($result)) {
$keys = array();
} else {
$keys = $result;
}
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "sshkeys", $keys);
}
return $keys;
}
return null;
}
/**
* Sets the login shell for the account
*
* @param string $shell absolute path to shell
*/
public function setLoginShell($shell, $operator = null, $send_mail = true)
{
$ldapUser = $this->getLDAPUser();
if ($ldapUser->exists()) {
$ldapUser->setAttribute("loginshell", $shell);
if (!$ldapUser->write()) {
throw new Exception("Failed to modify login shell for $this->uid");
}
}
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"loginshell_changed",
$this->getUID()
);
$this->REDIS->setCache($this->uid, "loginshell", $shell);
if ($send_mail) {
$this->MAILER->sendMail(
$this->getMail(),
"user_loginshell",
array("new_shell" => $this->getLoginShell())
);
}
}
/**
* Gets the login shell of the account
*
* @return string absolute path to login shell
*/
public function getLoginShell($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "loginshell");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$ldapUser = $this->getLDAPUser();
$loginshell = $ldapUser->getAttribute("loginshell")[0];
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "loginshell", $loginshell);
}
return $loginshell;
}
return null;
}
public function setHomeDir($home, $operator = null)
{
$ldapUser = $this->getLDAPUser();
if ($ldapUser->exists()) {
$ldapUser->setAttribute("homedirectory", $home);
if (!$ldapUser->write()) {
throw new Exception("Failed to modify home directory for $this->uid");
}
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
$this->SQL->addLog(
$operator,
$_SERVER['REMOTE_ADDR'],
"homedir_changed",
$this->getUID()
);
$this->REDIS->setCache($this->uid, "homedir", $home);
}
}
/**
* Gets the home directory of the user
*
* @return string home directory of the user
*/
public function getHomeDir($ignorecache = false)
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "homedir");
if (!is_null($cached_val)) {
return $cached_val;
}
}
if ($this->exists()) {
$ldapUser = $this->getLDAPUser();
$homedir = $ldapUser->getAttribute("homedirectory");
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "homedir", $homedir);
}
return $homedir;
}
return null;
}
/**
* Checks if the current account is an admin (in the sudo group)
*
* @return boolean true if admin, false if not
*/
public function isAdmin()
{
$admins = $this->LDAP->getAdminGroup()->getAttribute("memberuid");
$admins = (is_null($admins) ? [] : $admins);
return in_array($this->uid, $admins);
}
/**
* Checks if current user is a PI
*
* @return boolean true is PI, false if not
*/
public function isPI()
{
return $this->getPIGroup()->exists();
}
public function getPIGroup()
{
return new UnityGroup(
UnityGroup::getPIUIDfromUID($this->uid),
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS,
$this->WEBHOOK
);
}
public function getOrgGroup()
{
return new UnityOrg(
$this->getOrg(),
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS,
$this->WEBHOOK
);
}
/**
* Gets the groups this user is assigned to, can be more than one
* @return [type]
*/
public function getGroups($ignorecache = false)
{
$out = array();
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getUID(), "groups");
if (!is_null($cached_val)) {
$groups = $cached_val;
foreach ($groups as $group) {
$group_obj = new UnityGroup(
$group,
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS,
$this->WEBHOOK
);
array_push($out, $group_obj);
}
return $out;
}
}
$all_pi_groups = $this->LDAP->getAllPIGroups($this->SQL, $this->MAILER, $this->REDIS, $ignorecache);
$cache_arr = array();
foreach ($all_pi_groups as $pi_group) {
if (in_array($this->getUID(), $pi_group->getGroupMemberUIDs())) {
array_push($out, $pi_group);
array_push($cache_arr, $pi_group->getPIUID());
}
}
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "groups", $cache_arr);
}
return $out;
}
/**
* Sends an email to admins about account deletion request and also adds it to a table in the database
*/
public function requestAccountDeletion()
{
$this->SQL->addAccountDeletionRequest($this->getUID());
$this->MAILER->sendMail(
"admin",
"account_deletion_request_admin",
array(
"user" => $this->getUID(),
"name" => $this->getFullname(),
"email" => $this->getMail()
)
);
}
/**
* Checks if the user has requested account deletion
*
* @return boolean true if account deletion has been requested, false if not
*/
public function hasRequestedAccountDeletion()
{
return $this->SQL->accDeletionRequestExists($this->getUID());
}
/**
* Checks whether a user is in a group or not
* @param string $uid uid of the user
* @param string or object $group group to check
* @return boolean true if user is in group, false if not
*/
public function isInGroup($uid, $group)
{
if (gettype($group) == "string") {
$group_checked = new UnityGroup(
$group,
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS,
$this->WEBHOOK
);
} else {
$group_checked = $group;
}
return in_array($uid, $group_checked->getGroupMemberUIDs());
}
}