Skip to content

stored max id values in sitevars table #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private function init()
$ldapPiGroupEntry = $this->getLDAPPiGroup();

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

$ldapPiGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$ldapPiGroupEntry->setAttribute("gidnumber", strval($nextGID));
Expand Down
95 changes: 43 additions & 52 deletions resources/lib/UnityLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,67 +115,46 @@ public function getDefUserShell()
//
// ID Number selection functions
//
public function getNextUIDNumber()
public function getNextUIDNumber($UnitySQL)
{
$users = $this->userOU->getChildrenArray(true);
$max_uid = $UnitySQL->getSiteVar('MAX_UID');
$new_uid = $max_uid + 1;

// This could become inefficient with more users
usort($users, function ($a, $b) {
return $a["uidnumber"] <=> $b["uidnumber"];
});

$id = self::ID_MAP[0];
foreach ($users as $acc) {
if ($id == $acc["uidnumber"][0]) {
$id++;
} else {
if (!$this->GIDNumInUse($id)) {
break;
}
}
while ($this->UIDNumInUse($new_uid)) {
$new_uid++;
}

return $id;
$UnitySQL->updateSiteVar('MAX_UID', $new_uid);

return $new_uid;
}

public function getNextPiGIDNumber()
public function getNextPiGIDNumber($UnitySQL)
{
$groups = $this->pi_groupOU->getChildrenArray(true);

usort($groups, function ($a, $b) {
return $a["gidnumber"] <=> $b["gidnumber"];
});

$id = self::PI_ID_MAP[0];
foreach ($groups as $acc) {
if ($id == $acc["gidnumber"][0]) {
$id++;
} else {
break;
}
$max_pigid = $UnitySQL->getSiteVar('MAX_PIGID');
$new_pigid = $max_pigid + 1;

while ($this->PIGIDNumInUse($new_pigid)) {
$new_pigid++;
}

return $id;
$UnitySQL->updateSiteVar('MAX_PIGID', $new_pigid);

return $new_pigid;
}

public function getNextOrgGIDNumber()
public function getNextOrgGIDNumber($UnitySQL)
{
$groups = $this->org_groupOU->getChildrenArray(true);

usort($groups, function ($a, $b) {
return $a["gidnumber"] <=> $b["gidnumber"];
});

$id = self::ORG_ID_MAP[0];
foreach ($groups as $acc) {
if ($id == $acc["gidnumber"][0]) {
$id++;
} else {
break;
}
$max_gid = $UnitySQL->getSiteVar('MAX_GID');
$new_gid = $max_gid + 1;

while ($this->GIDNumInUse($new_gid)) {
$new_gid++;
}

return $id;
$UnitySQL->updateSiteVar('MAX_GID', $new_gid);

return $new_gid;
}

private function UIDNumInUse($id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if you made 2 additional methods for GIDNumInUse and PIGIDNumInUse to keep it consistent.

Expand All @@ -190,19 +169,31 @@ private function UIDNumInUse($id)
return false;
}

private function PIGIDNumInUse($id)
{
$pi_groups = $this->pi_groupOU->getChildrenArray(true);
foreach ($pi_groups as $pi_group) {
if ($pi_group["gidnumber"][0] == $id) {
return true;
}
}

return false;
}

private function GIDNumInUse($id)
{
$users = $this->groupOU->getChildrenArray(true);
foreach ($users as $user) {
if ($user["gidnumber"][0] == $id) {
$groups = $this->groupOU->getChildrenArray(true);
foreach ($groups as $group) {
if ($group["gidnumber"][0] == $id) {
return true;
}
}

return false;
}

public function getUnassignedID($uid)
public function getUnassignedID($uid, $UnitySQL)
{
$netid = strtok($uid, "_"); // extract netid
// scrape all files in custom folder
Expand All @@ -226,7 +217,7 @@ public function getUnassignedID($uid)
}

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

return $next_uid;
}
Expand Down
2 changes: 1 addition & 1 deletion resources/lib/UnityOrg.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function init()
$org_group = $this->getLDAPOrgGroup();

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

$org_group->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$org_group->setAttribute("gidnumber", strval($nextGID));
Expand Down
24 changes: 24 additions & 0 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class UnitySQL
private const TABLE_EVENTS = "events";
private const TABLE_AUDIT_LOG = "audit_log";
private const TABLE_ACCOUNT_DELETION_REQUESTS = "account_deletion_requests";
private const TABLE_SITEVARS = "sitevars";

private const REQUEST_ADMIN = "admin";

Expand Down Expand Up @@ -275,4 +276,27 @@ public function accDeletionRequestExists($uid)

return count($stmt->fetchAll()) > 0;
}

public function getSiteVar($name)
{
$stmt = $this->conn->prepare(
"SELECT * FROM " . self::TABLE_SITEVARS . " WHERE name=:name"
);
$stmt->bindParam(":name", $name);

$stmt->execute();

return $stmt->fetchAll()[0]['value'];
}

public function updateSiteVar($name, $value)
{
$stmt = $this->conn->prepare(
"UPDATE " . self::TABLE_SITEVARS . " SET value=:value WHERE name=:name"
);
$stmt->bindParam(":name", $name);
$stmt->bindParam(":value", $value);

$stmt->execute();
}
}
2 changes: 1 addition & 1 deletion resources/lib/UnityUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function init($send_mail = true)
// Create LDAP group
//
$ldapGroupEntry = $this->getLDAPGroup();
$id = $this->LDAP->getUnassignedID($this->getUID());
$id = $this->LDAP->getUnassignedID($this->getUID(), $this->SQL);

if (!$ldapGroupEntry->exists()) {
$ldapGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
Expand Down
27 changes: 27 additions & 0 deletions tools/docker-dev/sql/bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ CREATE TABLE `account_deletion_requests` (

-- --------------------------------------------------------

-- --------------------------------------------------------

--
-- Table structure for table `sitevars`
--

CREATE TABLE `sitevars` (
`id` int(11) NOT NULL,
`name` varchar(1000) NOT NULL,
`value` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Indexes for dumped tables
--
Expand Down Expand Up @@ -168,6 +182,12 @@ ALTER TABLE `audit_log`
ALTER TABLE `account_deletion_requests`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `sitevars`
--
ALTER TABLE `sitevars`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--
Expand Down Expand Up @@ -217,6 +237,13 @@ ALTER TABLE `account_deletion_requests`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

--
-- AUTO_INCREMENT for table `sitevars`
--
ALTER TABLE `sitevars`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;