Skip to content

Commit b9c43a7

Browse files
committed
optimize fetching of group member attributes
1 parent 04cdca0 commit b9c43a7

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

resources/lib/UnityGroup.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,4 +470,13 @@ public static function ownerMail2GID(string $email): string
470470
$ownerUid = $entry->getAttribute("cn")[0];
471471
return self::PI_PREFIX . $ownerUid;
472472
}
473+
474+
public function getGroupMembersAttributes(array $attributes, array $default_values = [])
475+
{
476+
return $this->LDAP->getUsersAttributes(
477+
$this->getGroupMemberUIDs(),
478+
$attributes,
479+
$default_values,
480+
);
481+
}
473482
}

resources/lib/UnityLDAP.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace UnityWebPortal\lib;
44

5+
use UnityWebPortal\lib\exceptions\EntryNotFoundException;
6+
use ValueError;
57
use PHPOpenLDAPer\LDAPConn;
68
use PHPOpenLDAPer\LDAPEntry;
79

@@ -487,4 +489,50 @@ public function getSortedGroupsForRedis(): array
487489
sort($groups);
488490
return $groups;
489491
}
492+
493+
/**
494+
* returns an array with each UID as an array key
495+
* @throws \UnityWebPortal\lib\exceptions\EntryNotFoundException
496+
*/
497+
public function getUsersAttributes(
498+
array $uids,
499+
array $attributes,
500+
array $default_values = [],
501+
): array {
502+
if (count($uids) === 0) {
503+
throw new ValueError("uids cannot be empty");
504+
}
505+
$attributes = array_map("strtolower", $attributes);
506+
if (in_array("uid", $attributes)) {
507+
$asked_for_uid_attribute = true;
508+
} else {
509+
$asked_for_uid_attribute = false;
510+
array_push($attributes, "uid");
511+
}
512+
$uids = array_map(fn($x) => ldap_escape($x, "", LDAP_ESCAPE_FILTER), $uids);
513+
$filter =
514+
"(&(objectClass=posixAccount)(|" .
515+
implode("", array_map(fn($x) => "(uid=$x)", $uids)) .
516+
"))";
517+
$entries = $this->baseOU->getChildrenArrayStrict(
518+
$attributes,
519+
true,
520+
$filter,
521+
$default_values,
522+
);
523+
$output = [];
524+
foreach ($entries as $entry) {
525+
$uid = $entry["uid"][0];
526+
if (!$asked_for_uid_attribute) {
527+
unset($entry["uid"]);
528+
}
529+
$output[$uid] = $entry;
530+
}
531+
$uids_not_found = array_diff($uids, array_keys($output));
532+
if (count($uids_not_found) > 0) {
533+
throw new EntryNotFoundException(jsonEncode($uids_not_found));
534+
}
535+
ksort($output);
536+
return $output;
537+
}
490538
}

webroot/admin/ajax/get_group_members.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
}
1515

1616
$group = new UnityGroup($_GET["gid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
17-
$members = $group->getGroupMembers();
17+
$members = $group->getGroupMembersAttributes(["gecos", "mail"]);
1818
$requests = $group->getRequests();
1919

2020
$i = 0;
2121
$count = count($members) + count($requests);
22-
foreach ($members as $member) {
23-
if ($member->uid == $group->getOwner()->uid) {
22+
foreach ($members as $uid => $attributes) {
23+
if ($uid == $group->getOwner()->uid) {
2424
continue;
2525
}
2626

@@ -29,22 +29,23 @@
2929
} else {
3030
echo "<tr class='expanded $i'>";
3131
}
32-
33-
echo "<td>" . $member->getFullname() . "</td>";
34-
echo "<td>" . $member->uid . "</td>";
35-
echo "<td><a href='mailto:" . $member->getMail() . "'>" . $member->getMail() . "</a></td>";
32+
$fullname = $attributes["gecos"][0];
33+
$mail = $attributes["mail"][0];
34+
echo "<td>$fullname</td>";
35+
echo "<td>$uid</td>";
36+
echo "<td><a href='mailto:$mail'>$mail</a></td>";
3637
echo "<td>";
3738
echo "
3839
<form
3940
action=''
4041
method='POST'
4142
onsubmit='
42-
return confirm(\"Are you sure you want to remove $member->uid from this group?\");
43+
return confirm(\"Are you sure you want to remove $uid from this group?\");
4344
'
4445
>
4546
<input type='hidden' name='form_type' value='remUserChild'>
46-
<input type='hidden' name='uid' value='" . $member->uid . "'>
47-
<input type='hidden' name='pi' value='" . $group->gid . "'>
47+
<input type='hidden' name='uid' value='$uid'>
48+
<input type='hidden' name='pi' value='$group->gid'>
4849
<input type='submit' value='Remove'>
4950
</form>
5051
";

webroot/panel/ajax/get_group_members.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
if (!$group->memberExists($USER)) {
1414
UnityHTTPD::forbidden("not a group member");
1515
}
16-
$members = $group->getGroupMembers();
16+
$members = $group->getGroupMembersAttributes(["gecos", "mail"]);
1717
$count = count($members);
18-
foreach ($members as $key => $member) {
19-
if ($member->uid == $group->getOwner()->uid) {
18+
foreach ($members as $uid => $attributes) {
19+
if ($uid == $group->getOwner()->uid) {
2020
continue;
2121
}
2222

@@ -25,10 +25,11 @@
2525
} else {
2626
echo "<tr class='expanded $key'>";
2727
}
28-
29-
echo "<td>" . $member->getFullname() . "</td>";
30-
echo "<td>" . $member->uid . "</td>";
31-
echo "<td><a href='mailto:" . $member->getMail() . "'>" . $member->getMail() . "</a></td>";
32-
echo "<td><input type='hidden' name='uid' value='" . $member->uid . "'></td>";
28+
$fullname = $attributes["gecos"][0];
29+
$mail = $attributes["mail"][0];
30+
echo "<td>$fullname</td>";
31+
echo "<td>$uid</td>";
32+
echo "<td><a href='mailto:$mail'>$mail</a></td>";
33+
echo "<td><input type='hidden' name='uid' value='$uid'></td>";
3334
echo "</tr>";
3435
}

0 commit comments

Comments
 (0)