Skip to content
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

Leaf 4486 - prevent user name recycle #2624

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
144 changes: 140 additions & 4 deletions LEAF_Nexus/sources/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ class Employee extends Data

private $deepSearch = 3; // Threshold for deeper search (min # of results before searching deeper)

private $disabledUsers;

// the first value is the table, the second is the field. If the field is an array
// the first value needs to be the field used for the where clause. The field array
// is not current used but is setup to be able to be used later if needed.
private $disableUserNameOrgchartTables = array(
'employee_data' => 'author',
'employee_data_history' => 'author',
'group_data' => 'author',
'group_data_history' => 'author',
'position_data' => 'author',
'position_data_history' => 'author',
'relation_employee_backup' => 'approverUserName'
);

// the first value is the table, the second is the field. If the field is an array
// the first value needs to be the field used for the where clause.
private $disableUserNamePortalTables = array(
'action_history' => 'userID',
'approvals' => 'userID',
'data' => 'userID',
'data_extended' => 'userID',
'data_history' => 'userID',
'email_tracker' => 'userID',
'notes' => 'userID',
'process_query' => 'userID',
'records' => 'userID',
'service_chiefs' => array('userID', 'backupID'),
'signatures' => 'userID',
'tags' => 'userID',
'users' => array('userID', 'backupID')
);

public function initialize()
{
$this->setDataTable($this->dataTable);
Expand Down Expand Up @@ -221,6 +254,9 @@ private function updateEmployeeDataBatch(array $local_employees): array

if (!empty($local_deleted_employees)) {
$results[] = $this->disableEmployees($local_deleted_employees);

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

if (!empty($local_array)) {
Expand All @@ -240,6 +276,104 @@ private function updateEmployeeDataBatch(array $local_employees): array
return $results;
}

private function disablePortalTables(): void
{
$portals = $this->getPortals();

$portal_db = $this->db;

$sql = '';

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

foreach ($portals as $portal) {
$portal_db->query('USE' . $portal['portal_database']);

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

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

$this->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);
$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';

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

return $return_value;
}

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

$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;';
} else {
$sql .= 'UPDATE `' . $table .'`
SET `' . $field . '` = :disabledUserName
WHERE `' . $field . '` = :originalUserName;';
}
}

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

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

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

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

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

return $return_value;
}

/**
* @param array $local_employees_array
*
Expand Down Expand Up @@ -344,7 +478,8 @@ private function disableEmployees(array $deleted_employees): array
{
if (!empty($deleted_employees)) {
$sql = "UPDATE `employee`
SET `deleted` = UNIX_TIMESTAMP(NOW())
SET `deleted` = UNIX_TIMESTAMP(NOW()),
`userName` = concat('disabled_', `deleted`, '_', `userName`)
WHERE `userName` IN (" . implode(",", array_fill(1, count($deleted_employees), '?')) . ")";

$result = $this->db->prepared_query($sql, array_values($deleted_employees));
Expand Down Expand Up @@ -449,7 +584,8 @@ private function getAllEmployees(Db $db): array
{
$vars = array();
$sql = 'SELECT LOWER(`userName`) AS `userName`
FROM `employee`';
FROM `employee`
WHERE `userName` NOT LIKE "disabled_%"';

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

Expand Down Expand Up @@ -1442,7 +1578,7 @@ public function search($input, $indicatorID = '')

if (count($searchResult) > 0)
{

$empUID_list = '';
foreach ($searchResult as $employee)
{
Expand Down Expand Up @@ -1477,7 +1613,7 @@ public function search($input, $indicatorID = '')
}
$finalResult[$currEmpUID]['data'] = $this->getAllData($currEmpUID);
}

// attach all the assigned positions
foreach ($result as $employeeData){
$finalResult[$employeeData['empUID']]['positionData'][] = $employeeData;
Expand Down
Loading