Skip to content

Commit e9284aa

Browse files
authored
Cleanup (#267)
* store entry as property * don't store extra variable for $this->entry * UnityOrg as well * fixes from uid/gid change * rename getLDAPGroup to getGroupEntry
1 parent 1e05710 commit e9284aa

File tree

4 files changed

+75
-115
lines changed

4 files changed

+75
-115
lines changed

resources/lib/UnityGroup.php

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

1414
public $gid;
15+
private $entry;
1516

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

3436
$this->LDAP = $LDAP;
3537
$this->SQL = $SQL;
@@ -59,7 +61,7 @@ public function __toString()
5961
*/
6062
public function exists()
6163
{
62-
return $this->getLDAPPiGroup()->exists();
64+
return $this->entry->exists();
6365
}
6466

6567
//
@@ -255,9 +257,8 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
255257
// $users = $this->getGroupMembers();
256258

257259
// // now we delete the ldap entry
258-
// $ldapPiGroupEntry = $this->getLDAPPiGroup();
259-
// if ($ldapPiGroupEntry->exists()) {
260-
// $ldapPiGroupEntry->delete();
260+
// if ($this->entry->exists()) {
261+
// $this->entry->delete();
261262
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->gid);
262263
// foreach ($users as $user) {
263264
// $this->REDIS->removeCacheArray($user->uid, "groups", $this->gid);
@@ -486,8 +487,7 @@ public function getGroupMemberUIDs($ignorecache = false)
486487
}
487488
$updatecache = false;
488489
if (!isset($members)) {
489-
$pi_group = $this->getLDAPPiGroup();
490-
$members = $pi_group->getAttribute("memberuid");
490+
$members = $this->entry->getAttribute("memberuid");
491491
$updatecache = true;
492492
}
493493
if (!$ignorecache && $updatecache) {
@@ -520,16 +520,13 @@ private function init()
520520
// make this user a PI
521521
$owner = $this->getOwner();
522522

523-
// (1) Create LDAP PI group
524-
$ldapPiGroupEntry = $this->getLDAPPiGroup();
525-
526-
if (!$ldapPiGroupEntry->exists()) {
523+
if (!$this->entry->exists()) {
527524
$nextGID = $this->LDAP->getNextPiGIDNumber($this->SQL);
528525

529-
$ldapPiGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
530-
$ldapPiGroupEntry->setAttribute("gidnumber", strval($nextGID));
531-
$ldapPiGroupEntry->setAttribute("memberuid", array($owner->uid));
532-
$ldapPiGroupEntry->write();
526+
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
527+
$this->entry->setAttribute("gidnumber", strval($nextGID));
528+
$this->entry->setAttribute("memberuid", array($owner->uid));
529+
$this->entry->write();
533530
}
534531

535532
$this->REDIS->appendCacheArray("sorted_groups", "", $this->gid);
@@ -540,19 +537,17 @@ private function init()
540537
private function addUserToGroup($new_user)
541538
{
542539
// Add to LDAP Group
543-
$pi_group = $this->getLDAPPiGroup();
544-
$pi_group->appendAttribute("memberuid", $new_user->uid);
545-
$pi_group->write();
540+
$this->entry->appendAttribute("memberuid", $new_user->uid);
541+
$this->entry->write();
546542
$this->REDIS->appendCacheArray($this->gid, "members", $new_user->uid);
547543
$this->REDIS->appendCacheArray($new_user->uid, "groups", $this->gid);
548544
}
549545

550546
private function removeUserFromGroup($old_user)
551547
{
552548
// Remove from LDAP Group
553-
$pi_group = $this->getLDAPPiGroup();
554-
$pi_group->removeAttributeEntryByValue("memberuid", $old_user->uid);
555-
$pi_group->write();
549+
$this->entry->removeAttributeEntryByValue("memberuid", $old_user->uid);
550+
$this->entry->write();
556551
$this->REDIS->removeCacheArray($this->gid, "members", $old_user->uid);
557552
$this->REDIS->removeCacheArray($old_user->uid, "groups", $this->gid);
558553
}
@@ -583,11 +578,6 @@ public function getOwner()
583578
);
584579
}
585580

586-
public function getLDAPPiGroup()
587-
{
588-
return $this->LDAP->getPIGroupEntry($this->gid);
589-
}
590-
591581
public static function ownerUID2GID($uid)
592582
{
593583
return self::PI_PREFIX . $uid;

resources/lib/UnityOrg.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class UnityOrg
88
{
99
public $gid;
10+
private $entry;
1011

1112
private $MAILER;
1213
private $SQL;
@@ -17,6 +18,7 @@ class UnityOrg
1718
public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
1819
{
1920
$this->gid = $gid;
21+
$this->entry = $LDAP->getOrgGroupEntry($this->gid);
2022

2123
$this->LDAP = $LDAP;
2224
$this->SQL = $SQL;
@@ -27,27 +29,20 @@ public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
2729

2830
public function init()
2931
{
30-
$org_group = $this->getLDAPOrgGroup();
31-
32-
if (!$org_group->exists()) {
32+
if (!$this->entry->exists()) {
3333
$nextGID = $this->LDAP->getNextOrgGIDNumber($this->SQL);
3434

35-
$org_group->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
36-
$org_group->setAttribute("gidnumber", strval($nextGID));
37-
$org_group->write();
35+
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
36+
$this->entry->setAttribute("gidnumber", strval($nextGID));
37+
$this->entry->write();
3838
}
3939

4040
$this->REDIS->appendCacheArray("sorted_orgs", "", $this->gid);
4141
}
4242

4343
public function exists()
4444
{
45-
return $this->getLDAPOrgGroup()->exists();
46-
}
47-
48-
public function getLDAPOrgGroup()
49-
{
50-
return $this->LDAP->getOrgGroupEntry($this->gid);
45+
return $this->entry->exists();
5146
}
5247

5348
public function inOrg($user, $ignorecache = false)
@@ -83,8 +78,7 @@ public function getOrgMemberUIDs($ignorecache = false)
8378
}
8479
$updatecache = false;
8580
if (!isset($members)) {
86-
$org_group = $this->getLDAPOrgGroup();
87-
$members = $org_group->getAttribute("memberuid");
81+
$members = $this->entry->getAttribute("memberuid");
8882
$updatecache = true;
8983
}
9084
if (!$ignorecache && $updatecache) {
@@ -96,17 +90,15 @@ public function getOrgMemberUIDs($ignorecache = false)
9690

9791
public function addUser($user)
9892
{
99-
$org_group = $this->getLDAPOrgGroup();
100-
$org_group->appendAttribute("memberuid", $user->uid);
101-
$org_group->write();
93+
$this->entry->appendAttribute("memberuid", $user->uid);
94+
$this->entry->write();
10295
$this->REDIS->appendCacheArray($this->gid, "members", $user->uid);
10396
}
10497

10598
public function removeUser($user)
10699
{
107-
$org_group = $this->getLDAPOrgGroup();
108-
$org_group->removeAttributeEntryByValue("memberuid", $user->uid);
109-
$org_group->write();
100+
$this->entry->removeAttributeEntryByValue("memberuid", $user->uid);
101+
$this->entry->write();
110102
$this->REDIS->removeCacheArray($this->gid, "members", $user->uid);
111103
}
112104
}

0 commit comments

Comments
 (0)