Skip to content

Commit 34b2135

Browse files
remove cache from big pages (#266)
* 1st attempt * replace LDAPConn->search with LDAPEntry->search * unityLDAP store base DN / base OU * replace/rewrite getGroups * fix bugs * remove cache from pi-mgmt * fix bug * more readable * specific escape * remove extra variable * comment * shorter * lowercase * list -> array * query only for attributes that we need * style * "entry" is the wrong word * fix missing return * rename "arrays" to "attributes", make attribute getters consistent * no need for recursive * no need for recursive * remove default value for attributes * getuid is gone * remove multiple calls to getPIGroupGIDs * fix bug? * typo Co-authored-by: Copilot <[email protected]> * remove newline Co-authored-by: Copilot <[email protected]> * function name has changed * use nested table instead of <br> --------- Co-authored-by: Copilot <[email protected]>
1 parent 8550c49 commit 34b2135

File tree

8 files changed

+168
-130
lines changed

8 files changed

+168
-130
lines changed

resources/init.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
$CONFIG["ldap"]["user"],
4646
$CONFIG["ldap"]["pass"],
4747
__DIR__ . "/../deployment/custom_user_mappings",
48+
$CONFIG["ldap"]["basedn"],
4849
$CONFIG["ldap"]["user_ou"],
4950
$CONFIG["ldap"]["group_ou"],
5051
$CONFIG["ldap"]["pigroup_ou"],

resources/lib/UnityLDAP.php

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ class UnityLDAP extends ldapConn
2626
);
2727

2828
// string vars for OUs
29+
private $STR_BASEOU;
2930
private $STR_USEROU;
3031
private $STR_GROUPOU;
3132
private $STR_PIGROUPOU;
3233
private $STR_ORGGROUPOU;
3334
private $STR_ADMINGROUP;
3435

3536
// Instance vars for various ldapEntry objects
37+
private $baseOU;
3638
private $userOU;
3739
private $groupOU;
3840
private $pi_groupOU;
@@ -49,6 +51,7 @@ public function __construct(
4951
$dn,
5052
$pass,
5153
$custom_user_mappings,
54+
$base_ou,
5255
$user_ou,
5356
$group_ou,
5457
$pigroup_ou,
@@ -59,13 +62,15 @@ public function __construct(
5962
) {
6063
parent::__construct($host, $dn, $pass);
6164

65+
$this->STR_BASEOU = $base_ou;
6266
$this->STR_USEROU = $user_ou;
6367
$this->STR_GROUPOU = $group_ou;
6468
$this->STR_PIGROUPOU = $pigroup_ou;
6569
$this->STR_ORGGROUPOU = $orggroup_ou;
6670
$this->STR_ADMINGROUP = $admin_group;
6771

6872
// Get Global Entries
73+
$this->baseOU = $this->getEntry($base_ou);
6974
$this->userOU = $this->getEntry($user_ou);
7075
$this->groupOU = $this->getEntry($group_ou);
7176
$this->pi_groupOU = $this->getEntry($pigroup_ou);
@@ -219,6 +224,13 @@ public function getUnassignedID($uid, $UnitySQL)
219224
return $next_uid;
220225
}
221226

227+
public function getAllUsersUIDs()
228+
{
229+
// should not use $user_ou->getChildren or $base_ou->getChildren(objectClass=posixAccount)
230+
// Unity users might be outside user ou, and not all users in LDAP tree are unity users
231+
return $this->userGroup->getAttribute("memberuid");
232+
}
233+
222234
//
223235
// Functions that return user/group objects
224236
//
@@ -232,21 +244,35 @@ public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook,
232244
foreach ($users as $user) {
233245
array_push($out, new UnityUser($user, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook));
234246
}
235-
236247
return $out;
237248
}
238249
}
239250

240-
$users = $this->userGroup->getAttribute("memberuid");
251+
$users = $this->getAllUsersUIDs();
241252
sort($users);
242253
foreach ($users as $user) {
243254
$params = array($user, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook);
244255
array_push($out, new UnityUser(...$params));
245256
}
246-
247257
return $out;
248258
}
249259

260+
public function getAllUsersAttributes($attributes)
261+
{
262+
$include_uids = $this->getAllUsersUIDs();
263+
$user_attributes = $this->baseOU->getChildrenArray(
264+
$attributes,
265+
true, // recursive
266+
"objectClass=posixAccount"
267+
);
268+
foreach ($user_attributes as $i => $attributes) {
269+
if (!in_array($attributes["uid"][0], $include_uids)) {
270+
unset($user_attributes[$i]);
271+
}
272+
}
273+
return $user_attributes;
274+
}
275+
250276
public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
251277
{
252278
$out = array();
@@ -279,6 +305,58 @@ public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebho
279305
return $out;
280306
}
281307

