Skip to content

Commit ddad19d

Browse files
optimize fetching of group member attributes (#369)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 04cdca0 commit ddad19d

File tree

4 files changed

+81
-25
lines changed

4 files changed

+81
-25
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 = []): array
475+
{
476+
return $this->LDAP->getUsersAttributes(
477+
$this->getGroupMemberUIDs(),
478+
$attributes,
479+
$default_values,
480+
);
481+
}
473482
}

resources/lib/UnityLDAP.php

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

33
namespace UnityWebPortal\lib;
44

5+
use UnityWebPortal\lib\exceptions\EntryNotFoundException;
56
use PHPOpenLDAPer\LDAPConn;
67
use PHPOpenLDAPer\LDAPEntry;
78

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

webroot/admin/ajax/get_group_members.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,42 @@
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
}
26-
2726
if ($i >= $count - 1) {
2827
echo "<tr class='expanded $i last'>";
2928
} else {
3029
echo "<tr class='expanded $i'>";
3130
}
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>";
31+
$fullname = $attributes["gecos"][0];
32+
$mail = $attributes["mail"][0];
33+
echo "<td>$fullname</td>";
34+
echo "<td>$uid</td>";
35+
echo "<td><a href='mailto:$mail'>$mail</a></td>";
3636
echo "<td>";
3737
echo "
3838
<form
3939
action=''
4040
method='POST'
4141
onsubmit='
42-
return confirm(\"Are you sure you want to remove $member->uid from this group?\");
42+
return confirm(\"Are you sure you want to remove $uid from this group?\");
4343
'
4444
>
4545
<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 . "'>
46+
<input type='hidden' name='uid' value='$uid'>
47+
<input type='hidden' name='pi' value='$group->gid'>
4848
<input type='submit' value='Remove'>
4949
</form>
5050
";
5151
echo "</td>";
5252
echo "</tr>";
53-
5453
$i++;
5554
}
5655

@@ -76,6 +75,5 @@
7675
<input type='submit' name='action' value='Deny'></form>";
7776
echo "</td>";
7877
echo "</tr>";
79-
8078
$i++;
8179
}

webroot/panel/ajax/get_group_members.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@
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+
$i = 0;
19+
foreach ($members as $uid => $attributes) {
20+
if ($uid == $group->getOwner()->uid) {
2021
continue;
2122
}
22-
23-
if ($key >= $count - 1) {
24-
echo "<tr class='expanded $key last'>";
23+
if ($i >= $count - 1) {
24+
echo "<tr class='expanded $i last'>";
2525
} else {
26-
echo "<tr class='expanded $key'>";
26+
echo "<tr class='expanded $i'>";
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>";
35+
$i++;
3436
}

0 commit comments

Comments
 (0)