Skip to content

Commit 2a0d335

Browse files
committed
rewrite update-ldap-cache.php
1 parent a1679de commit 2a0d335

File tree

3 files changed

+104
-63
lines changed

3 files changed

+104
-63
lines changed

resources/lib/UnityLDAP.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,33 @@ 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+
// input comes from LdapEntry::getChildrenArray on a UnityUser
342+
$output = [];
343+
$required_string_attributes = [
344+
"gidnumber",
345+
"givenname",
346+
"homedirectory",
347+
"loginshell",
348+
"mail",
349+
"o",
350+
"sn",
351+
"uid",
352+
"uidnumber",
353+
];
354+
foreach($required_string_attributes as $key){
355+
$output[$key] = $userChildrenArray[$key][0];
356+
}
357+
$output["firstname"] = $output["givenname"];
358+
$output["lastname"] = $output["sn"];
359+
$output["org"] = $output["o"];
360+
$output["objectclass"] = $userChildrenArray["objectclass"];
361+
if(array_key_exists("sspublickey", $userChildrenArray)){
362+
$output["sshpublickey"] = $userChildrenArray["sshpublickey"];
363+
} else {
364+
$output["sshpublickey"] = [];
365+
}
366+
return $output;
367+
}
339368
}

resources/lib/UnityRedis.php

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

workers/update-ldap-cache.php

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,82 @@
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+
$user_CNs = array_map(function($x){return $x["cn"][0];}, $users);
33+
sort($user_CNs);
34+
$REDIS->setCache("sorted_users", "", $user_CNs);
35+
foreach($users as $user){
36+
$attribute_array = UnityLDAP::parseUserChildrenArray($user);
37+
foreach($attribute_array as $key => $val){
38+
$REDIS->setCache($user["cn"][0], $key, $val);
39+
}
4640
}
4741

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());
42+
$org_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["orggroup_ou"]);
43+
echo "waiting for LDAP response (org_groups)...\n";
44+
$org_groups = $org_group_ou->getChildrenArray(true);
45+
echo "response received.\n";
46+
$org_group_CNs = array_map(function($x){return $x["cn"][0];}, $org_groups);
47+
sort($org_group_CNs);
48+
$REDIS->setCache("sorted_orgs", "", $org_group_CNs);
49+
foreach($org_groups as $org_group){
50+
$REDIS->setCache($org_group["cn"][0], "members", $org_group["memberuid"]);
6651
}
6752

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

71-
sort($sorted_orgs);
72-
$REDIS->setCache("sorted_orgs", "", $sorted_orgs);
73-
74-
// Confirmation Message
75-
echo "OK\n";

0 commit comments

Comments
 (0)