308+
public function getAllPIGroupsAttributes($attributes)
309+
{
310+
return $this->pi_groupOU->getChildrenArray($attributes);
311+
}
312+
313+
public function getPIGroupGIDsWithMemberUID($uid)
314+
{
315+
return array_map(
316+
fn($x) => $x["cn"][0],
317+
$this->pi_groupOU->getChildrenArray(
318+
["cn"],
319+
false,
320+
"(memberuid=" . ldap_escape($uid, LDAP_ESCAPE_FILTER) . ")",
321+
)
322+
);
323+
}
324+
325+
public function getAllPIGroupOwnerAttributes($attributes)
326+
{
327+
// get the PI groups, filter for just the GIDs, then map the GIDs to owner UIDs
328+
$owner_uids = array_map(
329+
fn($x) => UnityGroup::GID2OwnerUID($x),
330+
array_map(
331+
fn($x) => $x["cn"][0],
332+
$this->pi_groupOU->getChildrenArray(["cn"]),
333+
),
334+
);
335+
$owner_attributes = $this->getAllUsersAttributes($attributes);
336+
foreach ($owner_attributes as $i => $attributes) {
337+
if (!in_array($attributes["uid"][0], $owner_uids)) {
338+
unset($owner_attributes[$i]);
339+
}
340+
}
341+
return $owner_attributes;
342+
}
343+
344+
/** Returns an associative array where keys are UIDs and values are arrays of PI GIDs */
345+
public function getAllUID2PIGIDs()
346+
{
347+
// initialize output so each UID is a key with an empty array as its value
348+
$uids = $this->getAllUsersUIDs();
349+
$uid2pigids = array_combine($uids, array_fill(0, count($uids), []));
350+
// for each PI group, append that GID to the member list for each of its member UIDs
351+
foreach ($this->getAllPIGroupsAttributes(["cn", "memberuid"]) as $array) {
352+
$gid = $array["cn"][0];
353+
foreach ($array["memberuid"] as $uid) {
354+
array_push($uid2pigids[$uid], $gid);
355+
}
356+
}
357+
return $uid2pigids;
358+
}
359+
282360
public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
283361
{
284362
$out = array();
@@ -310,6 +388,11 @@ public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebh
310388
return $out;
311389
}
312390

