@@ -26,13 +26,15 @@ class UnityLDAP extends ldapConn
2626 );
2727
2828 // string vars for OUs
29+ private $ STR_BASEOU ;
2930 private $ STR_USEROU ;
3031 private $ STR_GROUPOU ;
3132 private $ STR_PIGROUPOU ;
3233 private $ STR_ORGGROUPOU ;
3334 private $ STR_ADMINGROUP ;
3435
3536 // Instance vars for various ldapEntry objects
37+ private $ baseOU ;
3638 private $ userOU ;
3739 private $ groupOU ;
3840 private $ pi_groupOU ;
@@ -49,6 +51,7 @@ public function __construct(
4951 $ dn ,
5052 $ pass ,
5153 $ custom_user_mappings ,
54+ $ base_ou ,
5255 $ user_ou ,
5356 $ group_ou ,
5457 $ pigroup_ou ,
@@ -59,13 +62,15 @@ public function __construct(
5962 ) {
6063 parent ::__construct ($ host , $ dn , $ pass );
6164
65+ $ this ->STR_BASEOU = $ base_ou ;
6266 $ this ->STR_USEROU = $ user_ou ;
6367 $ this ->STR_GROUPOU = $ group_ou ;
6468 $ this ->STR_PIGROUPOU = $ pigroup_ou ;
6569 $ this ->STR_ORGGROUPOU = $ orggroup_ou ;
6670 $ this ->STR_ADMINGROUP = $ admin_group ;
6771
6872 // Get Global Entries
73+ $ this ->baseOU = $ this ->getEntry ($ base_ou );
6974 $ this ->userOU = $ this ->getEntry ($ user_ou );
7075 $ this ->groupOU = $ this ->getEntry ($ group_ou );
7176 $ this ->pi_groupOU = $ this ->getEntry ($ pigroup_ou );
@@ -219,6 +224,13 @@ public function getUnassignedID($uid, $UnitySQL)
219224 return $ next_uid ;
220225 }
221226
227+ public function getAllUsersUIDs ()
228+ {
229+ // should not use $user_ou->getChildren or $base_ou->getChildren(objectClass=posixAccount)
230+ // Unity users might be outside user ou, and not all users in LDAP tree are unity users
231+ return $ this ->userGroup ->getAttribute ("memberuid " );
232+ }
233+
222234 //
223235 // Functions that return user/group objects
224236 //
@@ -232,21 +244,35 @@ public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook,
232244 foreach ($ users as $ user ) {
233245 array_push ($ out , new UnityUser ($ user , $ this , $ UnitySQL , $ UnityMailer , $ UnityRedis , $ UnityWebhook ));
234246 }
235-
236247 return $ out ;
237248 }
238249 }
239250
240- $ users = $ this ->userGroup -> getAttribute ( " memberuid " );
251+ $ users = $ this ->getAllUsersUIDs ( );
241252 sort ($ users );
242253 foreach ($ users as $ user ) {
243254 $ params = array ($ user , $ this , $ UnitySQL , $ UnityMailer , $ UnityRedis , $ UnityWebhook );
244255 array_push ($ out , new UnityUser (...$ params ));
245256 }
246-
247257 return $ out ;
248258 }
249259
260+ public function getAllUsersAttributes ($ attributes )
261+ {
262+ $ include_uids = $ this ->getAllUsersUIDs ();
263+ $ user_attributes = $ this ->baseOU ->getChildrenArray (
264+ $ attributes ,
265+ true , // recursive
266+ "objectClass=posixAccount "
267+ );
268+ foreach ($ user_attributes as $ i => $ attributes ) {
269+ if (!in_array ($ attributes ["uid " ][0 ], $ include_uids )) {
270+ unset($ user_attributes [$ i ]);
271+ }
272+ }
273+ return $ user_attributes ;
274+ }
275+
250276 public function getAllPIGroups ($ UnitySQL , $ UnityMailer , $ UnityRedis , $ UnityWebhook , $ ignorecache = false )
251277 {
252278 $ out = array ();
@@ -279,6 +305,58 @@ public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebho
279305 return $ out ;
280306 }
281307
308+ public function getAllPIGroupsAttributes ($ attributes )
309+ {
310+ return $ this ->pi_groupOU ->getChildrenArray ($ attributes );
311+ }
312+
313+ public function getPIGroupGIDsWithMemberUID ($ uid )
314+ {
315+ return array_map (
316+ fn ($ x ) => $ x ["cn " ][0 ],
317+ $ this ->pi_groupOU ->getChildrenArray (
318+ ["cn " ],
319+ false ,
320+ "(memberuid= " . ldap_escape ($ uid , LDAP_ESCAPE_FILTER ) . ") " ,
321+ )
322+ );
323+ }
324+
325+ public function getAllPIGroupOwnerAttributes ($ attributes )
326+ {
327+ // get the PI groups, filter for just the GIDs, then map the GIDs to owner UIDs
328+ $ owner_uids = array_map (
329+ fn ($ x ) => UnityGroup::GID2OwnerUID ($ x ),
330+ array_map (
331+ fn ($ x ) => $ x ["cn " ][0 ],
332+ $ this ->pi_groupOU ->getChildrenArray (["cn " ]),
333+ ),
334+ );
335+ $ owner_attributes = $ this ->getAllUsersAttributes ($ attributes );
336+ foreach ($ owner_attributes as $ i => $ attributes ) {
337+ if (!in_array ($ attributes ["uid " ][0 ], $ owner_uids )) {
338+ unset($ owner_attributes [$ i ]);
339+ }
340+ }
341+ return $ owner_attributes ;
342+ }
343+
344+ /** Returns an associative array where keys are UIDs and values are arrays of PI GIDs */
345+ public function getAllUID2PIGIDs ()
346+ {
347+ // initialize output so each UID is a key with an empty array as its value
348+ $ uids = $ this ->getAllUsersUIDs ();
349+ $ uid2pigids = array_combine ($ uids , array_fill (0 , count ($ uids ), []));
350+ // for each PI group, append that GID to the member list for each of its member UIDs
351+ foreach ($ this ->getAllPIGroupsAttributes (["cn " , "memberuid " ]) as $ array ) {
352+ $ gid = $ array ["cn " ][0 ];
353+ foreach ($ array ["memberuid " ] as $ uid ) {
354+ array_push ($ uid2pigids [$ uid ], $ gid );
355+ }
356+ }
357+ return $ uid2pigids ;
358+ }
359+
282360 public function getAllOrgGroups ($ UnitySQL , $ UnityMailer , $ UnityRedis , $ UnityWebhook , $ ignorecache = false )
283361 {
284362 $ out = array ();
@@ -310,6 +388,11 @@ public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebh
310388 return $ out ;
311389 }
312390
391+ public function getAllOrgGroupsAttributes ($ attributes )
392+ {
393+ return $ this ->org_groupOU ->getChildrenArray ($ attributes );
394+ }
395+
313396 public function getUserEntry ($ uid )
314397 {
315398 $ uid = ldap_escape ($ uid , "" , LDAP_ESCAPE_DN );
0 commit comments