@@ -26,13 +26,15 @@ class UnityLDAP extends ldapConn
26
26
);
27
27
28
28
// string vars for OUs
29
+ private $ STR_BASEOU ;
29
30
private $ STR_USEROU ;
30
31
private $ STR_GROUPOU ;
31
32
private $ STR_PIGROUPOU ;
32
33
private $ STR_ORGGROUPOU ;
33
34
private $ STR_ADMINGROUP ;
34
35
35
36
// Instance vars for various ldapEntry objects
37
+ private $ baseOU ;
36
38
private $ userOU ;
37
39
private $ groupOU ;
38
40
private $ pi_groupOU ;
@@ -49,6 +51,7 @@ public function __construct(
49
51
$ dn ,
50
52
$ pass ,
51
53
$ custom_user_mappings ,
54
+ $ base_dn ,
52
55
$ user_ou ,
53
56
$ group_ou ,
54
57
$ pigroup_ou ,
@@ -59,13 +62,15 @@ public function __construct(
59
62
) {
60
63
parent ::__construct ($ host , $ dn , $ pass );
61
64
65
+ $ this ->STR_BASEOU = $ base_dn ;
62
66
$ this ->STR_USEROU = $ user_ou ;
63
67
$ this ->STR_GROUPOU = $ group_ou ;
64
68
$ this ->STR_PIGROUPOU = $ pigroup_ou ;
65
69
$ this ->STR_ORGGROUPOU = $ orggroup_ou ;
66
70
$ this ->STR_ADMINGROUP = $ admin_group ;
67
71
68
72
// Get Global Entries
73
+ $ this ->baseOU = $ this ->getEntry ($ base_dn );
69
74
$ this ->userOU = $ this ->getEntry ($ user_ou );
70
75
$ this ->groupOU = $ this ->getEntry ($ group_ou );
71
76
$ this ->pi_groupOU = $ this ->getEntry ($ pigroup_ou );
@@ -123,75 +128,66 @@ public function getNextUIDNumber($UnitySQL)
123
128
{
124
129
$ max_uid = $ UnitySQL ->getSiteVar ('MAX_UID ' );
125
130
$ new_uid = $ max_uid + 1 ;
126
-
127
- while ($ this ->IDNumInUse ($ new_uid )) {
131
+ $ id_nums_in_use = $ this -> getIDNumsInUse ();
132
+ while ($ this ->IDNumInUse ($ new_uid, $ id_nums_in_use )) {
128
133
$ new_uid ++;
129
134
}
130
-
131
135
$ UnitySQL ->updateSiteVar ('MAX_UID ' , $ new_uid );
132
-
133
136
return $ new_uid ;
134
137
}
135
138
136
139
public function getNextPiGIDNumber ($ UnitySQL )
137
140
{
138
141
$ max_pigid = $ UnitySQL ->getSiteVar ('MAX_PIGID ' );
139
142
$ new_pigid = $ max_pigid + 1 ;
140
-
141
- while ($ this ->IDNumInUse ($ new_pigid )) {
143
+ $ id_nums_in_use = $ this -> getIDNumsInUse ();
144
+ while ($ this ->IDNumInUse ($ new_pigid, $ id_nums_in_use )) {
142
145
$ new_pigid ++;
143
146
}
144
-
145
147
$ UnitySQL ->updateSiteVar ('MAX_PIGID ' , $ new_pigid );
146
-
147
148
return $ new_pigid ;
148
149
}
149
150
150
151
public function getNextOrgGIDNumber ($ UnitySQL )
151
152
{
152
153
$ max_gid = $ UnitySQL ->getSiteVar ('MAX_GID ' );
153
154
$ new_gid = $ max_gid + 1 ;
154
-
155
- while ($ this ->IDNumInUse ($ new_gid )) {
155
+ $ id_nums_in_use = $ this -> getIDNumsInUse ();
156
+ while ($ this ->IDNumInUse ($ new_gid, $ id_nums_in_use )) {
156
157
$ new_gid ++;
157
158
}
158
-
159
159
$ UnitySQL ->updateSiteVar ('MAX_GID ' , $ new_gid );
160
-
161
160
return $ new_gid ;
162
161
}
163
162
164
- private function IDNumInUse ($ id )
163
+ private function IDNumInUse ($ id_num , $ id_nums_in_use )
165
164
{
166
165
// 0-99 are probably going to be used for local system accounts instead of LDAP accounts
167
166
// 100-999, 60000-64999 are reserved for debian packages
168
- if (($ id <= 999 ) || ($ id >= 60000 && $ id <= 64999 )) {
167
+ if (($ id_num <= 999 ) || ($ id_num >= 60000 && $ id_num <= 64999 )) {
169
168
return true ;
170
169
}
171
- $ users = $ this ->userOU ->getChildrenArray ([], true );
172
- foreach ($ users as $ user ) {
173
- if ($ user ["uidnumber " ][0 ] == $ id ) {
174
- return true ;
175
- }
176
- }
177
- $ pi_groups = $ this ->pi_groupOU ->getChildrenArray (["gidnumber " ], true );
178
- foreach ($ pi_groups as $ pi_group ) {
179
- if ($ pi_group ["gidnumber " ][0 ] == $ id ) {
180
- return true ;
181
- }
182
- }
183
- $ groups = $ this ->groupOU ->getChildrenArray (["gidnumber " ], true );
184
- foreach ($ groups as $ group ) {
185
- if ($ group ["gidnumber " ][0 ] == $ id ) {
186
- return true ;
187
- }
188
- }
170
+ return in_array ($ id_num , $ id_nums_in_use );
171
+ }
189
172
190
- return false ;
173
+ private function getIDNumsInUse ()
174
+ {
175
+ return array_merge (
176
+ // search entire LDAP tree, not just for entries created by portal
177
+ array_map (
178
+ fn ($ x ) => intval ($ x ["uidnumber " ][0 ]),
179
+ $ this ->baseOU ->getChildrenArray (["uidnumber " ], true , "objectClass=posixAccount " )
180
+ ),
181
+ array_map (
182
+ fn ($ x ) => intval ($ x ["gidnumber " ][0 ]),
183
+ $ this ->baseOU ->getChildrenArray (["gidnumber " ], true , "objectClass=posixGroup " )
184
+ ),
185
+ );
191
186
}
192
187
193
188
public function getUnassignedID ($ uid , $ UnitySQL )
194
189
{
190
+ $ id_nums_in_use = $ this ->getIDNumsInUse ();
195
191
$ netid = strtok ($ uid , "_ " ); // extract netid
196
192
// scrape all files in custom folder
197
193
$ dir = new \DirectoryIterator ($ this ->custom_mappings_path );
@@ -205,7 +201,7 @@ public function getUnassignedID($uid, $UnitySQL)
205
201
206
202
if ($ uid == $ netid_match || $ netid == $ netid_match ) {
207
203
// found a match
208
- if (!$ this ->IDNumInUse ($ uid_match )) {
204
+ if (!$ this ->IDNumInUse ($ uid_match, $ id_nums_in_use )) {
209
205
return $ uid_match ;
210
206
}
211
207
}
0 commit comments