Skip to content

Commit 932b89e

Browse files
committed
store entry as property
1 parent cdd85a3 commit 932b89e

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

resources/lib/UnityGroup.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class UnityGroup
1212
public const PI_PREFIX = "pi_";
1313

1414
private $pi_uid;
15+
private $entry;
1516

1617
// Services
1718
private $LDAP;
@@ -30,6 +31,7 @@ class UnityGroup
3031
public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
3132
{
3233
$this->pi_uid = $pi_uid;
34+
$this->entry = $LDAP->getPIGroupEntry($pi_uid);
3335

3436
$this->LDAP = $LDAP;
3537
$this->SQL = $SQL;
@@ -69,7 +71,7 @@ public function getPIUID()
6971
*/
7072
public function exists()
7173
{
72-
return $this->getLDAPPiGroup()->exists();
74+
return $this->entry->exists();
7375
}
7476

7577
//
@@ -265,7 +267,7 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
265267
// $users = $this->getGroupMembers();
266268

267269
// // now we delete the ldap entry
268-
// $ldapPiGroupEntry = $this->getLDAPPiGroup();
270+
// $ldapPiGroupEntry = $this->entry;
269271
// if ($ldapPiGroupEntry->exists()) {
270272
// $ldapPiGroupEntry->delete();
271273
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->getPIUID());
@@ -501,7 +503,7 @@ public function getGroupMemberUIDs($ignorecache = false)
501503
}
502504
$updatecache = false;
503505
if (!isset($members)) {
504-
$pi_group = $this->getLDAPPiGroup();
506+
$pi_group = $this->entry;
505507
$members = $pi_group->getAttribute("memberuid");
506508
$updatecache = true;
507509
}
@@ -536,7 +538,7 @@ private function init()
536538
$owner = $this->getOwner();
537539

538540
// (1) Create LDAP PI group
539-
$ldapPiGroupEntry = $this->getLDAPPiGroup();
541+
$ldapPiGroupEntry = $this->entry;
540542

