Skip to content

Commit 40259c4

Browse files
committed
Merge branch 'main' into prg
2 parents 01139fe + 4719bb4 commit 40259c4

File tree

15 files changed

+1772
-142
lines changed

15 files changed

+1772
-142
lines changed

resources/lib/UnityGroup.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,8 @@ public function getRequests()
460460

461461
public function getGroupMembers($ignorecache = false)
462462
{
463-
if (!$ignorecache) {
464-
$cached_val = $this->REDIS->getCache($this->getPIUID(), "members");
465-
if (!is_null($cached_val)) {
466-
$members = $cached_val;
467-
}
468-
}
469-
470-
$updatecache = false;
471-
if (!isset($members)) {
472-
$pi_group = $this->getLDAPPiGroup();
473-
$members = $pi_group->getAttribute("memberuid");
474-
$updatecache = true;
475-
}
476-
463+
$members = $this->getGroupMemberUIDs($ignorecache);
477464
$out = array();
478-
$cache_arr = array();
479465
$owner_uid = $this->getOwner()->getUID();
480466
foreach ($members as $member) {
481467
$user_obj = new UnityUser(
@@ -487,22 +473,28 @@ public function getGroupMembers($ignorecache = false)
487473
$this->WEBHOOK
488474
);
489475
array_push($out, $user_obj);
490-
array_push($cache_arr, $user_obj->getUID());
491-
}
492-
493-
if (!$ignorecache && $updatecache) {
494-
sort($cache_arr);
495-
$this->REDIS->setCache($this->getPIUID(), "members", $cache_arr);
496476
}
497-
498477
return $out;
499478
}
500479

501-
public function getGroupMemberUIDs()
480+
public function getGroupMemberUIDs($ignorecache = false)
502481
{
503-
$pi_group = $this->getLDAPPiGroup();
504-
$members = $pi_group->getAttribute("memberuid");
505-
482+
if (!$ignorecache) {
483+
$cached_val = $this->REDIS->getCache($this->getPIUID(), "members");
484+
if (!is_null($cached_val)) {
485+
$members = $cached_val;
486+
}
487+
}
488+
$updatecache = false;
489+
if (!isset($members)) {
490+
$pi_group = $this->getLDAPPiGroup();
491+
$members = $pi_group->getAttribute("memberuid");
492+
$updatecache = true;
493+
}
494+
if (!$ignorecache && $updatecache) {
495+
sort($members);
496+
$this->REDIS->setCache($this->getPIUID(), "members", $members);
497+
}
506498
return $members;
507499
}
508500

resources/lib/UnityLDAP.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function getNextUIDNumber($UnitySQL)
124124
$max_uid = $UnitySQL->getSiteVar('MAX_UID');
125125
$new_uid = $max_uid + 1;
126126

