Skip to content

Commit 3557080

Browse files
committed
rewrite update-ldap-cache.php
fix style fix style fix style
1 parent a1679de commit 3557080

File tree

3 files changed

+112
-64
lines changed

3 files changed

+112
-64
lines changed

resources/lib/UnityLDAP.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,34 @@ public function getOrgGroupEntry($gid)
336336
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
337337
return $ldap_entry;
338338
}
339+
340+
public static function parseUserChildrenArray(array $userChildrenArray): array
341+
{
342+
// input comes from LdapEntry::getChildrenArray on a UnityUser
343+
$output = [];
344+
$required_string_attributes = [
345+
"gidnumber",
346+
"givenname",
347+
"homedirectory",
348+
"loginshell",
349+
"mail",
350+
"o",
351+
"sn",
352+
"uid",
353+
"uidnumber",
354+
];
355+
foreach ($required_string_attributes as $key) {
356+
$output[$key] = $userChildrenArray[$key][0];
357+
}
358+
$output["firstname"] = $output["givenname"];
359+
$output["lastname"] = $output["sn"];
360+
$output["org"] = $output["o"];
361+
$output["objectclass"] = $userChildrenArray["objectclass"];
362+
if (array_key_exists("sspublickey", $userChildrenArray)) {
363+
$output["sshpublickey"] = $userChildrenArray["sshpublickey"];
364+
} else {
365+
$output["sshpublickey"] = [];
366+
}
367+
return $output;
368+
}
339369
}

resources/lib/UnityRedis.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ public function removeCacheArray($object, $key, $value)
9595
$this->setCache($object, $key, $cached_val);
9696
}
9797
}
98+
99+
public function flushAll()
100+
{
101+
$this->client->flushAll();
102+
}
98103
}

workers/update-ldap-cache.php

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,87 @@
22

33
require_once "../resources/autoload.php";
44

