Skip to content

Commit

Permalink
Leaf 4486 - update local disable/enable
Browse files Browse the repository at this point in the history
  • Loading branch information
jampaul3 committed Dec 18, 2024
1 parent 863f3d1 commit 3838fe0
Showing 1 changed file with 149 additions and 40 deletions.
189 changes: 149 additions & 40 deletions LEAF_Nexus/sources/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public function refresh(string $user_name): array

if (!isset($national_emp['data'])) {
$this->disableEmployees(explode(',', $user_name));

$this->disableAllTables();
$this->disablePortalTables();

$return_value = array(
'status' => array(
'code' => 4,
Expand All @@ -150,6 +154,10 @@ public function refresh(string $user_name): array
);
} else {
$this->disableEmployees(explode(',', $user_name));

$this->disableAllTables();
$this->disablePortalTables();

$return_value = array(
'status' => array(
'code' => 4,
Expand Down Expand Up @@ -286,10 +294,12 @@ private function disablePortalTables(): void

foreach ($this->disableUserNamePortalTables as $table => $field) {
if (is_array($field)) {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field[0] . '` = :disabledUserName,
`' . $field[1] . '` = :disabledUserName
WHERE `' . $field[0] . '` = :originalUserName;';
foreach ($field as $fld) {
$sql .= 'UPDATE `' . $table .'`
SET `' . $fld . '` = :disabledUserName
WHERE `' . $fld . '` = :originalUserName;';
}

} else {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field . '` = :disabledUserName
Expand All @@ -298,51 +308,91 @@ private function disablePortalTables(): void
}

foreach ($portals as $portal) {
$portal_db->query('USE' . $portal['portal_database']);
$sql2 = 'USE ' . $portal['portal_database'];
$portal_db->prepared_query($sql2, array());

foreach ($this->disabledUsers as $user) {
// break down the userName to get original userName
$userName = explode('_', $user);
$userName = explode('_', $user['userName']);

// update all tables with the new userName
$vars = array(':disabledUserName' => $user,
$vars = array(':disabledUserName' => $user['userName'],
':originalUserName' => $userName[2]);

$this->db->prepared_query($sql, $vars);
$portal_db->prepared_query($sql, $vars);
}
}
}

private function enableAllPortalTables(string $userName): void
{
$portals = $this->getPortals();

$portal_db = $this->db;

$userNameParts = explode('_', $userName);

$vars = array(':disabledUserName' => $userName,
':originalUserName' => $userNameParts[2]);
$sql = '';

foreach ($this->disableUserNamePortalTables as $table => $field) {
if (is_array($field)) {
foreach ($field as $fld) {
$sql .= 'UPDATE `' . $table .'`
SET `' . $fld . '` = :originalUserName
WHERE `' . $fld . '` = :disabledUserName;';
}

} else {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field . '` = :originalUserName
WHERE `' . $field . '` = :disabledUserName;';
}
}

foreach ($portals as $portal) {
$sql2 = 'USE ' . $portal['portal_database'];
$portal_db->prepared_query($sql2, array());

$portal_db->prepared_query($sql, $vars);
}
}

private function getPortals(): array
{
// need to get the portals to update. Use ABSOLUTE_ORG_PATH to get all portals from
// the sites table will need to strip https://domain
$orgchart = str_replace(HTTP_HOST, '', ABSOLUTE_ORG_PATH);
$orgchart = str_replace('https://' . HTTP_HOST, '', ABSOLUTE_ORG_PATH);
$launchpad_db = new Db(DIRECTORY_HOST, DIRECTORY_USER, DIRECTORY_PASS, 'national_leaf_launchpad');

$vars = array(':orgchartPath' => $orgchart);
$sql = 'SELECT `portal_database`
FROM `sites`
WHERE `orgchart_path` = :orgchartPath';
WHERE `orgchart_path` = :orgchartPath
AND (`portal_database` IS NOT NULL
OR `portal_database` <> "")';

$return_value = $launchpad_db->prepared_query($sql, $vars);

return $return_value;
}

private function disableAllTables(): void
private function disableAllTables(int $deletedAgo = 600): void
{
// get all the newly disabled users
$this->disabledUsers = $this->getNewlyDisabledUsers();
$this->disabledUsers = $this->getNewlyDisabledUsers($deletedAgo);

$sql = '';

foreach ($this->disableUserNameOrgchartTables as $table => $field) {
if (is_array($field)) {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field[0] . '` = :disabledUserName,
`' . $field[1] . '` = :disabledUserName
WHERE `' . $field[0] . '` = :originalUserName;';
foreach ($field as $fld) {
$sql .= 'UPDATE `' . $table .'`
SET `' . $fld . '` = :disabledUserName
WHERE `' . $fld . '` = :originalUserName;';
}

} else {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field . '` = :disabledUserName
Expand All @@ -352,21 +402,47 @@ private function disableAllTables(): void

foreach ($this->disabledUsers as $user) {
// break down the userName to get original userName
$userName = explode('_', $user);
$userName = explode('_', $user['userName']);

// update all tables with the new userName
$vars = array(':disabledUserName' => $user,
$vars = array(':disabledUserName' => $user['userName'],
':originalUserName' => $userName[2]);

$this->db->prepared_query($sql, $vars);
}
}

private function getNewlyDisabledUsers(): array
private function enableAllTables(string $userName): void
{
$vars = array(':deleteTime' => time() - 600);
$userNameParts = explode('_', $userName);

$vars = array(':disabledUserName' => $userName,
':originalUserName' => $userNameParts[2]);
$sql = '';

foreach ($this->disableUserNameOrgchartTables as $table => $field) {
if (is_array($field)) {
foreach ($field as $fld) {
$sql .= 'UPDATE `' . $table .'`
SET `' . $fld . '` = :originalUserName
WHERE `' . $fld . '` = :disabledUserName;';
}

} else {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field . '` = :originalUserName
WHERE `' . $field . '` = :disabledUserName;';
}
}

$this->db->prepared_query($sql, $vars);
}

private function getNewlyDisabledUsers(int $deletedAgo = 600): array
{
$vars = array(':deleteTime' => time() - $deletedAgo);
$sql = 'SELECT `userName`
FROM `employees`
FROM `employee`
WHERE `deleted` > :deleteTime';

$return_value = $this->db->prepared_query($sql, $vars);
Expand Down Expand Up @@ -503,6 +579,23 @@ private function disableEmployees(array $deleted_employees): array
return $return_value;
}

private function enableEmployee(string $userName): void
{
// userName will be in the format of disabled_ts_userName.
// This will need to be torn apart and only the userName kept.
$userNameParts = explode('_', $userName);

$vars = array(':userName' => $userNameParts[2],
':disabledUserName' => $userName,
':deletedTime' => 0);
$sql = 'UPDATE `employee`
SET `userName` = :userName,
`deleted` = :deletedTime
WHERE `userName` = :disabledUserName';

$this->db->prepared_query($sql, $vars);
}

/**
* @param array $national_employee_uids
* @param array $local_employee_array
Expand Down Expand Up @@ -921,54 +1014,70 @@ public function importFromNational($userName)

/**
* Marks employee as deleted
* disabling a user requires that all instances of the userName be updated both in
* orgchart and portals
* @param int $empUID
* @return bool
*/
public function disableAccount($empUID)
{
if (!is_numeric($empUID))
{
if (!is_numeric($empUID)) {
return false;
}

$memberships = $this->login->getMembership();
if (!isset($memberships['groupID'][1]))
{

if (!isset($memberships['groupID'][1])) {
throw new Exception('Administrator access required to disable accounts');
}

$vars = array(':empUID' => $empUID,
':time' => time(),
);
$res = $this->db->prepared_query('UPDATE employee
SET deleted=:time
WHERE empUID=:empUID', $vars);
$vars = array(':empUID' => $empUID);
$sql = 'SELECT `userName`
FROM `employee`
WHERE `empUID` = :empUID';

$res = $this->db->prepared_query($sql, $vars);

$this->disableEmployees(array($res[0]['userName']));

$this->disableAllTables(60);
$this->disablePortalTables();

return true;
}

/**
* Marks employee as not deleted
* Enabling someone requires that all instances of the userName be updated in both
* the orgchart and portals
* @param int $empUID
* @return bool
*/
public function enableAccount($empUID)
{
if (!is_numeric($empUID))
{
if (!is_numeric($empUID)) {
return false;
}

$memberships = $this->login->getMembership();
if (!isset($memberships['groupID'][1]))
{

if (!isset($memberships['groupID'][1])) {
throw new Exception('Administrator access required to enable accounts');
}

$vars = array(':empUID' => $empUID,
':time' => 0,
);
$res = $this->db->prepared_query('UPDATE employee
$vars = array(':empUID' => $empUID);
$sql = 'SELECT `userName`
FROM `employee`
WHERE `empUID` = :empUID';

$res = $this->db->prepared_query($sql, $vars);

$this->enableEmployee($res[0]['userName']);
$this->enableAllTables($res[0]['userName']);
$this->enableAllPortalTables($res[0]['userName']);
/* $res = $this->db->prepared_query('UPDATE employee
SET deleted=:time
WHERE empUID=:empUID', $vars);
WHERE empUID=:empUID', $vars); */

return true;
}
Expand Down

0 comments on commit 3838fe0

Please sign in to comment.