Skip to content

Commit bf2e212

Browse files
authored
Merge pull request #84 from sheldor1510/maximum-uid
stored max id values in sitevars table
2 parents cb6d232 + 8a0a3c8 commit bf2e212

File tree

6 files changed

+97
-55
lines changed

6 files changed

+97
-55
lines changed

resources/lib/UnityGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ private function init()
482482
$ldapPiGroupEntry = $this->getLDAPPiGroup();
483483

484484
if (!$ldapPiGroupEntry->exists()) {
485-
$nextGID = $this->LDAP->getNextPiGIDNumber();
485+
$nextGID = $this->LDAP->getNextPiGIDNumber($this->SQL);
486486

487487
$ldapPiGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
488488
$ldapPiGroupEntry->setAttribute("gidnumber", strval($nextGID));

resources/lib/UnityLDAP.php

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -115,67 +115,46 @@ public function getDefUserShell()
115115
//
116116
// ID Number selection functions
117117
//
118-
public function getNextUIDNumber()
118+
public function getNextUIDNumber($UnitySQL)
119119
{
120-
$users = $this->userOU->getChildrenArray(true);
120+
$max_uid = $UnitySQL->getSiteVar('MAX_UID');
121+
$new_uid = $max_uid + 1;
121122

122-
// This could become inefficient with more users
123-
usort($users, function ($a, $b) {
124-
return $a["uidnumber"] <=> $b["uidnumber"];
125-
});
126-
127-
$id = self::ID_MAP[0];
128-
foreach ($users as $acc) {
129-
if ($id == $acc["uidnumber"][0]) {
130-
$id++;
131-
} else {
132-
if (!$this->GIDNumInUse($id)) {
133-
break;
134-
}
135-
}
123+
while ($this->UIDNumInUse($new_uid)) {
124+
$new_uid++;
136125
}
137126

138-
return $id;
127+
$UnitySQL->updateSiteVar('MAX_UID', $new_uid);
128+
129+
return $new_uid;
139130
}
140131

141-
public function getNextPiGIDNumber()
132+
public function getNextPiGIDNumber($UnitySQL)
142133
{
143-
$groups = $this->pi_groupOU->getChildrenArray(true);
144-
145-
usort($groups, function ($a, $b) {
146-
return $a["gidnumber"] <=> $b["gidnumber"];
147-
});
148-
149-
$id = self::PI_ID_MAP[0];
150-
foreach ($groups as $acc) {
151-
if ($id == $acc["gidnumber"][0]) {
152-
$id++;
153-
} else {
154-
break;
155-
}
134+
$max_pigid = $UnitySQL->getSiteVar('MAX_PIGID');
135+
$new_pigid = $max_pigid + 1;
136+
137+
while ($this->PIGIDNumInUse($new_pigid)) {
138+
$new_pigid++;
156139
}
157140

158-
return $id;
141+
$UnitySQL->updateSiteVar('MAX_PIGID', $new_pigid);
142+
143+
return $new_pigid;
159144
}
160145

161-
public function getNextOrgGIDNumber()
146+
public function getNextOrgGIDNumber($UnitySQL)
162147
{
163-
$groups = $this->org_groupOU->getChildrenArray(true);
164-
165-
usort($groups, function ($a, $b) {
166-
return $a["gidnumber"] <=> $b["gidnumber"];
167-
});
168-
169-
$id = self::ORG_ID_MAP[0];
170-
foreach ($groups as $acc) {
171-
if ($id == $acc["gidnumber"][0]) {
172-
$id++;
173-
} else {
174-
break;
175-
}
148+
$max_gid = $UnitySQL->getSiteVar('MAX_GID');
149+
$new_gid = $max_gid + 1;
150+
151+
while ($this->GIDNumInUse($new_gid)) {
152+
$new_gid++;
176153
}
177154

178-
return $id;
155+
$UnitySQL->updateSiteVar('MAX_GID', $new_gid);
156+
157+
return $new_gid;
179158
}
180159

181160
private function UIDNumInUse($id)
@@ -190,19 +169,31 @@ private function UIDNumInUse($id)
190169
return false;
191170
}
192171

172+
private function PIGIDNumInUse($id)
173+
{
174+
$pi_groups = $this->pi_groupOU->getChildrenArray(true);
175+
foreach ($pi_groups as $pi_group) {
176+
if ($pi_group["gidnumber"][0] == $id) {
177+
return true;
178+
}
179+
}
180+
181+
return false;
182+
}
183+
193184
private function GIDNumInUse($id)
194185
{
195-
$users = $this->groupOU->getChildrenArray(true);
196-
foreach ($users as $user) {
197-
if ($user["gidnumber"][0] == $id) {
186+
$groups = $this->groupOU->getChildrenArray(true);
187+
foreach ($groups as $group) {
188+
if ($group["gidnumber"][0] == $id) {
198189
return true;
199190
}
200191
}
201192

202193
return false;
203194
}
204195

205-
public function getUnassignedID($uid)
196+
public function getUnassignedID($uid, $UnitySQL)
206197
{
207198
$netid = strtok($uid, "_"); // extract netid
208199
// scrape all files in custom folder
@@ -226,7 +217,7 @@ public function getUnassignedID($uid)
226217
}
227218

228219
// didn't find anything from existing mappings, use next available
229-
$next_uid = $this->getNextUIDNumber();
220+
$next_uid = $this->getNextUIDNumber($UnitySQL);
230221

231222
return $next_uid;
232223
}

resources/lib/UnityOrg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function init()
3030
$org_group = $this->getLDAPOrgGroup();
3131

3232
if (!$org_group->exists()) {
33-
$nextGID = $this->LDAP->getNextOrgGIDNumber();
33+
$nextGID = $this->LDAP->getNextOrgGIDNumber($this->SQL);
3434

3535
$org_group->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
3636
$org_group->setAttribute("gidnumber", strval($nextGID));

resources/lib/UnitySQL.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class UnitySQL
1313
private const TABLE_EVENTS = "events";
1414
private const TABLE_AUDIT_LOG = "audit_log";
1515
private const TABLE_ACCOUNT_DELETION_REQUESTS = "account_deletion_requests";
16+
private const TABLE_SITEVARS = "sitevars";
1617

1718
private const REQUEST_ADMIN = "admin";
1819

@@ -275,4 +276,27 @@ public function accDeletionRequestExists($uid)
275276

276277
return count($stmt->fetchAll()) > 0;
277278
}
279+
280+
public function getSiteVar($name)
281+
{
282+
$stmt = $this->conn->prepare(
283+
"SELECT * FROM " . self::TABLE_SITEVARS . " WHERE name=:name"
284+
);
285+
$stmt->bindParam(":name", $name);
286+
287+
$stmt->execute();
288+
289+
return $stmt->fetchAll()[0]['value'];
290+
}
291+
292+
public function updateSiteVar($name, $value)
293+
{
294+
$stmt = $this->conn->prepare(
295+
"UPDATE " . self::TABLE_SITEVARS . " SET value=:value WHERE name=:name"
296+
);
297+
$stmt->bindParam(":name", $name);
298+
$stmt->bindParam(":value", $value);
299+
300+
$stmt->execute();
301+
}
278302
}

resources/lib/UnityUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function init($send_mail = true)
5353
// Create LDAP group
5454
//
5555
$ldapGroupEntry = $this->getLDAPGroup();
56-
$id = $this->LDAP->getUnassignedID($this->getUID());
56+
$id = $this->LDAP->getUnassignedID($this->getUID(), $this->SQL);
5757

5858
if (!$ldapGroupEntry->exists()) {
5959
$ldapGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);

tools/docker-dev/sql/bootstrap.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ CREATE TABLE `account_deletion_requests` (
122122

123123
-- --------------------------------------------------------
124124

125+
-- --------------------------------------------------------
126+
127+
--
128+
-- Table structure for table `sitevars`
129+
--
130+
131+
CREATE TABLE `sitevars` (
132+
`id` int(11) NOT NULL,
133+
`name` varchar(1000) NOT NULL,
134+
`value` varchar(1000) NOT NULL
135+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
136+
137+
-- --------------------------------------------------------
138+
125139
--
126140
-- Indexes for dumped tables
127141
--
@@ -168,6 +182,12 @@ ALTER TABLE `audit_log`
168182
ALTER TABLE `account_deletion_requests`
169183
ADD PRIMARY KEY (`id`);
170184

185+
--
186+
-- Indexes for table `sitevars`
187+
--
188+
ALTER TABLE `sitevars`
189+
ADD PRIMARY KEY (`id`);
190+
171191
--
172192
-- AUTO_INCREMENT for dumped tables
173193
--
@@ -217,6 +237,13 @@ ALTER TABLE `account_deletion_requests`
217237
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
218238
COMMIT;
219239

240+
--
241+
-- AUTO_INCREMENT for table `sitevars`
242+
--
243+
ALTER TABLE `sitevars`
244+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
245+
COMMIT;
246+
220247
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
221248
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
222249
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 commit comments

Comments
 (0)