@@ -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,74 +128,65 @@ 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
- // id reserved for debian packages
167
- if (($ id >= 100 && $ id <= 999 ) || ($ id >= 60000 && $ id <= 64999 )) {
165
+ // reserved for debian packages
166
+ if (($ id_num >= 100 && $ id_num <= 999 ) || ($ id_num >= 60000 && $ id_num <= 64999 )) {
168
167
return true ;
169
168
}
170
- $ users = $ this ->userOU ->getChildrenArray ([], true );
171
- foreach ($ users as $ user ) {
172
- if ($ user ["uidnumber " ][0 ] == $ id ) {
173
- return true ;
174
- }
175
- }
176
- $ pi_groups = $ this ->pi_groupOU ->getChildrenArray (["gidnumber " ], true );
177
- foreach ($ pi_groups as $ pi_group ) {
178
- if ($ pi_group ["gidnumber " ][0 ] == $ id ) {
179
- return true ;
180
- }
181
- }
182
- $ groups = $ this ->groupOU ->getChildrenArray (["gidnumber " ], true );
183
- foreach ($ groups as $ group ) {
184
- if ($ group ["gidnumber " ][0 ] == $ id ) {
185
- return true ;
186
- }
187
- }
169
+ return in_array ($ id_num , $ id_nums_in_use );
170
+ }
188
171
189
- return false ;
172
+ private function getIDNumsInUse ()
173
+ {
174
+ return array_merge (
175
+ // search entire LDAP tree, not just for entries created by portal
176
+ array_map (
177
+ fn ($ x ) => intval ($ x ["uidnumber " ][0 ]),
178
+ $ this ->baseOU ->getChildrenArray (["uidnumber " ], true , "objectClass=posixAccount " )
179
+ ),
180
+ array_map (
181
+ fn ($ x ) => intval ($ x ["gidnumber " ][0 ]),
182
+ $ this ->baseOU ->getChildrenArray (["gidnumber " ], true , "objectClass=posixGroup " )
183
+ ),
184
+ );
190
185
}
191
186
192
187
public function getUnassignedID ($ uid , $ UnitySQL )
193
188
{
189
+ $ id_nums_in_use = $ this ->getIDNumsInUse ();
194
190
$ netid = strtok ($ uid , "_ " ); // extract netid
195
191
// scrape all files in custom folder
196
192
$ dir = new \DirectoryIterator ($ this ->custom_mappings_path );
@@ -204,7 +200,7 @@ public function getUnassignedID($uid, $UnitySQL)
204
200
205
201
if ($ uid == $ netid_match || $ netid == $ netid_match ) {
206
202
// found a match
207
- if (!$ this ->IDNumInUse ($ uid_match )) {
203
+ if (!$ this ->IDNumInUse ($ uid_match, $ id_nums_in_use )) {
208
204
return $ uid_match ;
209
205
}
210
206
}
0 commit comments