|
2 | 2 |
|
3 | 3 | require_once "../resources/autoload.php";
|
4 | 4 |
|
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(); |
29 | 22 | }
|
30 | 23 |
|
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 | + } |
46 | 42 | }
|
47 | 43 |
|
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"]); |
66 | 55 | }
|
67 | 56 |
|
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"; |
69 | 88 | }
|
70 |
| - |
71 |
| -sort($sorted_orgs); |
72 |
| -$REDIS->setCache("sorted_orgs", "", $sorted_orgs); |
73 |
| - |
74 |
| -// Confirmation Message |
75 |
| -echo "OK\n"; |
|
0 commit comments