127-
while ($this->UIDNumInUse($new_uid)) {
127+
while ($this->IDNumInUse($new_uid)) {
128128
$new_uid++;
129129
}
130130

@@ -138,7 +138,7 @@ public function getNextPiGIDNumber($UnitySQL)
138138
$max_pigid = $UnitySQL->getSiteVar('MAX_PIGID');
139139
$new_pigid = $max_pigid + 1;
140140

141-
while ($this->PIGIDNumInUse($new_pigid)) {
141+
while ($this->IDNumInUse($new_pigid)) {
142142
$new_pigid++;
143143
}
144144

@@ -152,7 +152,7 @@ public function getNextOrgGIDNumber($UnitySQL)
152152
$max_gid = $UnitySQL->getSiteVar('MAX_GID');
153153
$new_gid = $max_gid + 1;
154154

155-
while ($this->GIDNumInUse($new_gid)) {
155+
while ($this->IDNumInUse($new_gid)) {
156156
$new_gid++;
157157
}
158158

@@ -161,32 +161,24 @@ public function getNextOrgGIDNumber($UnitySQL)
161161
return $new_gid;
162162
}
163163

164-
private function UIDNumInUse($id)
164+
private function IDNumInUse($id)
165165
{
166+
// id reserved for debian packages
167+
if (($id >= 100 && $id <= 999) || ($id >= 60000 && $id <= 64999)) {
168+
return true;
169+
}
166170
$users = $this->userOU->getChildrenArray(true);
167171
foreach ($users as $user) {
168172
if ($user["uidnumber"][0] == $id) {
169173
return true;
170174
}
171175
}
172-
173-
return false;
174-
}
175-
176-
private function PIGIDNumInUse($id)
177-
{
178176
$pi_groups = $this->pi_groupOU->getChildrenArray(true);
179177
foreach ($pi_groups as $pi_group) {
180178
if ($pi_group["gidnumber"][0] == $id) {
181179
return true;
182180
}
183181
}
184-
185-
return false;
186-
}
187-
188-
private function GIDNumInUse($id)
189-
{
190182
$groups = $this->groupOU->getChildrenArray(true);
191183
foreach ($groups as $group) {
192184
if ($group["gidnumber"][0] == $id) {
@@ -212,7 +204,7 @@ public function getUnassignedID($uid, $UnitySQL)
212204

213205
if ($uid == $netid_match || $netid == $netid_match) {
214206
// found a match
215-
if (!$this->UIDNumInUse($uid_match) && !$this->GIDNumInUse($uid_match)) {
207+
if (!$this->IDNumInUse($uid_match)) {
216208
return $uid_match;
217209
}
218210
}
@@ -340,4 +332,35 @@ public function getOrgGroupEntry($gid)
340332
$gid = ldap_escape($gid, LDAP_ESCAPE_DN);
341333
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
342334
}
335+
336+
public static function parseUserChildrenArray(array $userChildrenArray): array
337+
{
338+
// input comes from LdapEntry::getChildrenArray on a UnityUser
339+
$output = [];
340+
$required_string_attributes = [
341+
"gidnumber",
342+
"givenname",
343+
"homedirectory",
344+
"loginshell",
345+
"mail",
346+
"o",
347+
"sn",
348+
"uid",
349+
"uidnumber",
350+
"gecos",
351+
];
352+
foreach ($required_string_attributes as $key) {
353+
$output[$key] = $userChildrenArray[$key][0];
354+
}
355+
$output["firstname"] = $output["givenname"];
356+
$output["lastname"] = $output["sn"];
357+
$output["org"] = $output["o"];
358+
$output["objectclass"] = $userChildrenArray["objectclass"];
359+
if (array_key_exists("sspublickey", $userChildrenArray)) {
360+
$output["sshpublickey"] = $userChildrenArray["sshpublickey"];
361+
} else {
362+
$output["sshpublickey"] = [];
363+
}
364+
return $output;
365+
}
343366
}

resources/lib/UnityRedis.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ public function removeCacheArray($object, $key, $value)
9595
$this->setCache($object, $key, $cached_val);
9696
}
9797
}
98+
99+
public function flushAll()
100+
{
101+
$this->client->flushAll();
102+
}
98103
}

resources/lib/UnitySite.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class UnitySite
99
{
10-
public static function die($x = null)
10+
public static function die($x = null, $show_user = false)
1111
{
1212
if (@$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] == true) {
1313
if (is_null($x)) {
@@ -16,18 +16,18 @@ public static function die($x = null)
1616
throw new PhpUnitNoDieException($x);
1717
}
1818
} else {
19-
if (is_null($x)) {
20-
die();
21-
} else {
19+
if (!is_null($x) and $show_user) {
2220
die($x);
21+
} else {
22+
die();
2323
}
2424
}
2525
}
2626

2727
public static function redirect($destination)
2828
{
2929
header("Location: $destination");
30-
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.");
30+
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.", true);
3131
}
3232

3333
private static function headerResponseCode(int $code, string $reason)
@@ -55,14 +55,14 @@ public static function badRequest($message)
5555
{
5656
self::headerResponseCode(400, "bad request");
5757
self::errorLog("bad request", $message);
58-
self::die();
58+
self::die($message);
5959
}
6060

6161
public static function forbidden($message)
6262
{
6363
self::headerResponseCode(403, "forbidden");
6464
self::errorLog("forbidden", $message);
65-
self::die();
65+
self::die($message);
6666
}
6767

6868
public static function arrayGetOrBadRequest(array $array, ...$keys)

0 commit comments

Comments
 (0)