391+
public function getAllOrgGroupsAttributes($attributes)
392+
{
393+
return $this->org_groupOU->getChildrenArray($attributes);
394+
}
395+
313396
public function getUserEntry($uid)
314397
{
315398
$uid = ldap_escape($uid, "", LDAP_ESCAPE_DN);

resources/lib/UnityUser.php

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -575,48 +575,21 @@ public function getOrgGroup()
575575

576576
/**
577577
* Gets the groups this user is assigned to, can be more than one
578-
* @return [type]
578+
* @return string[]
579579
*/
580-
public function getGroups($ignorecache = false)
580+
public function getPIGroupGIDs($ignorecache = false)
581581
{
582-
$out = array();
583-
584582
if (!$ignorecache) {
585583
$cached_val = $this->REDIS->getCache($this->uid, "groups");
586584
if (!is_null($cached_val)) {
587-
$groups = $cached_val;
588-
foreach ($groups as $group) {
589-
$group_obj = new UnityGroup(
590-
$group,
591-
$this->LDAP,
592-
$this->SQL,
593-
$this->MAILER,
594-
$this->REDIS,
595-
$this->WEBHOOK
596-
);
597-
array_push($out, $group_obj);
598-
}
599-
600-
return $out;
601-
}
602-
}
603-
604-
$all_pi_groups = $this->LDAP->getAllPIGroups($this->SQL, $this->MAILER, $this->REDIS, $ignorecache);
605-
606-
$cache_arr = array();
607-
608-
foreach ($all_pi_groups as $pi_group) {
609-
if (in_array($this->uid, $pi_group->getGroupMemberUIDs())) {
610-
array_push($out, $pi_group);
611-
array_push($cache_arr, $pi_group->gid);
585+
return $cached_val;
612586
}
613587
}
614-
588+
$gids = $this->LDAP->getPIGroupGIDsWithMemberUID($this->uid);
615589
if (!$ignorecache) {
616-
$this->REDIS->setCache($this->uid, "groups", $cache_arr);
590+
$this->REDIS->setCache($this->uid, "groups", $gids);
617591
}
618-
619-
return $out;
592+
return $gids;
620593
}
621594

622595
/**

test/functional/AccountDeletionRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testRequestAccountDeletionUserHasNoGroups()
3535
{
3636
global $USER, $SQL;
3737
switchUser(...getUserHasNotRequestedAccountDeletionHasNoGroups());
38-
$this->assertEmpty($USER->getGroups());
38+
$this->assertEmpty($USER->getPIGroupGIDs());
3939
$this->assertNumberAccountDeletionRequests(0);
4040
try {
4141
http_post(
@@ -59,7 +59,7 @@ public function testRequestAccountDeletionUserHasGroup()
5959
// FIXME this should be an error
6060
global $USER, $SQL;
6161
switchUser(...getUserHasNotRequestedAccountDeletionHasGroup());
62-
$this->assertNotEmpty($USER->getGroups());
62+
$this->assertNotEmpty($USER->getPIGroupGIDs());
6363
$this->assertNumberAccountDeletionRequests(0);
6464
try {
6565
http_post(

webroot/admin/pi-mgmt.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,13 @@
103103
</tr>
104104

105105
<?php
106-
$accounts = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK);
107-
108-
usort($accounts, function ($a, $b) {
109-
return strcmp($a->gid, $b->gid);
110-
});
111-
112-
foreach ($accounts as $pi_group) {
113-
$pi_user = $pi_group->getOwner();
114-
106+
$owner_attributes = $LDAP->getAllPIGroupOwnerAttributes(["uid", "gecos", "mail"]);
107+
usort($owner_attributes, fn($a, $b) => strcmp($a["uid"][0], $b["uid"][0]));
108+
foreach ($owner_attributes as $attributes) {
115109
echo "<tr class='expandable'>";
116-
echo "<td><button class='btnExpand'>&#9654;</button>" . $pi_user->getFirstname() .
117-
" " . $pi_user->getLastname() . "</td>";
118-
echo "<td>" . $pi_group->gid . "</td>";
119-
echo "<td><a href='mailto:" . $pi_user->getMail() . "'>" . $pi_user->getMail() . "</a></td>";
110+
echo "<td><button class='btnExpand'>&#9654;</button>" . $attributes["gecos"][0] . "</td>";
111+
echo "<td>" . UnityGroup::OwnerUID2GID($attributes["uid"][0]) . "</td>";
112+
echo "<td><a href='mailto:" . $attributes["mail"][0] . "'>" . $attributes["mail"][0] . "</a></td>";
120113
echo "</tr>";
121114
}
122115
?>

webroot/admin/user-mgmt.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,34 @@
3737
</tr>
3838

3939
<?php
40-
$users = $LDAP->getAllUsers($SQL, $MAILER, $REDIS, $WEBHOOK);
41-
42-
usort($users, function ($a, $b) {
43-
return strcmp($a->uid, $b->uid);
44-
});
45-
46-
foreach ($users as $user) {
47-
if ($user->hasRequestedAccountDeletion()) {
40+
$UID2PIGIDs = $LDAP->getAllUID2PIGIDs();
41+
$user_attributes = $LDAP->getAllUsersAttributes(["uid", "gecos", "o", "mail"]);
42+
usort($user_attributes, fn ($a, $b) => strcmp($a["uid"][0], $b["uid"][0]));
43+
foreach ($user_attributes as $attributes) {
44+
$uid = $attributes["uid"][0];
45+
if ($SQL->accDeletionRequestExists($uid)) {
4846
echo "<tr style='color:grey; font-style: italic'>";
4947
} else {
5048
echo "<tr>";
5149
}
52-
echo "<td>" . $user->getFirstname() . " " . $user->getLastname() . "</td>";
53-
echo "<td>" . $user->uid . "</td>";
54-
echo "<td>" . $user->getOrg() . "</td>";
55-
echo "<td><a href='mailto:" . $user->getMail() . "'>" . $user->getMail() . "</a></td>";
50+
echo "<td>" . $attributes["gecos"][0] . "</td>";
51+
echo "<td>" . $uid . "</td>";
52+
echo "<td>" . $attributes["o"][0] . "</td>";
53+
echo "<td><a href='mailto:" . $attributes["mail"][0] . "'>" . $attributes["mail"][0] . "</a></td>";
5654
echo "<td>";
57-
$cur_user_groups = $user->getGroups();
58-
foreach ($cur_user_groups as $cur_group) {
59-
echo "<a href='mailto:" . $cur_group->getOwner()->getMail() . "'>" . $cur_group->gid . "</a>";
60-
if ($cur_group !== array_key_last($cur_user_groups)) {
61-
echo '<br>';
55+
if (count($UID2PIGIDs[$uid]) > 0) {
56+
echo "<table>";
57+
foreach ($UID2PIGIDs[$uid] as $gid) {
58+
echo "<tr><td>$gid</td></tr>";
6259
}
60+
echo "</table>";
6361
}
6462
echo "</td>";
6563
echo "<td>";
6664
echo "<form class='viewAsUserForm' action='' method='POST'
67-
onsubmit='return confirm(\"Are you sure you want to switch to the user " . $user->uid . "?\");'>
65+
onsubmit='return confirm(\"Are you sure you want to switch to the user '$uid'?\");'>
6866
<input type='hidden' name='form_type' value='viewAsUser'>
69-
<input type='hidden' name='uid' value='" . $user->uid . "'>
67+
<input type='hidden' name='uid' value='$uid'>
7068
<input type='submit' name='action' value='Access'>
7169
</form>";
7270
echo "</td>";

0 commit comments

Comments
 (0)