541543
if (!$ldapPiGroupEntry->exists()) {
542544
$nextGID = $this->LDAP->getNextPiGIDNumber($this->SQL);
@@ -555,7 +557,7 @@ private function init()
555557
private function addUserToGroup($new_user)
556558
{
557559
// Add to LDAP Group
558-
$pi_group = $this->getLDAPPiGroup();
560+
$pi_group = $this->entry;
559561
$pi_group->appendAttribute("memberuid", $new_user->getUID());
560562
$pi_group->write();
561563
$this->REDIS->appendCacheArray($this->getPIUID(), "members", $new_user->getUID());
@@ -565,7 +567,7 @@ private function addUserToGroup($new_user)
565567
private function removeUserFromGroup($old_user)
566568
{
567569
// Remove from LDAP Group
568-
$pi_group = $this->getLDAPPiGroup();
570+
$pi_group = $this->entry;
569571
$pi_group->removeAttributeEntryByValue("memberuid", $old_user->getUID());
570572
$pi_group->write();
571573
$this->REDIS->removeCacheArray($this->getPIUID(), "members", $old_user->getUID());
@@ -598,11 +600,6 @@ public function getOwner()
598600
);
599601
}
600602

601-
public function getLDAPPiGroup()
602-
{
603-
return $this->LDAP->getPIGroupEntry($this->pi_uid);
604-
}
605-
606603
public static function getPIUIDfromUID($uid)
607604
{
608605
return self::PI_PREFIX . $uid;

resources/lib/UnityUser.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class UnityUser
1010
private const HOME_DIR = "/home/";
1111

1212
private $uid;
13+
private $entry;
1314

1415
// service stack
1516
private $LDAP;
@@ -21,6 +22,7 @@ class UnityUser
2122
public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
2223
{
2324
$this->uid = $uid;
25+
$this->entry = $LDAP->getUserEntry($uid);
2426

2527
$this->LDAP = $LDAP;
2628
$this->SQL = $SQL;
@@ -70,7 +72,7 @@ public function init($firstname, $lastname, $email, $org, $send_mail = true)
7072
//
7173
// Create LDAP user
7274
//
73-
$ldapUserEntry = $this->getLDAPUser();
75+
$ldapUserEntry = $this->entry;
7476

7577
if (!$ldapUserEntry->exists()) {
7678
$ldapUserEntry->setAttribute("objectclass", UnityLDAP::POSIX_ACCOUNT_CLASS);
@@ -141,16 +143,6 @@ public function init($firstname, $lastname, $email, $org, $send_mail = true)
141143
}
142144
}
143145

144-
/**
145-
* Returns the ldap account entry corresponding to the user
146-
*
147-
* @return ldapEntry posix account
148-
*/
149-
public function getLDAPUser()
150-
{
151-
return $this->LDAP->getUserEntry($this->uid);
152-
}
153-
154146
/**
155147
* Returns the ldap group entry corresponding to the user
156148
*
@@ -163,7 +155,7 @@ public function getLDAPGroup()
163155

164156
public function exists()
165157
{
166-
return $this->getLDAPUser()->exists() && $this->getLDAPGroup()->exists();
158+
return $this->entry->exists() && $this->getLDAPGroup()->exists();
167159
}
168160

169161
//
@@ -182,7 +174,7 @@ public function getUID()
182174

183175
public function setOrg($org)
184176
{
185-
$ldap_user = $this->getLDAPUser();
177+
$ldap_user = $this->entry;
186178
$ldap_user->setAttribute("o", $org);
187179
$ldap_user->write();
188180
$this->REDIS->setCache($this->uid, "org", $org);
@@ -199,13 +191,13 @@ public function getOrg($ignorecache = false)
199191
}
200192

201193
if ($this->exists()) {
202-
$org = $this->getLDAPUser()->getAttribute("o")[0];
194+
$org = $this->entry->getAttribute("o")[0];
203195

204196
if (!$ignorecache) {
205197
$this->REDIS->setCache($this->getUID(), "org", $org);
206198
}
207199

208-
return $this->getLDAPUser()->getAttribute("o")[0];
200+
return $this->entry->getAttribute("o")[0];
209201
}
210202

211203
return null;
@@ -218,7 +210,7 @@ public function getOrg($ignorecache = false)
218210
*/
219211
public function setFirstname($firstname, $operator = null)
220212
{
221-
$ldap_user = $this->getLDAPUser();
213+
$ldap_user = $this->entry;
222214
$ldap_user->setAttribute("givenname", $firstname);
223215
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
224216

@@ -249,7 +241,7 @@ public function getFirstname($ignorecache = false)
249241
}
250242

251243
if ($this->exists()) {
252-
$firstname = $this->getLDAPUser()->getAttribute("givenname")[0];
244+
$firstname = $this->entry->getAttribute("givenname")[0];
253245

254246
if (!$ignorecache) {
255247
$this->REDIS->setCache($this->getUID(), "firstname", $firstname);
@@ -268,7 +260,7 @@ public function getFirstname($ignorecache = false)
268260
*/
269261
public function setLastname($lastname, $operator = null)
270262
{
271-
$ldap_user = $this->getLDAPUser();
263+
$ldap_user = $this->entry;
272264
$ldap_user->setAttribute("sn", $lastname);
273265
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
274266

@@ -279,7 +271,7 @@ public function setLastname($lastname, $operator = null)
279271
$this->getUID()
280272
);
281273

282-
$this->getLDAPUser()->write();
274+
$this->entry->write();
283275
$this->REDIS->setCache($this->uid, "lastname", $lastname);
284276
}
285277

@@ -299,7 +291,7 @@ public function getLastname($ignorecache = false)
299291
}
300292

301293
if ($this->exists()) {
302-
$lastname = $this->getLDAPUser()->getAttribute("sn")[0];
294+
$lastname = $this->entry->getAttribute("sn")[0];
303295

304296
if (!$ignorecache) {
305297
$this->REDIS->setCache($this->getUID(), "lastname", $lastname);
@@ -324,7 +316,7 @@ public function getFullname()
324316
*/
325317
public function setMail($email, $operator = null)
326318
{
327-
$ldap_user = $this->getLDAPUser();
319+
$ldap_user = $this->entry;
328320
$ldap_user->setAttribute("mail", $email);
329321
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
330322

@@ -335,7 +327,7 @@ public function setMail($email, $operator = null)
335327
$this->getUID()
336328
);
337329

338-
$this->getLDAPUser()->write();
330+
$this->entry->write();
339331
$this->REDIS->setCache($this->uid, "mail", $email);
340332
}
341333

@@ -355,7 +347,7 @@ public function getMail($ignorecache = false)
355347
}
356348

357349
if ($this->exists()) {
358-
$mail = $this->getLDAPUser()->getAttribute("mail")[0];
350+
$mail = $this->entry->getAttribute("mail")[0];
359351

360352
if (!$ignorecache) {
361353
$this->REDIS->setCache($this->getUID(), "mail", $mail);
@@ -374,7 +366,7 @@ public function getMail($ignorecache = false)
374366
*/
375367
public function setSSHKeys($keys, $operator = null, $send_mail = true)
376368
{
377-
$ldapUser = $this->getLDAPUser();
369+
$ldapUser = $this->entry;
378370
$operator = is_null($operator) ? $this->getUID() : $operator->getUID();
379371
$keys_filt = array_values(array_unique($keys));
380372
if ($ldapUser->exists()) {
@@ -419,7 +411,7 @@ public function getSSHKeys($ignorecache = false)
419411
}
420412

421413
if ($this->exists()) {
422-
$ldapUser = $this->getLDAPUser();
414+
$ldapUser = $this->entry;
423415
$result = $ldapUser->getAttribute("sshpublickey");
424416
if (is_null($result)) {
425417
$keys = array();
@@ -454,7 +446,7 @@ public function setLoginShell($shell, $operator = null, $send_mail = true)
454446
if (empty($shell)) {
455447
throw new Exception("login shell must not be empty!");
456448
}
457-
$ldapUser = $this->getLDAPUser();
449+
$ldapUser = $this->entry;
458450
if ($ldapUser->exists()) {
459451
$ldapUser->setAttribute("loginshell", $shell);
460452
$ldapUser->write();
@@ -496,7 +488,7 @@ public function getLoginShell($ignorecache = false)
496488
}
497489

498490
if ($this->exists()) {
499-
$ldapUser = $this->getLDAPUser();
491+
$ldapUser = $this->entry;
500492

501493
$loginshell = $ldapUser->getAttribute("loginshell")[0];
502494

@@ -512,7 +504,7 @@ public function getLoginShell($ignorecache = false)
512504

513505
public function setHomeDir($home, $operator = null)
514506
{
515-
$ldapUser = $this->getLDAPUser();
507+
$ldapUser = $this->entry;
516508
if ($ldapUser->exists()) {
517509
$ldapUser->setAttribute("homedirectory", $home);
518510
$ldapUser->write();
@@ -545,7 +537,7 @@ public function getHomeDir($ignorecache = false)
545537
}
546538

547539
if ($this->exists()) {
548-
$ldapUser = $this->getLDAPUser();
540+
$ldapUser = $this->entry;
549541

550542
$homedir = $ldapUser->getAttribute("homedirectory");
551543

test/functional/NewUserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function ensureUserDoesNotExist()
6565
$org->removeUser($USER);
6666
assert(!$org->inOrg($USER));
6767
}
68-
$USER->getLDAPUser()->delete();
68+
$LDAP->getUserEntry($USER->getUID())->delete();
6969
assert(!$USER->exists());
7070
}
7171
$all_users_group = $LDAP->getUserGroup();
@@ -102,9 +102,9 @@ private function ensureUserNotInPIGroup(UnityGroup $pi_group)
102102

103103
private function ensurePIGroupDoesNotExist()
104104
{
105-
global $USER;
105+
global $USER, $LDAP;
106106
if ($USER->getPIGroup()->exists()) {
107-
$USER->getPIGroup()->getLDAPPIGroup()->delete();
107+
$LDAP->getPIGroupEntry($USER->getPIGroup()->getPIUID())->delete();
108108
assert(!$USER->getPIGroup()->exists());
109109
}
110110
}

0 commit comments

Comments
 (0)