Skip to content

Commit 89cbfdf

Browse files
committed
fix update-ldap-cache.php
1 parent 3dffd6c commit 89cbfdf

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

resources/lib/UnityLDAP.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -332,35 +332,4 @@ public function getOrgGroupEntry($gid)
332332
$gid = ldap_escape($gid, LDAP_ESCAPE_DN);
333333
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
334334
}
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-
}
366335
}

workers/update-ldap-cache.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
use UnityWebPortal\lib\UnityWebhook;
1414
use PHPOpenLDAPer\LDAPEntry;
1515

16+
// in PHP LDAP all attributes are arrays, we need these as strings instead
17+
// it's possible but probably difficult to find this out using LDAP schema information
18+
$user_string_attributes = [
19+
"gidnumber",
20+
"givenname",
21+
"homedirectory",
22+
"loginshell",
23+
"mail",
24+
"o",
25+
"sn",
26+
"uid",
27+
"uidnumber",
28+
"gecos",
29+
];
30+
31+
$pi_group_string_attributes = [
32+
"gidnumber",
33+
];
34+
1635
$options = getopt("fu");
1736
if (array_key_exists("f", $options)) {
1837
echo "flushing cache...\n";
@@ -21,36 +40,39 @@
2140

2241
if ((!is_null($REDIS->getCache("initialized", "")) and (!array_key_exists("u", $options)))) {
2342
echo "cache is already initialized, nothing doing.";
24-
echo " use -f argument to flush cache, or -u argument to update without flush.";
43+
echo " use -f argument to flush cache, or -u argument to update without flush.\n";
2544
} else {
2645
echo "updating cache...\n";
27-
$user_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["user_ou"]);
2846
echo "waiting for LDAP response (users)...\n";
29-
$users = $user_ou->getChildrenArray(true);
47+
$users = $LDAP->search("objectClass=posixAccount", $CONFIG["ldap"]["basedn"]);
3048
echo "response received.\n";
3149
// phpcs:disable
32-
$user_CNs = array_map(function ($x){return $x["cn"][0];}, $users);
50+
$user_CNs = array_map(function ($x){return $x->getAttribute("cn")[0];}, $users);
3351
// phpcs:enable
3452
sort($user_CNs);
3553
$REDIS->setCache("sorted_users", "", $user_CNs);
3654
foreach ($users as $user) {
37-
$attribute_array = UnityLDAP::parseUserChildrenArray($user);
38-
foreach ($attribute_array as $key => $val) {
39-
$REDIS->setCache($user["cn"][0], $key, $val);
55+
$cn = $user->getAttribute("cn")[0];
56+
foreach ($user->getAttributes() as $key => $val) {
57+
if (in_array($key, $user_string_attributes)) {
58+
$REDIS->setCache($cn, $key, $val[0]);
59+
} else {
60+
$REDIS->setCache($cn, $key, $val);
61+
}
4062
}
4163
}
4264

4365
$org_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["orggroup_ou"]);
4466
echo "waiting for LDAP response (org_groups)...\n";
45-
$org_groups = $org_group_ou->getChildrenArray(true);
67+
$org_groups = $LDAP->search("objectClass=posixGroup", $CONFIG["ldap"]["basedn"]);
4668
echo "response received.\n";
4769
// phpcs:disable
48-
$org_group_CNs = array_map(function($x){return $x["cn"][0];}, $org_groups);
70+
$org_group_CNs = array_map(function($x){return $x->getAttribute("cn")[0];}, $org_groups);
4971
// phpcs:enable
5072
sort($org_group_CNs);
5173
$REDIS->setCache("sorted_orgs", "", $org_group_CNs);
5274
foreach ($org_groups as $org_group) {
53-
$REDIS->setCache($org_group["cn"][0], "members", $org_group["memberuid"]);
75+
$REDIS->setCache($org_group->getAttribute("cn")[0], "members", $org_group->getAttribute("memberuid"));
5476
}
5577

5678
$pi_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["pigroup_ou"]);

0 commit comments

Comments
 (0)