5-
// Get Users
6-
$users = $LDAP->getAllUsers($SQL, $MAILER, $REDIS, $WEBHOOK, true);
7-
8-
$sorted_uids = array();
9-
10-
foreach ($users as $user) {
11-
$uid = $user->getUID();
12-
array_push($sorted_uids, $uid);
13-
14-
$REDIS->setCache($uid, "firstname", $user->getFirstname(true));
15-
$REDIS->setCache($uid, "lastname", $user->getLastname(true));
16-
$REDIS->setCache($uid, "org", $user->getOrg(true));
17-
$REDIS->setCache($uid, "mail", $user->getMail(true));
18-
$REDIS->setCache($uid, "sshkeys", $user->getSSHKeys(true));
19-
$REDIS->setCache($uid, "loginshell", $user->getLoginShell(true));
20-
$REDIS->setCache($uid, "homedir", $user->getHomeDir(true));
21-
22-
$parsed_groups = array();
23-
24-
foreach ($user->getGroups(true) as $cur_group) {
25-
array_push($parsed_groups, $cur_group->getPIUID());
26-
}
27-
28-
$REDIS->setCache($uid, "groups", $parsed_groups);
5+
use UnityWebPortal\lib\{
6+
UnityConfig,
7+
UnityLDAP,
8+
UnityMailer,
9+
UnitySQL,
10+
UnitySite,
11+
UnitySSO,
12+
UnityUser,
13+
UnityRedis,
14+
UnityWebhook
15+
};
16+
use PHPOpenLDAPer\LDAPEntry;
17+
18+
$options = getopt("fu");
19+
if (array_key_exists("f", $options)) {
20+
echo "flushing cache...\n";
21+
$REDIS->flushAll();
2922
}
3023

31-
sort($sorted_uids);
32-
$REDIS->setCache("sorted_users", "", $sorted_uids);
33-
34-
// Get groups
35-
$groups = $LDAP->getAllPIGroups($SQL, $MAILER, $REDIS, $WEBHOOK, true);
36-
37-
$sorted_groups = array();
38-
39-
foreach ($groups as $group) {
40-
$gid = $group->getPIUID();
41-
array_push($sorted_groups, $gid);
42-
43-
$parsed_members = array();
44-
foreach ($group->getGroupMembers(true) as $member) {
45-
array_push($parsed_members, $member->getUID());
24+
if ((!is_null($REDIS->getCache("initialized", "")) and (!array_key_exists("u", $options)))) {
25+
echo "cache is already initialized, nothing doing. use -f argument to flush cache, or -u argument to update without flush.\n";
26+
} else {
27+
echo "updating cache...\n";
28+
$user_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["user_ou"]);
29+
echo "waiting for LDAP response (users)...\n";
30+
$users = $user_ou->getChildrenArray(true);
31+
echo "response received.\n";
32+
// phpcs:disable
33+
$user_CNs = array_map(function ($x){return $x["cn"][0];}, $users);
34+
// phpcs:enable
35+
sort($user_CNs);
36+
$REDIS->setCache("sorted_users", "", $user_CNs);
37+
foreach ($users as $user) {
38+
$attribute_array = UnityLDAP::parseUserChildrenArray($user);
39+
foreach ($attribute_array as $key => $val) {
40+
$REDIS->setCache($user["cn"][0], $key, $val);
41+
}
4642
}
4743

48-
$REDIS->setCache($gid, "members", $parsed_members);
49-
}
50-
51-
sort($sorted_groups);
52-
$REDIS->setCache("sorted_groups", "", $sorted_groups);
53-
54-
// Get Orgs
55-
$orgs = $LDAP->getAllOrgGroups($SQL, $MAILER, $REDIS, $WEBHOOK, true);
56-
57-
$sorted_orgs = array();
58-
59-
foreach ($orgs as $org) {
60-
$orgid = $org->getOrgID();
61-
array_push($sorted_orgs, $orgid);
62-
63-
$parsed_orgs = array();
64-
foreach ($org->getOrgMembers(true) as $member) {
65-
array_push($parsed_members, $member->getUID());
44+
$org_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["orggroup_ou"]);
45+
echo "waiting for LDAP response (org_groups)...\n";
46+
$org_groups = $org_group_ou->getChildrenArray(true);
47+
echo "response received.\n";
48+
// phpcs:disable
49+
$org_group_CNs = array_map(function($x){return $x["cn"][0];}, $org_groups);
50+
// phpcs:enable
51+
sort($org_group_CNs);
52+
$REDIS->setCache("sorted_orgs", "", $org_group_CNs);
53+
foreach ($org_groups as $org_group) {
54+
$REDIS->setCache($org_group["cn"][0], "members", $org_group["memberuid"]);
6655
}
6756

68-
$REDIS->setCache($orgid, "members", $parsed_orgs);
57+
$pi_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["pigroup_ou"]);
58+
echo "waiting for LDAP response (pi_groups)...\n";
59+
$pi_groups = $pi_group_ou->getChildrenArray(true);
60+
echo "response received.\n";
61+
// phpcs:disable
62+
$pi_group_CNs = array_map(function($x){return $x["cn"][0];}, $pi_groups);
63+
// phpcs:enable
64+
sort($pi_group_CNs);
65+
// FIXME should be sorted_pi_groups
66+
$REDIS->setCache("sorted_groups", "", $pi_group_CNs);
67+
$user_pi_group_member_of = [];
68+
foreach ($user_CNs as $uid) {
69+
$user_pi_group_member_of[$uid] = [];
70+
}
71+
foreach ($pi_groups as $pi_group) {
72+
if (array_key_exists("memberuid", $pi_group)) {
73+
$REDIS->setCache($pi_group["cn"][0], "members", $pi_group["memberuid"]);
74+
foreach ($pi_group["memberuid"] as $member_uid) {
75+
array_push($user_pi_group_member_of[$member_uid], $pi_group["cn"][0]);
76+
}
77+
} else {
78+
$REDIS->setCache($pi_group["cn"][0], "members", []);
79+
}
80+
}
81+
foreach ($user_pi_group_member_of as $uid => $pi_groups) {
82+
// FIXME should be pi_groups
83+
$REDIS->setCache($uid, "groups", $pi_groups);
84+
}
85+
$REDIS->setCache("initializing", "", false);
86+
$REDIS->setCache("initialized", "", true);
87+
echo "done!\n";
6988
}
70-
71-
sort($sorted_orgs);
72-
$REDIS->setCache("sorted_orgs", "", $sorted_orgs);
73-
74-
// Confirmation Message
75-
echo "OK\n";

0 commit comments

Comments
 (0)