Skip to content

Commit 796ee0d

Browse files
authored
Fix redis keys (#241)
* add functions to build cache properly * pretty * fix update-ldap-cache.php * rearrange * fix user groups list * oops * edge case
1 parent 94e11a8 commit 796ee0d

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

workers/update-ldap-cache.php

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,6 @@
1414
use UnityWebPortal\lib\UnityWebhook;
1515
use PHPOpenLDAPer\LDAPEntry;
1616

17-
// in PHP LDAP all attributes are arrays, we need these as strings instead
18-
// it's possible but probably difficult to find this out using LDAP schema information
19-
$user_string_attributes = [
20-
"gidnumber",
21-
"givenname",
22-
"homedirectory",
23-
"loginshell",
24-
"mail",
25-
"o",
26-
"sn",
27-
"uid",
28-
"uidnumber",
29-
"gecos",
30-
];
31-
32-
$pi_group_string_attributes = [
33-
"gidnumber",
34-
];
35-
3617
$options = getopt("fuh", ["help"]);
3718
if (array_key_exists("h", $options) or array_key_exists("help", $options)) {
3819
echo "arguments:
@@ -51,40 +32,44 @@
5132
echo " use -f argument to flush cache, or -u argument to update without flush.\n";
5233
} else {
5334
echo "updating cache...\n";
54-
echo "waiting for LDAP response (users)...\n";
35+
36+
// search entire tree, some users created for admin purposes might not be in the normal OU
37+
echo "waiting for LDAP search (users)...\n";
5538
$users = $LDAP->search("objectClass=posixAccount", $CONFIG["ldap"]["basedn"]);
5639
echo "response received.\n";
57-
// phpcs:disable
58-
$user_CNs = array_map(function ($x){return $x->getAttribute("cn")[0];}, $users);
59-
// phpcs:enable
40+
$user_CNs = $LDAP->getUserGroup()->getAttribute("memberuid");
6041
sort($user_CNs);
6142
$REDIS->setCache("sorted_users", "", $user_CNs);
6243
foreach ($users as $user) {
63-
$cn = $user->getAttribute("cn")[0];
64-
foreach ($user->getAttributes() as $key => $val) {
65-
if (in_array($key, $user_string_attributes)) {
66-
$REDIS->setCache($cn, $key, $val[0]);
67-
} else {
68-
$REDIS->setCache($cn, $key, $val);
69-
}
44+
$uid = $user->getAttribute("cn")[0];
45+
if (!in_array($uid, $user_CNs)) {
46+
continue;
7047
}
48+
$REDIS->setCache($uid, "firstname", $user->getAttribute("givenname")[0]);
49+
$REDIS->setCache($uid, "lastname", $user->getAttribute("sn")[0]);
50+
$REDIS->setCache($uid, "org", $user->getAttribute("o")[0]);
51+
$REDIS->setCache($uid, "mail", $user->getAttribute("mail")[0]);
52+
$REDIS->setCache($uid, "sshkeys", $user->getAttribute("sshpublickey"));
53+
$REDIS->setCache($uid, "loginshell", $user->getAttribute("loginshell")[0]);
54+
$REDIS->setCache($uid, "homedir", $user->getAttribute("homedirectory")[0]);
7155
}
7256

7357
$org_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["orggroup_ou"]);
74-
echo "waiting for LDAP response (org_groups)...\n";
75-
$org_groups = $LDAP->search("objectClass=posixGroup", $CONFIG["ldap"]["basedn"]);
58+
echo "waiting for LDAP search (org groups)...\n";
59+
$org_groups = $org_group_ou->getChildrenArray(true);
7660
echo "response received.\n";
7761
// phpcs:disable
78-
$org_group_CNs = array_map(function($x){return $x->getAttribute("cn")[0];}, $org_groups);
62+
$org_group_CNs = array_map(function($x){return $x["cn"][0];}, $org_groups);
7963
// phpcs:enable
8064
sort($org_group_CNs);
8165
$REDIS->setCache("sorted_orgs", "", $org_group_CNs);
8266
foreach ($org_groups as $org_group) {
83-
$REDIS->setCache($org_group->getAttribute("cn")[0], "members", $org_group->getAttribute("memberuid"));
67+
$gid = $org_group["cn"][0];
68+
$REDIS->setCache($gid, "members", (@$org_group["memberuid"] ?? []));
8469
}
8570

8671
$pi_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["pigroup_ou"]);
87-
echo "waiting for LDAP response (pi_groups)...\n";
72+
echo "waiting for LDAP search (pi groups)...\n";
8873
$pi_groups = $pi_group_ou->getChildrenArray(true);
8974
echo "response received.\n";
9075
// phpcs:disable
@@ -93,19 +78,22 @@
9378
sort($pi_group_CNs);
9479
// FIXME should be sorted_pi_groups
9580
$REDIS->setCache("sorted_groups", "", $pi_group_CNs);
81+
9682
$user_pi_group_member_of = [];
9783
foreach ($user_CNs as $uid) {
9884
$user_pi_group_member_of[$uid] = [];
9985
}
10086
foreach ($pi_groups as $pi_group) {
101-
if (array_key_exists("memberuid", $pi_group)) {
102-
$REDIS->setCache($pi_group["cn"][0], "members", $pi_group["memberuid"]);
103-
foreach ($pi_group["memberuid"] as $member_uid) {
104-
array_push($user_pi_group_member_of[$member_uid], $pi_group["cn"][0]);
87+
$gid = $pi_group["cn"][0];
88+
$members = (@$pi_group["memberuid"] ?? []);
89+
foreach ($members as $uid) {
90+
if (in_array($uid, $user_CNs)) {
91+
array_push($user_pi_group_member_of[$uid], $gid);
92+
} else {
93+
echo "warning: group '$gid' has member '$uid' who is not in the users group!\n";
10594
}
106-
} else {
107-
$REDIS->setCache($pi_group["cn"][0], "members", []);
10895
}
96+
$REDIS->setCache($gid, "members", (@$pi_group["memberuid"] ?? []));
10997
}
11098
foreach ($user_pi_group_member_of as $uid => $pi_groups) {
11199
// FIXME should be pi_groups

0 commit comments

Comments
 (0)