From eb4570853c6a0c3ea9d0f6692187fec73cda510d Mon Sep 17 00:00:00 2001 From: Carrie Hanscom Date: Wed, 16 Aug 2023 08:13:25 -0400 Subject: [PATCH 01/22] LEAF-3939 add class name --- LEAF_Request_Portal/admin/templates/main.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LEAF_Request_Portal/admin/templates/main.tpl b/LEAF_Request_Portal/admin/templates/main.tpl index 9d974777e..bf49e5c29 100644 --- a/LEAF_Request_Portal/admin/templates/main.tpl +++ b/LEAF_Request_Portal/admin/templates/main.tpl @@ -56,7 +56,7 @@
-

+

 Do not enter PHI/PII

From 1726bcaecf7860f7d48c6ec22823cba10344e43d Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Wed, 16 Aug 2023 10:04:20 -0400 Subject: [PATCH 02/22] Leaf 3902 - Update groups, both service and user --- LEAF_Nexus/sources/Employee.php | 2 +- LEAF_Nexus/sources/Tag.php | 22 + .../admin/templates/mod_groups.tpl | 32 +- .../admin/templates/mod_svcChief.tpl | 163 +- .../api/controllers/GroupController.php | 4 + .../api/controllers/ServiceController.php | 13 + .../api/controllers/SystemController.php | 10 +- LEAF_Request_Portal/scripts/sync_services.php | 2 +- LEAF_Request_Portal/sources/Group.php | 111 +- LEAF_Request_Portal/sources/Service.php | 273 +++- LEAF_Request_Portal/sources/System.php | 1450 +++++++---------- .../Update_RMC_DB_2023072000-2023082400.sql | 32 + 12 files changed, 1099 insertions(+), 1015 deletions(-) create mode 100644 docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023072000-2023082400.sql diff --git a/LEAF_Nexus/sources/Employee.php b/LEAF_Nexus/sources/Employee.php index 9834df4fd..430a8ef2f 100644 --- a/LEAF_Nexus/sources/Employee.php +++ b/LEAF_Nexus/sources/Employee.php @@ -709,7 +709,7 @@ public function importFromNational($userName) $cacheHash = "lookupLogin{$userName}"; unset($this->cache[$cacheHash]); - $db_nat = new \Leaf\Db(DIRECTORY_HOST, DIRECTORY_USER, DIRECTORY_PASS, DIRECTORY_DB); + $db_nat = new Db(DIRECTORY_HOST, DIRECTORY_USER, DIRECTORY_PASS, DIRECTORY_DB); $login_nat = new Login($db_nat, $db_nat); $natEmployee = new NationalEmployee($db_nat, $login_nat); diff --git a/LEAF_Nexus/sources/Tag.php b/LEAF_Nexus/sources/Tag.php index 0caa7522a..f841e3124 100644 --- a/LEAF_Nexus/sources/Tag.php +++ b/LEAF_Nexus/sources/Tag.php @@ -111,4 +111,26 @@ public function getAll() return $res; } + + /** + * @param int $id + * @param string $tag + * + * @return array + * + * Created at: 8/16/2023, 8:57:05 AM (America/New_York) + */ + public function groupIsTagged(int $id, string $tag): array + { + $vars = array(':groupID' => $id, + ':tag' => $tag); + $sql = 'SELECT `groupID`, `tag` + FROM `group_tags` + WHERE `groupID` = :groupID + AND `tag` = :tag'; + + $return_value = $this->db->pdo_select_query($sql, $vars); + + return $return_value; + } } \ No newline at end of file diff --git a/LEAF_Request_Portal/admin/templates/mod_groups.tpl b/LEAF_Request_Portal/admin/templates/mod_groups.tpl index d5693607b..69a63fe94 100644 --- a/LEAF_Request_Portal/admin/templates/mod_groups.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_groups.tpl @@ -353,6 +353,18 @@ function pruneMember(groupID, userID) { }); } +function reactivateMember(groupID, userID) { + $.ajax({ + type: 'POST', + url: "../api/group/" + groupID + "/members/_" + userID + "/reactivate", + data: {'CSRFToken': ''}, + fail: function(err) { + console.log(err); + }, + cache: false + }); +} + function addNexusMember(groupID, empUID) { $.ajax({ type: 'POST', @@ -385,9 +397,8 @@ function toTitleCase(str) { function addAdmin(userID) { $.ajax({ type: 'POST', - url: "ajaxIndex.php?a=add_user", + url: "../api/group/" + 1 + "/members", data: {'userID': userID, - 'groupID': 1, 'CSRFToken': ''}, success: function(response) { getMembers(1); @@ -515,7 +526,12 @@ function getGroupList() { actions += ''; employee_table += `${employeeName}${employeeUserName}${backups}${isLocal}${isRegional}${actions}`; } else { - let pruneMemberButton = ``; + let pruneMemberButton = ''; + if (res[i].regionallyManaged === false) { + pruneMemberButton = ``; + } else { + pruneMemberButton = ``; + } let actions = `${pruneMemberButton}`; actions += ''; inactive_table += `${employeeName}${employeeUserName}${backups}${isLocal}${isRegional}${actions}`; @@ -570,6 +586,16 @@ function getGroupList() { }); dialog_confirm.show(); }); + + $('#reActivateMember_' + counter).on('click', function () { + dialog_confirm.setContent('Are you sure you want to Reactivate this member?'); + dialog_confirm.setSaveHandler(function () { + reactivateMember(groupID, res[i].userName); + dialog_confirm.hide(); + dialog.hide(); + }); + dialog_confirm.show(); + }); } counter++; } diff --git a/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl b/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl index 5a58e9eb8..d0bcd87ac 100644 --- a/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl @@ -124,7 +124,7 @@ function getMembers(groupID = -1) { $.ajax({ type: 'GET', url: '../api/system/updateService/' + groupID, - success: function() { + success: function(res) { $.ajax({ url: "../api/service/" + groupID + "/members", dataType: "json", @@ -155,13 +155,13 @@ function populateMembers(groupID = -1, members = []) { $('#members' + groupID).html(''); let memberCt = -1; for (let i in members) { - if (members[i].active == 1 && members[i].backupID == null) { + if (members[i].active == 1 && members[i].backupID == '') { memberCt++; } } let countTxt = (memberCt > 0) ? (' + ' + memberCt + ' others') : ''; for (let i in members) { - if (members[i].active == 1 && members[i].backupID == null) { + if (members[i].active == 1 && members[i].backupID == '') { if ($('#members' + groupID).html('')) { $('#members' + groupID).append('
' + toTitleCase(members[i].Fname) + ' ' + toTitleCase(members[i].Lname) + countTxt + '
'); } @@ -175,8 +175,8 @@ function populateMembers(groupID = -1, members = []) { * @param {int} groupID - ID of group * @param {int} userID - ID of user being added */ -function addUser(groupID = -1, userID = -1) { - if (groupID < 0 || userID < 0) { +function addUser(groupID = -1, userID = '') { + if (groupID < 0 || userID == '') { return; } else { $.ajax({ @@ -194,14 +194,17 @@ function addUser(groupID = -1, userID = -1) { * @param {int} groupID - ID of group * @param {int} userID - ID of user being removed */ -function removeUser(groupID = -1, userID = -1) { - if (groupID < 0 || userID < 0) { +function removeUser(groupID = -1, userID = '') { + if (groupID < 0 || userID == '') { return; } else { $.ajax({ - type: 'DELETE', - url: "../api/service/" + groupID + "/members/_" + userID + '?' + - $.param({'CSRFToken': ''}), + type: 'POST', + url: "../api/service/" + groupID + "/members/_" + userID, + data: {'CSRFToken': ''}, + fail: function(err) { + console.log(err); + }, cache: false }); } @@ -219,15 +222,15 @@ function importUser(serviceID = 0, selectedUserName = '') { if (selectedUserName === '') { console.log('Invalid username'); } + $.ajax({ type: 'POST', url: '/api/employee/import/_' + selectedUserName, data: {CSRFToken: ''}, success: function(res) { - if(!isNaN(res)) { + if (!isNaN(res)) { addUser(serviceID, selectedUserName); // add identified user into portal. - } - else { + } else { alert(res); } }, @@ -282,28 +285,101 @@ function initiateModal(serviceID = 0, serviceName = '') { dialog.setContent( '
' + '

' + serviceName + '

Add Employee


'); - $('#employees').html('
'); + + $('#employees').html('

'); + let employee_table = '
'; + let inactive_table = '
NameUsernameBackupsLocalActions
'; + let counter = 0; for(let i in res) { - // Check for active members to list - if (res[i].active == 1) { - if (res[i].backupID == null) { - let removeButton = '- REMOVE'; - $('#employee_table').append(''); - // Check for Backups - for (let j in res) { - if (res[i].userName == res[j].backupID) { - $('#employee_table').append('
• ' + toTitleCase(res[j].Fname) + ' ' + toTitleCase(res[j].Lname) + ' - Backup for ' + toTitleCase(res[i].Fname) + ' ' + toTitleCase(res[i].Lname) + '
'); - } + if (res[i].backupID == '') { + let employeeName = ``; + let employeeUserName = ``; + let backups = ``; + let isRegional = ``; + let removeButton = `'; + + if (res[i].active === 1) { + let actions = `${removeButton}`; + + actions += ''; + employee_table += `${employeeName}${employeeUserName}${backups}${isLocal}${actions}`; + } else { + let pruneMemberButton = ''; + + if (res[i].locallyManaged == 1) { + pruneMemberButton = `'; + inactive_table += `${employeeName}${employeeUserName}${backups}${isLocal}${actions}`; + } + counter++; + } + } + employee_table += '
NameUsernameBackupsLocalActions
`; + let isLocal = `${res[i].locallyManaged > 0 ? '' : ''}${res[i].regionallyManaged ? '' : ''}`; + + // Check for Backups + for (let j in res) { + if (res[i].userName == res[j].backupID) { + backups += ('
' + toTitleCase(res[j].Fname) + ' ' + toTitleCase(res[j].Lname) + '\n'); } - $('#removeMember_' + counter).on('click', function (userID) { - return function () { - removeUser(serviceID, userID); + } + // close of actions and backups column + backups += '
`; + } else { + pruneMemberButton = ``; + } + + let actions = `${pruneMemberButton}`; + actions += '
'; + inactive_table += ''; + // generate formatted table + $('#employee_table').html(employee_table); + $('#inactive_table').html(inactive_table); + + if ($('#inactive_table > .table-bordered > tbody > tr').length === null || $('#inactive_table > .table-bordered > tbody > tr').length === 0){ + $('#showInactive').hide(); + } else { + $('#showInactive').on('click', function () { + $('#showInactive').toggleClass("fa-angle-right fa-angle-down"); + $('#inactive_table').slideToggle(); + }); + } + + // add functionality to action buttons after table generation + counter = 0; + for (let i in res) { + if (res[i].backupID == "") { + if (res[i].active === 1) { + $('#removeMember_' + counter).on('click', function () { + dialog_confirm.setContent('Are you sure you want to remove this member?'); + dialog_confirm.setSaveHandler(function () { + removeUser(serviceID, res[i].userName); + dialog_confirm.hide(); dialog.hide(); - }; - }(res[i].userName)); - counter++; + }); + dialog_confirm.show(); + }); + } else { + $('#pruneMember_' + counter).on('click', function () { + dialog_confirm.setContent('Are you sure you want to prune this member?'); + dialog_confirm.setSaveHandler(function () { + pruneMember(serviceID, res[i].userName); + dialog_confirm.hide(); + dialog.hide(); + }); + dialog_confirm.show(); + }); + $('#reactivateMember_' + counter).on('click', function () { + dialog_confirm.setContent('Are you sure you want to reactivate this member?'); + dialog_confirm.setSaveHandler(function () { + reactivateMember(serviceID, res[i].userName); + dialog_confirm.hide(); + dialog.hide(); + }); + dialog_confirm.show(); + }); } + counter++; } } @@ -342,6 +418,31 @@ function initiateModal(serviceID = 0, serviceName = '') { } } +function pruneMember(groupID, userID) { + console.log('pruneMember'); + $.ajax({ + type: 'POST', + url: "../api/service/" + groupID + "/members/_" + userID + "/prune", + data: {'CSRFToken': ''}, + fail: function(err) { + console.log(err); + }, + cache: false + }); +} + +function reactivateMember(groupID, userID) { + $.ajax({ + type: 'POST', + url: "../api/service/" + groupID + "/members/_" + userID + "/reactivate", + data: {'CSRFToken': ''}, + fail: function(err) { + console.log(err); + }, + cache: false + }); +} + /** * Initiate widgets for each service * @param {int} serviceID - ID for service being represented @@ -383,7 +484,7 @@ function getGroupList() { $('#groupList').append('

'+ toTitleCase(quadrads[i].name) +'

'); } for(let i in services) { - $('#group_' + services[i].groupID).append('
' + $('#group_' + services[i].groupID).append('
' + '

'+ services[i].service +'

' + '
' + '
'); diff --git a/LEAF_Request_Portal/api/controllers/GroupController.php b/LEAF_Request_Portal/api/controllers/GroupController.php index a8bdf2584..5c6119692 100644 --- a/LEAF_Request_Portal/api/controllers/GroupController.php +++ b/LEAF_Request_Portal/api/controllers/GroupController.php @@ -90,6 +90,10 @@ public function post($act) return $group->importGroup(\Leaf\XSSHelpers::sanitizeHTML($_POST['title'])); // POST for title of group }); + $this->index['POST']->register('group/[digit]/members/[text]/reactivate', function ($args) use ($group) { + return $group->reActivateMember($args[1], $args[0]); + }); + $this->index['POST']->register('group/[digit]/members/[text]/prune', function ($args) use ($group) { return $group->removeMember($args[1], $args[0]); }); diff --git a/LEAF_Request_Portal/api/controllers/ServiceController.php b/LEAF_Request_Portal/api/controllers/ServiceController.php index ca9b24391..fd94e9d13 100644 --- a/LEAF_Request_Portal/api/controllers/ServiceController.php +++ b/LEAF_Request_Portal/api/controllers/ServiceController.php @@ -70,6 +70,19 @@ public function post($act) return $service->addMember($args[0], $_POST['userID']); }); + $this->index['POST']->register('service/[digit]/members/[text]', function ($args) use ($service) { + return $service->deactivateChief($args[0], $args[1]); + }); + + $this->index['POST']->register('service/[digit]/members/[text]/reactivate', function ($args) use ($service) { + + return $service->reactivateChief($args[1], $args[0]); + }); + + $this->index['POST']->register('service/[digit]/members/[text]/prune', function ($args) use ($service) { + return $service->pruneChief($args[0], $args[1]); + }); + return $this->index['POST']->runControl($act['key'], $act['args']); } } diff --git a/LEAF_Request_Portal/api/controllers/SystemController.php b/LEAF_Request_Portal/api/controllers/SystemController.php index e80e05605..3eea67cd6 100644 --- a/LEAF_Request_Portal/api/controllers/SystemController.php +++ b/LEAF_Request_Portal/api/controllers/SystemController.php @@ -41,7 +41,15 @@ public function get($act) }); $this->index['GET']->register('system/updateService/[digit]', function ($args) use ($system) { - return $system->updateService($args[0]); + $updated_service = $system->updateService($args[0]); + + if ($updated_service['status']['code'] == 4) { + $return_value = $updated_service['status']['message']; + } else { + $return_value = "groupID: " . $args[0] . " updated"; + } + + return $return_value; }); $this->index['GET']->register('system/updateGroup/[digit]', function ($args) use ($system) { diff --git a/LEAF_Request_Portal/scripts/sync_services.php b/LEAF_Request_Portal/scripts/sync_services.php index 76cd1cc15..ecefc4770 100644 --- a/LEAF_Request_Portal/scripts/sync_services.php +++ b/LEAF_Request_Portal/scripts/sync_services.php @@ -19,6 +19,6 @@ $group_portal = new Portal\Group($db, $login); $service_portal = new Portal\Service($db, $login); $system_portal = new Portal\System($db, $login); -$syncing = $system_portal->syncSystem($group_portal, $service_portal, $group, $employee, $tag, $position); +$syncing = $system_portal->syncSystem($group); echo $syncing; \ No newline at end of file diff --git a/LEAF_Request_Portal/sources/Group.php b/LEAF_Request_Portal/sources/Group.php index d48e81b94..f40afdfc2 100644 --- a/LEAF_Request_Portal/sources/Group.php +++ b/LEAF_Request_Portal/sources/Group.php @@ -234,11 +234,7 @@ public function getMembers(int $groupID, bool $searchDeleted = false, bool $all $dirRes[0]['primary_admin'] = $member['primary_admin']; } - if ($member['locallyManaged'] == 1) { - $dirRes[0]['backupID'] = ""; - } else { - $dirRes[0]['backupID'] = $member['backupID']; - } + $dirRes[0]['backupID'] = $member['backupID']; $dirRes[0]['locallyManaged'] = $member['locallyManaged']; $dirRes[0]['active'] = $member['active']; @@ -298,36 +294,14 @@ public function addMember(string $member, int $groupID): array if (!empty($backups)) { foreach ($backups as $backup) { $vars = array(':userID' => $backup['userName'], - ':groupID' => $groupID); - $sql = 'SELECT `locallyManaged` - FROM `users` - WHERE `userID` = :userID - AND `groupID` = :groupID'; - - $res = $this->db->pdo_select_query($sql, $vars); - - if ($res['status']['code'] == 2) { - // Check for locallyManaged users - if ($res['data'][0]['locallyManaged'] == 1) { - $vars[':backupID'] = ''; - } else { - $vars[':backupID'] = $emp[0]['userName']; - } - $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`) - VALUES (:userID, :groupID, :backupID) - ON DUPLICATE KEY UPDATE `userID` = :userID, `groupID` = :groupID, - `backupID` = :backupID'; - - $return_value = $this->db->pdo_insert_query($sql, $vars); - } else { - $return_value = array ( - 'status' => array ( - 'code' => 4, - 'message' => 'Backup could not be found' - ) - ); - break; - } + ':groupID' => $groupID, + ':backupID' => $emp[0]['userName']); + $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`) + VALUES (:userID, :groupID, :backupID) + ON DUPLICATE KEY UPDATE `userID` = :userID, + `groupID` = :groupID, `backupID` = :backupID'; + + $return_value = $this->db->pdo_insert_query($sql, $vars); } $return_value = array ( @@ -388,12 +362,9 @@ public function importUser(string $userID, int $groupID, string $backupID): arra */ public function deactivateMember($member, $groupID): void { - $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); - $employee = new \Orgchart\Employee($oc_db, $this->login); - if (is_numeric($groupID) && $member != '') { - $sql_vars = array(':userID' => $member, + $vars = array(':userID' => $member, ':groupID' => $groupID, ); $this->dataActionLogger->logAction(\Leaf\DataActions::MODIFY, \Leaf\LoggableTypes::EMPLOYEE, [ @@ -401,24 +372,19 @@ public function deactivateMember($member, $groupID): void new \Leaf\LogItem("users", "groupID", $groupID, $this->getGroupName($groupID)) ]); - $this->db->prepared_query('UPDATE users SET active = 0, locallyManaged = 1 WHERE userID=:userID AND groupID=:groupID', $sql_vars); + $sql = 'UPDATE `users` + SET `active` = 0 + WHERE `userID` = :userID + AND `groupID` = :groupID'; - // include the backups of employee + $this->db->prepared_query($sql, $vars); - $emp = $employee->lookupLogin($member); - $backups = $employee->getBackups($emp[0]['empUID']); - foreach ($backups as $backup) { - $sql_vars = array(':userID' => $backup['userName'], - ':groupID' => $groupID, - ':backupID' => $member,); + $sql = 'UPDATE `users` + SET `active` = 0 + WHERE `backupID` = :userID + AND `groupID` = :groupID'; - $res = $this->db->prepared_query('SELECT locallyManaged FROM users WHERE userID=:userID AND groupID=:groupID AND backupID=:backupID', $sql_vars); - - // Check for locallyManaged users - if ($res[0]['locallyManaged'] == 0) { - $this->db->prepared_query('DELETE FROM users WHERE userID=:userID AND groupID=:groupID AND backupID=:backupID', $sql_vars); - } - } + $this->db->prepared_query($sql, $vars); } } @@ -463,6 +429,43 @@ public function removeMember($member, $groupID): void } } + /** + * @param string $member + * @param int $groupID + * + * @return array + * + * Created at: 8/16/2023, 8:55:54 AM (America/New_York) + */ + public function reActivateMember(string $member, int $groupID): array + { + if (is_numeric($groupID) && $member != '') { + $this->dataActionLogger->logAction(\Leaf\DataActions::ADD, \Leaf\LoggableTypes::EMPLOYEE, [ + new \Leaf\LogItem("users", "userID", $member, $this->getEmployeeDisplay($member)), + new \Leaf\LogItem("users", "groupID", $groupID, $this->getGroupName($groupID)) + ]); + + $sql_vars = array(':userID' => $member, + ':groupID' => $groupID); + $sql = 'UPDATE `users` + SET `active` = 1 + WHERE `groupID` = :groupID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $return_value = $this->db->pdo_update_query($sql, $sql_vars); + } else { + $return_value = array ( + 'status' => array ( + 'code' => 4, + 'message' => 'Improperly formatted data' + ) + ); + } + + return $return_value; + } + /** * exclude: 0 (no group), 24, (everyone), 16 (service chief) * diff --git a/LEAF_Request_Portal/sources/Service.php b/LEAF_Request_Portal/sources/Service.php index a9a88f174..7ba59c1b3 100644 --- a/LEAF_Request_Portal/sources/Service.php +++ b/LEAF_Request_Portal/sources/Service.php @@ -144,19 +144,29 @@ public function removeSyncService(int $groupID): bool return true; } + /** + * @param int $groupID + * @param string $member + * + * @return bool + * + * Created at: 8/16/2023, 8:45:10 AM (America/New_York) + */ public function addMember($groupID, $member) { $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); $employee = new \Orgchart\Employee($oc_db, $this->login); if (is_numeric($groupID) && $member != '') { - $sql_vars = array(':userID' => $member, - ':serviceID' => $groupID,); + $vars = array(':userID' => $member, + ':serviceID' => $groupID); + $sql = 'INSERT INTO `service_chiefs` ( + `serviceID`, `userID`, `backupID`, `locallyManaged`, `active`) + VALUES (:serviceID, :userID, "", 1, 1) + ON DUPLICATE KEY UPDATE `locallyManaged` = 1, `active` = 1'; // Update on duplicate keys - $this->db->prepared_query('INSERT INTO service_chiefs (serviceID, userID, backupID, locallyManaged, active) - VALUES (:serviceID, :userID, null, 1, 1) - ON DUPLICATE KEY UPDATE serviceID=:serviceID, userID=:userID, backupID=null, locallyManaged=1, active=1', $sql_vars); + $this->db->prepared_query($sql, $vars); $this->dataActionLogger->logAction(\Leaf\DataActions::ADD, \Leaf\LoggableTypes::SERVICE_CHIEF, [ new \Leaf\LogItem("service_chiefs","serviceID", $groupID, $this->getServiceName($groupID)), @@ -165,38 +175,44 @@ public function addMember($groupID, $member) ]); // check if this service is also an ELT - $sql_vars = array(':groupID' => $groupID); - $res = $this->db->prepared_query('SELECT * FROM services - WHERE serviceID=:groupID', $sql_vars); - // if so, update groups table - if ($res[0]['groupID'] == $groupID) - { - $sql_vars = array(':userID' => $member, - ':groupID' => $groupID, ); - $this->db->prepared_query('INSERT INTO users (userID, groupID, backupID) - VALUES (:userID, :groupID, "")', $sql_vars); + $vars = array(':groupID' => $groupID); + $sql = 'SELECT `groupID` + FROM `services` + WHERE `serviceID` = :groupID'; + $res = $this->db->prepared_query($sql, $vars); + + + if ($res[0]['groupID'] == $groupID) { + $vars = array(':userID' => $member, + ':serviceID' => $groupID); + $sql = 'INSERT INTO `users` ( + `groupID`, `userID`, `backupID`, `locallyManaged`, `active`) + VALUES (:serviceID, :userID, "", 1, 1) + ON DUPLICATE KEY UPDATE `locallyManaged` = 1, `active` = 1'; + + $this->db->prepared_query($sql, $vars); } // include the backups of employees $emp = $employee->lookupLogin($member); $backups = $employee->getBackups($emp[0]['empUID']); foreach ($backups as $backup) { - $sql_vars = array(':userID' => $backup['userName'], - ':serviceID' => $groupID, - ':backupID' => $emp[0]['userName'],); - - $res = $this->db->prepared_query('SELECT * FROM service_chiefs WHERE userID=:userID AND serviceID=:serviceID', $sql_vars); - - // Check for locallyManaged users - if ($res[0]['locallyManaged'] == 1) { - $sql_vars[':backupID'] = null; - } else { - $sql_vars[':backupID'] = $emp[0]['userName']; - } - // Add backupID check for updates - $this->db->prepared_query('INSERT INTO service_chiefs (userID, serviceID, backupID) - VALUES (:userID, :serviceID, :backupID) - ON DUPLICATE KEY UPDATE userID=:userID, serviceID=:serviceID, backupID=:backupID', $sql_vars); + $vars = array(':userID' => $backup['userName'], + ':serviceID' => $groupID, + ':backupID' => $emp[0]['userName']); + $sql = 'INSERT INTO `service_chiefs` (`userID`, `serviceID`, `backupID`) + VALUES (:userID, :serviceID, :backupID) + ON DUPLICATE KEY UPDATE `userID` = :userID, `serviceID` = :serviceID, + `backupID` = :backupID'; + + $this->db->prepared_query($sql, $vars); + + $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`) + VALUES (:userID, :serviceID, :backupID) + ON DUPLICATE KEY UPDATE `userID` = :userID, `groupID` = :serviceID, + `backupID` = :backupID'; + + $this->db->prepared_query($sql, $vars); } } @@ -227,7 +243,133 @@ public function importChief(int $serviceID, string $userID, string|null $backupI return $this->db->prepared_query('INSERT INTO service_chiefs (serviceID, userID, backupID, locallyManaged, active) VALUES (:serviceID, :userID, :backupID, 0, 1) - ON DUPLICATE KEY UPDATE serviceID=:serviceID, userID=:userID, backupID=:backupID, locallyManaged=0, active=1', $sql_vars); + ON DUPLICATE KEY UPDATE serviceID=:serviceID, userID=:userID, backupID=:backupID', $sql_vars); + } + + /** + * @param int $groupID + * @param string $member + * + * @return array + * + * Created at: 8/16/2023, 8:46:21 AM (America/New_York) + */ + public function deactivateChief(int $groupID, string $member): array + { + if (is_numeric($groupID) && $member != '') { + $this->dataActionLogger->logAction(\Leaf\DataActions::MODIFY, \Leaf\LoggableTypes::EMPLOYEE, [ + new \Leaf\LogItem("users", "userID", $member, $this->getEmployeeDisplay($member)), + new \Leaf\LogItem("users", "groupID", $groupID, $this->getServiceName($groupID)) + ]); + + $vars = array(':userID' => $member, + ':serviceID' => $groupID, ); + $sql = 'UPDATE `service_chiefs` + SET `active` = 0 + WHERE `serviceID` = :serviceID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $this->db->prepared_query($sql, $vars); + + $sql = 'UPDATE `users` + SET `active` = 0 + WHERE `groupID` = :serviceID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $this->db->prepared_query($sql, $vars); + + $return_value = array( + 'status' => array( + 'code' => 2, + 'message' => 'All processed properly' + ) + ); + } else { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'data formatted incorrectly' + ) + ); + } + + return $return_value; + } + + /** + * @param int $serviceID + * @param string $userName + * + * @return array + * + * Created at: 8/16/2023, 8:46:50 AM (America/New_York) + */ + public function pruneChief(int $serviceID, string $userName): array + { + $this->dataActionLogger->logAction(\Leaf\DataActions::DELETE, \Leaf\LoggableTypes::EMPLOYEE, [ + new \Leaf\LogItem("users", "userID", $userName, $this->getEmployeeDisplay($userName)), + new \Leaf\LogItem("users", "groupID", $serviceID, $this->getServiceName($serviceID)) + ]); + + $vars = array(':serviceID' => $serviceID, + ':userID' => $userName); + $sql = 'DELETE + FROM `service_chiefs` + WHERE `serviceID` = :serviceID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + $sql = 'DELETE + FROM `users` + WHERE `groupID` = :serviceID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + return $return_value; + } + + /** + * @param string $member + * @param int $serviceID + * + * @return array + * + * Created at: 8/16/2023, 8:47:05 AM (America/New_York) + */ + public function reactivateChief(string $member, int $serviceID): array + { + $this->dataActionLogger->logAction(\Leaf\DataActions::ADD, \Leaf\LoggableTypes::EMPLOYEE, [ + new \Leaf\LogItem("users", "userID", $member, $this->getEmployeeDisplay($member)), + new \Leaf\LogItem("users", "groupID", $serviceID, $this->getServiceName($serviceID)) + ]); + + $vars = array(':serviceID' => $serviceID, + ':userID' => $member); + $sql = 'UPDATE `service_chiefs` + SET `active` = 1 + WHERE `serviceID` = :serviceID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $return_value = $this->db->pdo_update_query($sql, $vars); + + $vars = array(':serviceID' => $serviceID, + ':userID' => $member); + $sql = 'UPDATE `users` + SET `active` = 1 + WHERE `groupID` = :serviceID + AND (`userID` = :userID + OR `backupID` = :userID)'; + + $return_value = $this->db->pdo_update_query($sql, $vars); + + return $return_value; } public function removeMember($groupID, $member) @@ -317,62 +459,59 @@ public function removeMember($groupID, $member) * * Created at: 9/14/2022, 11:33:53 AM (America/New_York) */ - public function removeChief(int $serviceID, string $userID, string|null $backupID): array + public function removeChief(int $serviceID, string $userID, string $backupID = ""): array { $this->dataActionLogger->logAction(\Leaf\DataActions::DELETE,\Leaf\LoggableTypes::SERVICE_CHIEF,[ new \Leaf\LogItem("service_chiefs","serviceID", $serviceID, $this->getServiceName($serviceID)), new \Leaf\LogItem("service_chiefs", "userID", $userID, $this->getEmployeeDisplay($userID)) ]); - if ($backupID == NULL){ - $sql_vars = array(':userID' => $userID, - ':serviceID' => $serviceID,); + $vars = array(':userID' => $userID, + ':serviceID' => $serviceID, + ':backupID' => $backupID); + $sql = 'DELETE + FROM `service_chiefs` + WHERE `userID` = :userID + AND `serviceID` = :serviceID + AND `backupID` = :backupID'; - $result = $this->db->prepared_query('DELETE FROM service_chiefs - WHERE userID=:userID - AND serviceID=:serviceID - AND backupID IS NULL', - $sql_vars); - } else { - $sql_vars = array(':userID' => $userID, - ':serviceID' => $serviceID, - ':backupID' => $backupID, ); - - $result = $this->db->prepared_query('DELETE FROM service_chiefs - WHERE userID=:userID - AND serviceID=:serviceID - AND backupID=:backupID', - $sql_vars); - } + $result = $this->db->prepared_query($sql, $vars); return $result; } public function getMembers($groupID) { - if (!is_numeric($groupID)) - { + if (!is_numeric($groupID)) { return; } + $sql_vars = array(':groupID' => $groupID); - $res = $this->db->prepared_query('SELECT * FROM service_chiefs WHERE serviceID=:groupID ORDER BY userID', $sql_vars); + $sql = 'SELECT `userID`, `backupID`, `locallyManaged`, `active` + FROM `service_chiefs` + WHERE `serviceID` = :groupID + ORDER BY `userID`'; + + $res = $this->db->prepared_query($sql, $sql_vars); $members = array(); - if (count($res) > 0) - { - $dir = new VAMC_Directory(); - foreach ($res as $member) - { - $dirRes = $dir->lookupLogin($member['userID']); + $dir = new VAMC_Directory(); + + if (count($res) > 0) { + foreach ($res as $member) { + $dirRes = $dir->lookupLogin($member['userID'], false, true); - if (isset($dirRes[0])) - { + if (isset($dirRes[0])) { $temp = $dirRes[0]; - if($member['locallyManaged'] == 1) { - $temp['backupID'] = null; - } else { - $temp['backupID'] = $member['backupID']; + $temp['regionallyManaged'] = 'no'; + + foreach ($dirRes[0]['groups'] as $group) { + if ($groupID == $group['groupID']) { + $temp['regionallyManaged'] = 'yes'; + } } + + $temp['backupID'] = $member['backupID']; $temp['locallyManaged'] = $member['locallyManaged']; $temp['active'] = $member['active']; $members[] = $temp; diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 173fd77c5..0e516a4d3 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -38,112 +38,105 @@ public function __construct($db, $login) $this->dataActionLogger = new \Leaf\DataActionLogger($db, $login); } - public function updateService($serviceID) + /** + * @param int $serviceID + * + * @return array + * + * Created at: 8/16/2023, 8:27:47 AM (America/New_York) + */ + public function updateService(int $serviceID): array { - if (!is_numeric($serviceID)) - { - return 'Invalid Service'; - } - // clear out old data first - $vars = array(':serviceID' => $serviceID); - $this->db->prepared_query('DELETE FROM services WHERE serviceID=:serviceID AND serviceID > 0', $vars); - //$this->db->prepared_query('DELETE FROM service_chiefs WHERE serviceID=:serviceID AND locallyManaged != 1', $vars); // Skip Local + if (!is_numeric($serviceID)) { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Invalid Service Id.' + ) + ); + } else { + $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); + $group = new \Orgchart\Group($oc_db, $this->login); + $position = new \Orgchart\Position($oc_db, $this->login); + $tag = new \Orgchart\Tag($oc_db, $this->login); - $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); - $group = new \Orgchart\Group($oc_db, $this->login); - $position = new \Orgchart\Position($oc_db, $this->login); - $employee = new \Orgchart\Employee($oc_db, $this->login); - $tag = new \Orgchart\Tag($oc_db, $this->login); - - // find quadrad/ELT tag name, and find groupID - $leader = $position->findRootPositionByGroupTag($group->getGroupLeader($serviceID), $tag->getParent('service')); - $quadID = $leader[0]['groupID']; - - //echo "Synching Service: {$service['groupTitle']}
"; - $service = $group->getGroup($serviceID)[0]; - $abbrService = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; - $vars = array(':serviceID' => $service['groupID'], - ':service' => $service['groupTitle'], - ':abbrService' => $abbrService, - ':groupID' => $quadID, ); - - $this->db->prepared_query('INSERT INTO services (serviceID, service, abbreviatedService, groupID) - VALUES (:serviceID, :service, :abbrService, :groupID)', $vars); - - $leaderGroupID = $group->getGroupLeader($service['groupID']); - $resEmp = $position->getEmployees($leaderGroupID); - foreach ($resEmp as $emp) - { - if ($emp['userName'] != '') - { - $vars = array(':userID' => $emp['userName'], - ':serviceID' => $service['groupID'], ); - - $this->db->prepared_query('INSERT INTO service_chiefs (serviceID, userID, active) - VALUES (:serviceID, :userID, 0) - ON DUPLICATE KEY UPDATE serviceID=:serviceID, userID=:userID', $vars); - - // include the backups of employees - $res = $this->db->prepared_query('SELECT * FROM service_chiefs WHERE userID=:userID AND serviceID=:serviceID', $vars); - if ($res[0]['active'] == 1) { - $backups = $employee->getBackups($emp['empUID']); - foreach ($backups as $backup) { - $vars = array(':userID' => $backup['userName'], - ':serviceID' => $service['groupID'], - ':backupID' => $emp['userName'],); - - // Add backupID check for updates - $this->db->prepared_query('INSERT INTO service_chiefs (userID, serviceID, backupID) - VALUES (:userID, :serviceID, :backupID) - ON DUPLICATE KEY UPDATE userID=:userID, serviceID=:groupID', $vars); - } - } - } - } + $leader_id = $group->getGroupLeader($serviceID); + $tag_parent = $tag->getParent('service'); - // check if this service is also an ELT - // if so, update groups table - if ($serviceID == $quadID) - { - $vars = array(':groupID' => $quadID); + $leader = $position->findRootPositionByGroupTag($leader_id, $tag_parent); - $this->db->prepared_query('DELETE FROM users WHERE groupID=:groupID', $vars); + $quadID = $leader[0]['groupID']; - $resChief = $this->db->prepared_query('SELECT * FROM service_chiefs - WHERE serviceID=:groupID - AND active=1', $vars); - foreach ($resChief as $chief) - { - $vars = array(':userID' => $chief['userID'], - ':groupID' => $quadID, ); - $this->db->prepared_query('INSERT INTO users (userID, groupID, backupID) - VALUES (:userID, :groupID, "")', $vars); - } - } + $service = $group->getGroup($serviceID)[0]; + + $abbrService = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; + + $insert_service = $this->insertService($service['groupID'], $service['groupTitle'], $abbrService, $quadID); - //refresh request portal members backups - $vars = array(':serviceID' => $service['groupID'],); + if ($insert_service['status']['code'] == 2) { + $delete_chief_backups = $this->deleteChiefs($service['groupID']); - $resRP = $this->db->prepared_query('SELECT * FROM service_chiefs WHERE serviceID=:serviceID', $vars); + if ($delete_chief_backups['status']['code'] == 2) { + $leaderGroupID = $group->getGroupLeader($service['groupID']); - foreach ($resRP as $empRP) { - if ($empRP['active'] == 1) { - $empID = $employee->lookupLogin($empRP['userID']); - $backups = $employee->getBackups($empID[0]['empUID']); - foreach ($backups as $backup) { - $vars = array(':userID' => $backup['userName'], - ':serviceID' => $service['groupID'], - ':backupID' => $empRP['userID'],); + $resEmp = $position->getEmployees($leaderGroupID); - // Add backupID check for updates - $this->db->prepared_query('INSERT INTO service_chiefs (userID, serviceID, backupID) - VALUES (:userID, :serviceID, :backupID) - ON DUPLICATE KEY UPDATE userID=:userID, serviceID=:serviceID, backupID=:backupID', $vars); + $return_value = array( + 'status' => array( + 'code' => 2, + 'message' => '' + ) + ); + + foreach ($resEmp as $emp) { + if ($emp['userName'] != '') { + $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); + + if ($insert_chief['status']['code'] == 2) { + // nothing to do here, just keep going + } else { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Chief unable to be added' + ) + ); + + break; + } + } + } + + if ($return_value['status']['code'] == 2) { + $backups = $this->addBackups($service['groupID'], false); + + if ($backups['status']['code'] == 2) { + // check if this service is also an ELT + // if so, update groups table + $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); + error_log(print_r($serviceID, true)); + error_log(print_r($quadID, true)); + error_log(print_r($tagged, true)); + + if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { + $this->updateGroup($serviceID); + } else { + // make sure this is not in the groups table? + $this->removeGroup($serviceID); + } + } else { + $return_value = $backups; + } + } + } else { + $return_value = $delete_chief_backups; } + } else { + $return_value = $insert_service; } } - return "groupID: {$serviceID} updated"; + return $return_value; } /** @@ -173,114 +166,99 @@ public function updateGroup(int $groupID): array $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); $group = new \Orgchart\Group($oc_db, $this->login); $position = new \Orgchart\Position($oc_db, $this->login); - $employee = new \Orgchart\Employee($oc_db, $this->login); $tag = new \Orgchart\Tag($oc_db, $this->login); - // clear out old data first - //$delete_groups = $this->clearGroups($groupID); - - //if ($delete_groups['status']['code'] == 2) { - // find quadrad/ELT tag name - $upperLevelTag = $tag->getParent('service'); - $isQuadrad = false; - - if (array_search($upperLevelTag, $group->getAllTags($groupID)) !== false) { - $isQuadrad = true; - } + $upperLevelTag = $tag->getParent('service'); + $isQuadrad = false; - $resGroup = $group->getGroup($groupID)[0]; + if (array_search($upperLevelTag, $group->getAllTags($groupID)) !== false) { + $isQuadrad = true; + } - $insert_group = $this->insertGroup($groupID, $isQuadrad, $resGroup['groupTitle']); + $resGroup = $group->getGroup($groupID)[0]; - if ($insert_group['status']['code'] == 2) { - $delete_user_backups = $this->deleteUsers($groupID); + $insert_group = $this->insertGroup($groupID, $isQuadrad, $resGroup['groupTitle']); - if ($delete_user_backups['status']['code'] == 2) { - $resEmp = array(); - $positions = $group->listGroupPositions($groupID); - $resEmp = $group->listGroupEmployees($groupID); + if ($insert_group['status']['code'] == 2) { + $delete_user_backups = $this->deleteUsers($groupID); - if (!empty($positions) && is_array($positions)){ - foreach ($positions as $tposition) { - $resEmp = array_merge($resEmp, $position->getEmployees($tposition['positionID'])); - } - } + if ($delete_user_backups['status']['code'] == 2) { + $resEmp = array(); + $positions = $group->listGroupPositions($groupID); + $resEmp = $group->listGroupEmployees($groupID); - if (!empty($resEmp) && is_array($resEmp)) { - foreach ($resEmp as $emp) { - $insert_user = $this->insertUser($groupID, $emp); - - if ($insert_user['status']['code'] == 2) { - // nothing to be done, all is good - } else { - $return_value = array ( - 'status' => array ( - 'code' => 4, - 'message' => 'Action failed to add users.' - ) - ); - break; - } - } + if (!empty($positions) && is_array($positions)){ + foreach ($positions as $tposition) { + $resEmp = array_merge($resEmp, $position->getEmployees($tposition['positionID'])); } + } - $backups = $this->addBackups($groupID); - - if ($backups['status']['code'] == 2) { - $privs = $this->updateCatPrivs($groupID); + if (!empty($resEmp) && is_array($resEmp)) { + foreach ($resEmp as $emp) { + $insert_user = $this->insertUser($groupID, $emp); - if ($privs['status']['code'] == 2) { - // at this point everything updated as expected - $return_value = array ( - 'status' => array ( - 'code' => 2, - 'message' => 'Everything updated as expected.' - ) - ); + if ($insert_user['status']['code'] == 2) { + // nothing to be done, all is good } else { - // something happened updating category privs $return_value = array ( 'status' => array ( 'code' => 4, - 'message' => 'There was an error updating category privs.' + 'message' => 'Action failed to add users.' ) ); + break; } + } + } + + $backups = $this->addBackups($groupID); + + if ($backups['status']['code'] == 2) { + $privs = $this->updateCatPrivs($groupID); + + if ($privs['status']['code'] == 2) { + // at this point everything updated as expected + $return_value = array ( + 'status' => array ( + 'code' => 2, + 'message' => 'Everything updated as expected.' + ) + ); } else { + // something happened updating category privs $return_value = array ( 'status' => array ( 'code' => 4, - 'message' => 'There was an arror adding backups.' + 'message' => 'There was an error updating category privs.' ) ); } } else { - // something happened deleting user backups $return_value = array ( 'status' => array ( 'code' => 4, - 'message' => 'There was an error deleting user backups.' + 'message' => 'There was an arror adding backups.' ) ); } } else { - // something happened with the inserting of groups + // something happened deleting user backups $return_value = array ( 'status' => array ( 'code' => 4, - 'message' => 'There was an error inserting groups.' + 'message' => 'There was an error deleting user backups.' ) ); } - /* } else { - // something happened with the delete groups + } else { + // something happened with the inserting of groups $return_value = array ( 'status' => array ( 'code' => 4, - 'message' => 'There was an error when deleting groups.' + 'message' => 'There was an error inserting groups.' ) ); - } */ + } } return $return_value; @@ -289,413 +267,153 @@ public function updateGroup(int $groupID): array /** * @param int $groupID * - * @return array - * - * Created at: 6/30/2023, 1:25:07 PM (America/New_York) + * @return string */ - private function updateCatPrivs(int $groupID): array + public function importGroup($groupID): string { - $cat_privs = $this->getCatPrivs($groupID); - - if ($cat_privs['status']['code'] == 2 && !empty($cat_privs['data'])) { - $return_value = $this->deleteCatPrivs($groupID); + if (!is_numeric($groupID)) { + $return_value = 'Invalid Group'; + } else if ($groupID == 1) { + $return_value = 'Cannot update admin group'; } else { - $return_value = array ( - 'status' => array ( - 'code' => 2, - 'message' => 'Nothing to be done with category_privs' - ) - ); + // clear out old data first + $vars = array(':groupID' => $groupID); + //$this->db->prepared_query('DELETE FROM users WHERE groupID=:groupID AND backupID IS NULL', $vars); + $this->db->prepared_query('DELETE FROM `groups` WHERE groupID=:groupID', $vars); + + $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); + $group = new \Orgchart\Group($oc_db, $this->login); + $position = new \Orgchart\Position($oc_db, $this->login); + $employee = new \Orgchart\Employee($oc_db, $this->login); + $tag = new \Orgchart\Tag($oc_db, $this->login); + + // find quadrad/ELT tag name + $upperLevelTag = $tag->getParent('service'); + $isQuadrad = false; + if (array_search($upperLevelTag, $group->getAllTags($groupID)) !== false) { + $isQuadrad = true; + } + + $resGroup = $group->getGroup($groupID)[0]; + $vars = array(':groupID' => $groupID, + ':parentGroupID' => ($isQuadrad == true ? -1 : null), + ':name' => $resGroup['groupTitle'], + ':groupDescription' => '',); + + $this->db->prepared_query('INSERT INTO `groups` (groupID, parentGroupID, name, groupDescription) + VALUES (:groupID, :parentGroupID, :name, :groupDescription)', $vars); + + // build list of member employees + $resEmp = array(); + $positions = $group->listGroupPositions($groupID); + $resEmp = $group->listGroupEmployees($groupID); + foreach ($positions as $tposition) { + $resEmp = array_merge($resEmp, $position->getEmployees($tposition['positionID'])); + } + + // clear backups in case of updates + $vars = array(':groupID' => $groupID); + $sql = 'DELETE + FROM `users` + WHERE `backupID` <> "" + AND `groupID` = :groupID'; + + $this->db->prepared_query($sql, $vars); + + foreach ($resEmp as $emp) { + if ($emp['userName'] != '') { + $vars = array(':userID' => $emp['userName'], + ':groupID' => $groupID,); + + $this->db->prepared_query('INSERT INTO users (userID, groupID, backupID) + VALUES (:userID, :groupID, "") + ON DUPLICATE KEY UPDATE userID=:userID, groupID=:groupID', $vars); + + // include the backups of employees + $res = $this->db->prepared_query('SELECT * FROM users WHERE userID=:userID AND groupID=:groupID', $vars); + if ($res[0]['active'] == 1) { + $backups = $employee->getBackups($emp['empUID']); + foreach ($backups as $backup) { + $vars = array(':userID' => $backup['userName'], + ':groupID' => $groupID, + ':backupID' => $emp['userName'],); + + // Add backupID check for updates + $this->db->prepared_query('INSERT INTO users (userID, groupID, backupID) + VALUES (:userID, :groupID, :backupID) + ON DUPLICATE KEY UPDATE userID=:userID, groupID=:groupID', $vars); + } + } + } + } + $return_value = "groupID: {$groupID} imported"; } return $return_value; } - /** - * @param int $groupID - * - * @return array - * - * Created at: 6/30/2023, 1:25:25 PM (America/New_York) - */ - private function deleteCatPrivs(int $groupID): array + public function getServices() { - $vars = array(':groupID' => $groupID); - $sql = 'DELETE - FROM `category_privs` - WHERE `groupID` = :groupID'; - - $return_value = $this->db->pdo_delete_query($sql, $vars); - - return $return_value; + return $this->db->prepared_query('SELECT groupID as parentID, + serviceID as groupID, + service as groupTitle, + abbreviatedService as groupAbbreviation + FROM services + ORDER BY groupTitle ASC', array()); } /** - * @param int $groupID - * - * @return array + * Get the current database version * - * Created at: 6/30/2023, 1:25:38 PM (America/New_York) + * @return string the current database version */ - private function getCatPrivs(int $groupID): array + public function getDatabaseVersion() { - $vars = array(':groupID' => $groupID); - $sql = 'SELECT `categoryID` - FROM `category_privs` - LEFT JOIN `groups` USING (`groupID`) - WHERE `category_privs`.`groupID` = :groupID - AND `groups`.`groupID` IS NULL'; + $version = $this->db->prepared_query('SELECT data FROM settings WHERE setting = "dbVersion"', array()); + if (count($version) > 0 && $version[0]['data'] !== null) + { + return $version[0]['data']; + } - $return_value = $this->db->pdo_select_query($sql, $vars); + return 'unknown'; + } - return $return_value; + public function getGroups() + { + return $this->db->prepared_query('SELECT * FROM `groups` + WHERE groupID > 1 + ORDER BY name ASC', array()); } /** - * @param int $groupID - * * @return array * - * Created at: 6/30/2023, 1:25:53 PM (America/New_York) + * Created at: 7/31/2023, 7:41:43 AM (America/New_York) */ - private function addBackups(int $groupID): array + public function addAction(): array { - $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); - $employee = new \Orgchart\Employee($oc_db, $this->login); + if (!$this->login->checkGroup(1)) { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Admin access required' + ) + ); + } else { + $vars = array(':actionType' => preg_replace('/[^a-zA-Z0-9_]/', '', strip_tags($_POST['actionText']))); + $sql = 'SELECT `deleted` + FROM `actions` + WHERE `actionType` = :actionType'; - // get all users for this group - $group_users = $this->getGroupUsers($groupID); + $res = $this->db->pdo_select_query($sql, $vars); - // loop through group_users to add backups - if ($group_users['status']['code'] == 2){ - $userNames = array(); - - foreach ($group_users['data'] as $user) { - $userNames[] = $user['userID']; - } - - $employee_list = $employee->getEmployeeByUserName($userNames, $oc_db); - foreach ($employee_list['data'] as $user) { - // if active user, then get backups and add them - if ($user['deleted'] == 0) { - $backups = $employee->getBackups($user['empUID']); - - if (!empty($backups)) { - foreach ($backups as $backup) { - $backup_added = $this->addBackup($groupID, $backup['userName'], $user['userName']); - - if ($backup_added['status']['code'] == 2) { - continue; - } else { - $return_value = array ( - 'status' => array ( - 'code' => 4, - 'message' => 'Action failed to add backups.' - ) - ); - break; - } - } - } - } - } - $return_value = array ( - 'status' => array ( - 'code' => 2, - 'message' => '' - ) - ); - } else { - $return_value = $group_users; - } - - return $return_value; - } - - /** - * @param int $groupID - * @param string $backup_user - * @param string $user - * - * @return array - * - * Created at: 6/30/2023, 1:26:30 PM (America/New_York) - */ - private function addBackup(int $groupID, string $backup_user, string $user): array - { - $vars = array(':userID' => $backup_user, - ':groupID' => $groupID, - ':backupID' => $user); - $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`) - VALUES (:userID, :groupID, :backupID) - ON DUPLICATE KEY UPDATE `userID` = :userID, `groupID` = :groupID'; - - $return_value = $this->db->pdo_insert_query($sql, $vars); - - return $return_value; - } - - /** - * @param int $groupID - * - * @return array - * - * Created at: 6/30/2023, 1:26:53 PM (America/New_York) - */ - private function getGroupUsers(int $groupID): array - { - $vars = array(':groupID' => $groupID); - $sql = 'SELECT `userID` - FROM `users` - WHERE `groupID` = :groupID'; - - $return_value = $this->db->pdo_select_query($sql, $vars); - - return $return_value; - } - - /** - * @param int $groupID - * @param array $emp - * - * @return array - * - * Created at: 6/30/2023, 1:27:17 PM (America/New_York) - */ - private function insertUser(int $groupID, array $emp): array - { - if (!empty($emp['userName'])) { - $vars = array(':userID' => $emp['userName'], - ':groupID' => $groupID, ); - $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`, `active`) - VALUES (:userID, :groupID, "", 1) - ON DUPLICATE KEY UPDATE `userID` = :userID, `groupID` = :groupID'; - - $return_value = $this->db->pdo_insert_query($sql, $vars); - } else { - $return_value = array ( - 'status' => array ( - 'code' => 4, - 'message' => 'Improperly formatted data.' - ) - ); - } - - return $return_value; - } - - /** - * @param int $groupID - * - * @return array - * - * Created at: 6/30/2023, 1:27:47 PM (America/New_York) - */ - private function deleteUsers(int $groupID): array - { - $vars = array(':groupID' => $groupID); - $sql = 'DELETE - FROM `users` - WHERE `groupID` = :groupID - AND `locallyManaged` = 0'; - - $return_value = $this->db->pdo_delete_query($sql , $vars); - - return $return_value; - } - - /** - * @param int $groupID - * @param bool $isQuadrad - * @param string $title - * - * @return array - * - * Created at: 6/30/2023, 1:28:03 PM (America/New_York) - */ - private function insertGroup(int $groupID, bool $isQuadrad, string $title): array - { - $vars = array(':groupID' => $groupID, - ':parentGroupID' => ($isQuadrad == true ? -1 : null), - ':name' => $title, - ':groupDescription' => '', ); - $sql = 'INSERT INTO `groups` (`groupID`, `parentGroupID`, `name`, - `groupDescription`) - VALUES (:groupID, :parentGroupID, :name, :groupDescription) - ON DUPLICATE KEY UPDATE `parentGroupID` = :parentGroupID, `name` = :name, - `groupDescription` = :groupDescription'; - - $return_value = $this->db->pdo_insert_query($sql, $vars); - - return $return_value; - } - - /** - * @param int $groupID - * - * @return array - * - * Created at: 6/30/2023, 1:28:34 PM (America/New_York) - */ - private function clearGroups(int $groupID): array - { - $vars = array(':groupID' => $groupID); - $sql = 'DELETE - FROM `groups` - WHERE `groupID` = :groupID'; - - $return_value = $this->db->pdo_delete_query($sql, $vars); - - return $return_value; - } - - /** - * @param int $groupID - * - * @return string - */ - public function importGroup($groupID): string - { - if (!is_numeric($groupID)) { - $return_value = 'Invalid Group'; - } else if ($groupID == 1) { - $return_value = 'Cannot update admin group'; - } else { - // clear out old data first - $vars = array(':groupID' => $groupID); - //$this->db->prepared_query('DELETE FROM users WHERE groupID=:groupID AND backupID IS NULL', $vars); - $this->db->prepared_query('DELETE FROM `groups` WHERE groupID=:groupID', $vars); - - $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); - $group = new \Orgchart\Group($oc_db, $this->login); - $position = new \Orgchart\Position($oc_db, $this->login); - $employee = new \Orgchart\Employee($oc_db, $this->login); - $tag = new \Orgchart\Tag($oc_db, $this->login); - - // find quadrad/ELT tag name - $upperLevelTag = $tag->getParent('service'); - $isQuadrad = false; - if (array_search($upperLevelTag, $group->getAllTags($groupID)) !== false) { - $isQuadrad = true; - } - - $resGroup = $group->getGroup($groupID)[0]; - $vars = array(':groupID' => $groupID, - ':parentGroupID' => ($isQuadrad == true ? -1 : null), - ':name' => $resGroup['groupTitle'], - ':groupDescription' => '',); - - $this->db->prepared_query('INSERT INTO `groups` (groupID, parentGroupID, name, groupDescription) - VALUES (:groupID, :parentGroupID, :name, :groupDescription)', $vars); - - // build list of member employees - $resEmp = array(); - $positions = $group->listGroupPositions($groupID); - $resEmp = $group->listGroupEmployees($groupID); - foreach ($positions as $tposition) { - $resEmp = array_merge($resEmp, $position->getEmployees($tposition['positionID'])); - } - - // clear backups in case of updates - $vars = array(':groupID' => $groupID); - $this->db->prepared_query('DELETE FROM users WHERE backupID IS NOT NULL AND groupID=:groupID', $vars); - foreach ($resEmp as $emp) { - if ($emp['userName'] != '') { - $vars = array(':userID' => $emp['userName'], - ':groupID' => $groupID,); - - $this->db->prepared_query('INSERT INTO users (userID, groupID, backupID) - VALUES (:userID, :groupID, "") - ON DUPLICATE KEY UPDATE userID=:userID, groupID=:groupID', $vars); - - // include the backups of employees - $res = $this->db->prepared_query('SELECT * FROM users WHERE userID=:userID AND groupID=:groupID', $vars); - if ($res[0]['active'] == 1) { - $backups = $employee->getBackups($emp['empUID']); - foreach ($backups as $backup) { - $vars = array(':userID' => $backup['userName'], - ':groupID' => $groupID, - ':backupID' => $emp['userName'],); - - // Add backupID check for updates - $this->db->prepared_query('INSERT INTO users (userID, groupID, backupID) - VALUES (:userID, :groupID, :backupID) - ON DUPLICATE KEY UPDATE userID=:userID, groupID=:groupID', $vars); - } - } - } - } - $return_value = "groupID: {$groupID} imported"; - } - - return $return_value; - } - - public function getServices() - { - return $this->db->prepared_query('SELECT groupID as parentID, - serviceID as groupID, - service as groupTitle, - abbreviatedService as groupAbbreviation - FROM services - ORDER BY groupTitle ASC', array()); - } - - /** - * Get the current database version - * - * @return string the current database version - */ - public function getDatabaseVersion() - { - $version = $this->db->prepared_query('SELECT data FROM settings WHERE setting = "dbVersion"', array()); - if (count($version) > 0 && $version[0]['data'] !== null) - { - return $version[0]['data']; - } - - return 'unknown'; - } - - public function getGroups() - { - return $this->db->prepared_query('SELECT * FROM `groups` - WHERE groupID > 1 - ORDER BY name ASC', array()); - } - - - - /** - * @return array - * - * Created at: 7/31/2023, 7:41:43 AM (America/New_York) - */ - public function addAction(): array - { - if (!$this->login->checkGroup(1)) { - $return_value = array( - 'status' => array( - 'code' => 4, - 'message' => 'Admin access required' - ) - ); - } else { - $vars = array(':actionType' => preg_replace('/[^a-zA-Z0-9_]/', '', strip_tags($_POST['actionText']))); - $sql = 'SELECT `deleted` - FROM `actions` - WHERE `actionType` = :actionType'; - - $res = $this->db->pdo_select_query($sql, $vars); - error_log(print_r($res, true)); - - if ( - $res['status']['code'] == 2 - && ((!empty($res['data']) - && $res['data'][0]['deleted'] != 0) - || empty($res['data'])) - ) { - $alignment = 'right'; + if ( + $res['status']['code'] == 2 + && ((!empty($res['data']) + && $res['data'][0]['deleted'] != 0) + || empty($res['data'])) + ) { + $alignment = 'right'; if ($_POST['fillDependency'] < 1) { $alignment = 'left'; @@ -1034,423 +752,441 @@ public function getHistory($filterById) } /** - * - * @param Group $org_group - * @param Service $org_service * @param \Orgchart\Group $nexus_group - * @param \Orgchart\Employee $nexus_employee - * @param \Orgchart\Tag $nexus_tag - * @param \Orgchart\Position $nexus_position * * @return string * * Created at: 10/3/2022, 6:59:30 AM (America/New_York) */ - public function syncSystem(Group $org_group, Service $org_service, \Orgchart\Group $nexus_group, \Orgchart\Employee $nexus_employee, \Orgchart\Tag $nexus_tag, \Orgchart\Position $nexus_position): string + public function syncSystem(\Orgchart\Group $nexus_group): string { - // this is needed to clean up some databases where a user is currently not - // locally managed and they are also not active - $org_group->cleanDb(); - - $nexus_services = array(); - $nexus_chiefs = array(); - $nexus_groups = array(); - $nexus_users = array(); - $counter = 0; - $group_counter = 0; - $chief_counter = 0; - // update services and service chiefs $services = $nexus_group->listGroupsByTag('service'); foreach ($services as $service) { - $leader = $nexus_position->findRootPositionByGroupTag($nexus_group->getGroupLeader($service['groupID']), $nexus_tag->getParent('service')); + $this->updateService($service['groupID']); + } + + $groups = $this->getOrgchartImportTags($nexus_group); - $nexus_services[$counter]['serviceID'] = $service['groupID']; - $nexus_services[$counter]['service'] = $service['groupTitle']; - $nexus_services[$counter]['abbreviatedService'] = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; - $nexus_services[$counter]['groupID'] = is_array($leader) && isset($leader[0]['groupID']) ? $leader[0]['groupID'] : null; + foreach ($groups as $group) { + $this->updateGroup($group['groupID']); + } - $leaderGroupID = $nexus_group->getGroupLeader($service['groupID']); - $serviceEmployee = $nexus_position->getEmployees($leaderGroupID); + return 'Syncing has finished. You are set to go.'; + } - foreach($serviceEmployee as $employee){ - if (is_numeric($service['groupID']) && !empty($employee['userName'])) { - $nexus_chiefs[$chief_counter]['serviceID'] = $service['groupID']; - $nexus_chiefs[$chief_counter]['userID'] = $employee['userName']; - $nexus_chiefs[$chief_counter]['backupID'] = null; + /** + * getOrgchartImportTags retrieves + * + * @param \Orgchart\Group $group + * + * @return array + * + * Created at: 9/14/2022, 7:35:53 AM (America/New_York) + */ + private function getOrgchartImportTags(\Orgchart\Group $group): array + { + $groups = array(); + $tags = Config::$orgchartImportTags; + $tags[] = 'Pentad'; - $chief_counter++; - } + foreach ($tags as $tag) + { + $groups = array_merge($groups, $group->listGroupsByTag($tag)); + } - if (count($employee['backups']) > 0) { - foreach ($employee['backups'] as $backup) { - if (is_numeric($service['groupID']) && !empty($backup['userName'])) { - $nexus_chiefs[$chief_counter]['serviceID'] = $service['groupID']; - $nexus_chiefs[$chief_counter]['userID'] = $backup['userName']; - $nexus_chiefs[$chief_counter]['backupID'] = $employee['userName']; + return $groups; + } - $chief_counter++; - } - } - } - } + private function removeGroup(int $groupID): array + { + $vars = array(':groupID' => $groupID); + $sql = 'DELETE + FROM `groups` + WHERE `groupID` = :groupID'; - if ($service['groupID'] == $nexus_services[$counter]['groupID']) { - $chiefs = $org_service->getChiefs($service['groupID']); + $return_value = $this->db->pdo_delete_query($sql, $vars); - foreach ($chiefs as $chief) { - $nexus_users[$group_counter]['userID'] = $chief['userID']; - $nexus_users[$group_counter]['groupID'] = $nexus_services[$counter]['groupID']; - $nexus_users[$group_counter]['backupID'] = $chief['backupID']; - } + $sql = 'DELETE + FROM `users` + WHERE `groupID` = :groupID'; - $group_counter++; - } + $return_value = $this->db->pdo_delete_query($sql, $vars); + + return $return_value; + } - $counter++; + /** + * @param int $groupID + * + * @return array + * + * Created at: 6/30/2023, 1:25:07 PM (America/New_York) + */ + private function updateCatPrivs(int $groupID): array + { + $cat_privs = $this->getCatPrivs($groupID); + + if ($cat_privs['status']['code'] == 2 && !empty($cat_privs['data'])) { + $return_value = $this->deleteCatPrivs($groupID); + } else { + $return_value = array ( + 'status' => array ( + 'code' => 2, + 'message' => 'Nothing to be done with category_privs' + ) + ); } - $portal_services = $org_service->getAllQuadrads(); - $portal_chiefs = $org_service->getAllChiefs(); + return $return_value; + } + + /** + * @param int $groupID + * + * @return array + * + * Created at: 6/30/2023, 1:25:25 PM (America/New_York) + */ + private function deleteCatPrivs(int $groupID): array + { + $vars = array(':groupID' => $groupID); + $sql = 'DELETE + FROM `category_privs` + WHERE `groupID` = :groupID'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + return $return_value; + } + + /** + * @param int $serviceID + * + * @return array + * + * Created at: 8/16/2023, 8:39:33 AM (America/New_York) + */ + private function deleteChiefs(int $serviceID): array + { + $vars = array(':serviceID' => $serviceID); + $sql = 'DELETE + FROM `service_chiefs` + WHERE `serviceID` = :serviceID + AND `locallyManaged` = 0 + AND `active` = 1'; + + $return_value = $this->db->pdo_delete_query($sql , $vars); + + return $return_value; + } + + /** + * @param string $userName + * @param int $serviceID + * + * @return array + * + * Created at: 8/16/2023, 8:41:14 AM (America/New_York) + */ + private function insertChief(string $userName, int $serviceID): array + { + $vars = array(':userID' => $userName, + ':serviceID' => $serviceID); + $sql = 'INSERT INTO `service_chiefs` (`serviceID`, `userID`, `active`) + VALUES (:serviceID, :userID, 1) + ON DUPLICATE KEY UPDATE `serviceID` = :serviceID, `userID` = :userID'; - $this->processServices($portal_services, $portal_chiefs, $nexus_services, $nexus_chiefs, $org_service); + $return_value = $this->db->pdo_insert_query($sql, $vars); - // update groups and users - $groups = $nexus_group->listGroupsByTag($nexus_tag->getParent('service')); - $counter = 0; + return $return_value; + } - foreach ($groups as $group) { - $nexus_groups[$counter]['groupID'] = $group['groupID']; - $nexus_groups[$counter]['parentGroupID'] = -1; - $nexus_groups[$counter]['name'] = $group['groupTitle']; + /** + * @param int $serviceID + * @param string $title + * @param string $abbr + * @param int $groupID + * + * @return array + * + * Created at: 8/16/2023, 8:41:30 AM (America/New_York) + */ + private function insertService(int $serviceID, string $title, string $abbr, int $groupID): array + { + $vars = array(':serviceID' => $serviceID, + ':service' => $title, + ':abbrService' => $abbr, + ':groupID' => $groupID ); + $sql = 'INSERT INTO `services` (`serviceID`, `service`, + `abbreviatedService`, `groupID`) + VALUES (:serviceID, :service, :abbrService, :groupID) + ON DUPLICATE KEY UPDATE `service` = :service, `groupID` = :groupID, + `abbreviatedService` = :abbrService'; - $leaderGroupID = $nexus_group->getGroupLeader($group['groupID']); + $return_value = $this->db->pdo_insert_query($sql, $vars); - $employees = array_merge($nexus_position->getEmployees($leaderGroupID), $nexus_group->listGroupEmployees($group['groupID'])); + return $return_value; + } - foreach ($employees as $employee) { - if ($employee['userName'] != '') { - $nexus_users[$group_counter]['userID'] = $employee['userName']; - $nexus_users[$group_counter]['groupID'] = $group['groupID']; - $nexus_users[$group_counter]['backupID'] = null; + /** + * @param int $groupID + * + * @return array + * + * Created at: 6/30/2023, 1:25:38 PM (America/New_York) + */ + private function getCatPrivs(int $groupID): array + { + $vars = array(':groupID' => $groupID); + $sql = 'SELECT `categoryID` + FROM `category_privs` + LEFT JOIN `groups` USING (`groupID`) + WHERE `category_privs`.`groupID` = :groupID + AND `groups`.`groupID` IS NULL'; - $group_counter++; + $return_value = $this->db->pdo_select_query($sql, $vars); - if (isset($employee['backups'])) { - foreach ($employee['backups'] as $backup) { - if ($backup['userName'] != '') { - $nexus_users[$group_counter]['userID'] = $backup['userName']; - $nexus_users[$group_counter]['groupID'] = $group['groupID']; - $nexus_users[$group_counter]['backupID'] = $employee['userName']; - $group_counter++; - } - } - } - } - } + return $return_value; + } + /** + * @param int $groupID + * @param bool $group + * + * @return array + * + * Created at: 8/16/2023, 8:32:46 AM (America/New_York) + */ + private function addBackups(int $groupID, bool $group = true): array + { + $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); + $employee = new \Orgchart\Employee($oc_db, $this->login); - $counter++; + // get all users for this group + if ($group) { + $group_users = $this->getGroupUsers($groupID); + } else { + $group_users = $this->getServiceUsers($groupID); } - // update Nexus with portal groups - $portal_groups = $org_group->getAllGroups(); - - $this->updateNexusWithPortalGroups($portal_groups, $nexus_group); - - $groups = $this->getOrgchartImportTags($nexus_group); - - foreach ($groups as $group) { - $nexus_groups[$counter]['groupID'] = $group['groupID']; - $nexus_groups[$counter]['parentGroupID'] = null; - $nexus_groups[$counter]['name'] = $group['groupTitle']; - - $positions = $nexus_group->listGroupPositions($group['groupID']); - $employees = $nexus_group->listGroupEmployees($group['groupID']); + // loop through group_users to add backups + if ($group_users['status']['code'] == 2){ + $userNames = array(); - foreach ($positions as $position) { - $employees = array_merge($employees, $nexus_position->getEmployees($position['positionID'])); + foreach ($group_users['data'] as $user) { + $userNames[] = $user['userID']; } - foreach ($employees as $employee) { - if (!empty($employee['userName'])) { - $nexus_users[$group_counter]['userID'] = $employee['userName']; - $nexus_users[$group_counter]['groupID'] = $group['groupID']; - $nexus_users[$group_counter]['backupID'] = null; + $return_value = array ( + 'status' => array ( + 'code' => 2, + 'message' => '' + ) + ); + + $employee_list = $employee->getEmployeeByUserName($userNames, $oc_db); + foreach ($employee_list['data'] as $user) { + // if active user, then get backups and add them + if ($user['deleted'] == 0) { + $backups = $employee->getBackups($user['empUID']); - $group_counter++; + if (!empty($backups)) { + foreach ($backups as $backup) { + if ($group) { + $backup_added = $this->addBackup($groupID, $backup['userName'], $user['userName']); + } else { + $backup_added = $this->addServiceBackup($groupID, $backup['userName'], $user['userName']); + } - $backups = $nexus_employee->getBackups($employee['empUID']); - foreach ($backups as $backup) { - if (isset($backup['userName']) && !empty($backup['userName'])) { - $nexus_users[$group_counter]['userID'] = $backup['userName']; - $nexus_users[$group_counter]['groupID'] = $group['groupID']; - $nexus_users[$group_counter]['backupID'] = $employee['userName']; - $group_counter++; + if ($backup_added['status']['code'] == 2) { + continue; + } else { + $return_value = array ( + 'status' => array ( + 'code' => 4, + 'message' => 'Action failed to add backups.' + ) + ); + break; + } } } } } - - $counter++; + } else { + $return_value = $group_users; } - $portal_users = $org_group->getAllUsers(); - - $this->processGroups($portal_groups, $portal_users, $nexus_groups, $nexus_users, $org_group); - - return 'Syncing has finished. You are set to go.'; + return $return_value; } /** - * [Description for processServices] + * @param int $serviceID * - * @param array $portal_services - * @param array $portal_chiefs - * @param array $nexus_services - * @param array $nexus_chiefs - * @param Service $org_service - * - * @return void + * @return array * - * Created at: 9/14/2022, 4:12:49 PM (America/New_York) + * Created at: 8/16/2023, 8:42:10 AM (America/New_York) */ - private function processServices(array $portal_services, array $portal_chiefs, array $nexus_services, array $nexus_chiefs, Service $org_service): void + private function getServiceUsers(int $serviceID): array { - // find service records to delete on portal side - foreach($portal_services as $service) { - if ($this->searchArray($nexus_services, $service)) { - // service exists do nothing - } else { - // service does not exist remove from portal db - //echo 'The service \'' . $service['service'] . '\' has been removed.
'; - $org_service->removeSyncService($service['serviceID']); - } - } - - // add service records that do not exist yet - foreach($nexus_services as $service) { - if ($this->searchArray($portal_services, $service)) { - // service exists do nothing - } else { - // service does not exist add it to the portal db - //echo 'The service \'' . $service['service'] . '\' was added.
'; - if(is_numeric($service['serviceID']) && !empty($service['service']) && (is_numeric($service['groupID']) || is_null($service['groupID']))) { - $org_service->importService($service['serviceID'], $service['service'], $service['abbreviatedService'], $service['groupID']); - } - } - } + $vars = array(':serviceID' => $serviceID); + $sql = 'SELECT `userID` + FROM `service_chiefs` + WHERE `serviceID` = :serviceID + AND `backupID` = ""'; - // find chiefs that need to be removed from portal - foreach($portal_chiefs as $chief) { - if ($this->searchArray($nexus_chiefs, $chief, false, 3)) { - // chief exists do nothing - } else { - // chief does not exist at nexus check for locallyManaged and active - // remove if locallyManaged and inactive - // remove if not locallyManaged - if ($chief['locallyManaged'] && $chief['active']) { - // this chief is locally managed and is active leave it here, do nothing - } else { - //echo 'The Service Chief with an userID of \'' . $chief['serviceID'] . '-' . $chief['userID'] . '\' was removed.
'; - $org_service->removeChief($chief['serviceID'], $chief['userID'], $chief['backupID']); - } - } - } + $return_value = $this->db->pdo_select_query($sql, $vars); - // add chief records that do not exist yet - foreach ($nexus_chiefs as $chief) { - if ($this->searchArray($portal_chiefs, $chief, false, 3)) { - // chief exists do nothing - } else { - // chief does not exist add them now - //echo 'The Service Chief with userID of \'' . $chief['userID']. '\' was added.
'; - $org_service->importChief($chief['serviceID'], $chief['userID'], $chief['backupID']); - } - } + return $return_value; } /** - * [Description for processGroups] - * - * @param array $portal_groups - * @param array $portal_users - * @param array $nexus_groups - * @param array $nexus_users - * @param Group $org_group + * @param int $serviceID + * @param string $backup_user + * @param string $user * - * @return void + * @return array * - * Created at: 10/3/2022, 7:02:32 AM (America/New_York) + * Created at: 8/16/2023, 8:42:47 AM (America/New_York) */ - private function processGroups(array $portal_groups, array $portal_users, array $nexus_groups, array $nexus_users, Group $org_group): void + private function addServiceBackup(int $serviceID, string $backup_user, string $user): array { - // find group records to delete on portal side - foreach($portal_groups as $group) { - if ($this->searchArray($nexus_groups, $group, false)) { - // group exists check in on backups - $this->updateGroup($group['groupID']); - } else { - // group does not exist remove from portal db - //echo 'The group \'' . $group['name'] . '\' has been removed
'; - // groups should never be deleted if on the portal side. No matter what Nexus says - // $org_group->removeSyncGroup($group['groupID']); - } - } + $vars = array(':userID' => $backup_user, + ':serviceID' => $serviceID, + ':backupID' => $user); + $sql = 'INSERT INTO `service_chiefs` (`userID`, `serviceID`, + `backupID`) + VALUES (:userID, :serviceID, :backupID) + ON DUPLICATE KEY UPDATE `userID` = :userID, + `serviceID` = :serviceID'; - // add group records that do not exist yet - foreach($nexus_groups as $group) { - if ($this->searchArray($portal_groups, $group, false)) { - // group exists do nothing - } else { - // group does not exist add it to the portal db - //echo 'The group \'' . $group['name'] . '\' has been added
'; - $org_group->syncImportGroup($group); - } - } + $return_value = $this->db->pdo_insert_query($sql, $vars); - // find users that need to be removed from portal - foreach($portal_users as $user) { - if ($this->searchArray($nexus_users, $user, false, 3)) { - // user exists do nothing - //echo 'User \'' . $user['groupID'] . '-' .$user['userID'] . '\' remained.
'; - } else { - // user does not exist check for locallyManaged and active - // remove if locallyManaged and inactive - // remove if not locallyManaged - if ($user['locallyManaged'] && $user['active']) { - // user is locally managed and active level them alone. - } else if (!$user['locallyManaged'] || ($user['locallyManaged']) && !$user['active']) { - // check one more thing, is this user a backup to a locally managed user - if ($user['backupID'] != '' && $this->imABackup($portal_users, $user)) { - // I'm a backup, do nothing - } else { - //echo 'User with userID of \'' . $user['userID'] . '\' and a groupID of ' . $user['groupID'] . ' has been removed.
'; - $org_group->removeUser($user['userID'], $user['groupID'], $user['backupID']); - } + return $return_value; + } - } else { - // this user is locally managed and is active leave it here, do nothing - } - } - } + /** + * @param int $groupID + * @param string $backup_user + * @param string $user + * + * @return array + * + * Created at: 6/30/2023, 1:26:30 PM (America/New_York) + */ + private function addBackup(int $groupID, string $backup_user, string $user): array + { + $vars = array(':userID' => $backup_user, + ':groupID' => $groupID, + ':backupID' => $user); + $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`) + VALUES (:userID, :groupID, :backupID) + ON DUPLICATE KEY UPDATE `userID` = :userID, `groupID` = :groupID'; - // add user records that do not exist yet - foreach ($nexus_users as $user) { - if ($this->searchArray($portal_users, $user, false, 3)) { - // user exists do nothing - } else { - // user does not exist add them now - //echo 'User with userID \'' . $user['userID'] . '\' was added.
'; - //echo 'User with userID \'' . $user['groupID'] . '-' .$user['userID'] . '\' was added.
'; - if ($user['backupID'] == null) { - $user['backupID'] = ''; - } + $return_value = $this->db->pdo_insert_query($sql, $vars); - $org_group->importUser($user['userID'], $user['groupID'], $user['backupID']); - } - } + return $return_value; } /** - * getOrgchartImportTags retrieves - * - * @param \Orgchart\Group $group + * @param int $groupID * * @return array * - * Created at: 9/14/2022, 7:35:53 AM (America/New_York) + * Created at: 6/30/2023, 1:26:53 PM (America/New_York) */ - private function getOrgchartImportTags(\Orgchart\Group $group): array + private function getGroupUsers(int $groupID): array { - $groups = array(); - $tags = Config::$orgchartImportTags; - $tags[] = 'Pentad'; + $vars = array(':groupID' => $groupID); + $sql = 'SELECT `userID` + FROM `users` + WHERE `groupID` = :groupID + AND `backupID` = ""'; - foreach ($tags as $tag) - { - $groups = array_merge($groups, $group->listGroupsByTag($tag)); - } + $return_value = $this->db->pdo_select_query($sql, $vars); - return $groups; + return $return_value; } /** - * Search multidimensional arrays for matches - * - * @param array $search - * @param array $criteria - * @param bool $whole_array - matching a whole array or just partial - * @param int $index - the number of associative array columns to match. - * must be the first ones listed. + * @param int $groupID + * @param array $emp * - * @return bool + * @return array * - * Created at: 9/13/2022, 7:56:52 AM (America/New_York) + * Created at: 6/30/2023, 1:27:17 PM (America/New_York) */ - private function searchArray($search, $criteria, $whole_array = true, $index = 3): bool + private function insertUser(int $groupID, array $emp): array { - $exists = false; + if (!empty($emp['userName'])) { + $vars = array(':userID' => $emp['userName'], + ':groupID' => $groupID, ); + $sql = 'INSERT INTO `users` (`userID`, `groupID`, `backupID`, `active`) + VALUES (:userID, :groupID, "", 1) + ON DUPLICATE KEY UPDATE `userID` = :userID, `groupID` = :groupID'; - if ($whole_array) { - foreach($search as $value) { - if ($value == $criteria) { - $exists = true; - break; - } - } + $return_value = $this->db->pdo_insert_query($sql, $vars); } else { - foreach($search as $value) { - $keys = array_keys($value); - - for($x = 0; $x < $index; $x++) { - if ($value[$keys[$x]] == $criteria[$keys[$x]]) { - // so far so good, check the next - } else { - // we don't have a match continue the search with the next array - break; - } - - if (($x + 1) == $index) { - $exists = true; - - break 2; - } - } - } + $return_value = array ( + 'status' => array ( + 'code' => 4, + 'message' => 'Improperly formatted data.' + ) + ); } - return $exists; + return $return_value; } - public function imABackup(array $portal_users, array $user): bool + /** + * @param int $groupID + * + * @return array + * + * Created at: 6/30/2023, 1:27:47 PM (America/New_York) + */ + private function deleteUsers(int $groupID): array { - $backup = false; + $vars = array(':groupID' => $groupID); + $sql = 'DELETE + FROM `users` + WHERE `groupID` = :groupID + AND `locallyManaged` = 0 + AND `active` = 1'; - foreach ($portal_users as $portal) { - if ($portal['groupID'] == $user['groupID'] and $portal['userID'] == $user['backupID']) { - $backup = true; - break; - } - } + $return_value = $this->db->pdo_delete_query($sql , $vars); - return $backup; + return $return_value; } - private function updateNexusWithPortalGroups(array $portal_groups, \Orgchart\Group $nexus_group): void + /** + * @param int $groupID + * @param bool $isQuadrad + * @param string $title + * + * @return array + * + * Created at: 6/30/2023, 1:28:03 PM (America/New_York) + */ + private function insertGroup(int $groupID, bool $isQuadrad, string $title): array { - $nexus_groups = $nexus_group->listGroupsByTag(Config::$orgchartImportTags[0]); + $vars = array(':groupID' => $groupID, + ':parentGroupID' => ($isQuadrad == true ? -1 : null), + ':name' => $title, + ':groupDescription' => '', ); + $sql = 'INSERT INTO `groups` (`groupID`, `parentGroupID`, `name`, + `groupDescription`) + VALUES (:groupID, :parentGroupID, :name, :groupDescription) + ON DUPLICATE KEY UPDATE `parentGroupID` = :parentGroupID, `name` = :name, + `groupDescription` = :groupDescription'; - foreach ($portal_groups as $group) { - if ($this->searchArray($nexus_groups, $group, false, 1)) { - // this group is already tagged. - } else { - // not tagged, add it now. - $nexus_group->addGroupTag(Config::$orgchartImportTags[0], $group['groupID']); - } - } + $return_value = $this->db->pdo_insert_query($sql, $vars); + return $return_value; } + + } diff --git a/docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023072000-2023082400.sql b/docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023072000-2023082400.sql new file mode 100644 index 000000000..572dcdc5d --- /dev/null +++ b/docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023072000-2023082400.sql @@ -0,0 +1,32 @@ +START TRANSACTION; + +UPDATE `service_chiefs` +SET `backupID` = "" +WHERE `backupID` IS NULL; + +ALTER TABLE `service_chiefs` MODIFY `backupID` varchar(50) NOT NULL DEFAULT ''; + +ALTER TABLE `service_chiefs` DROP INDEX `serviceID_2`; + +ALTER TABLE `service_chiefs` ADD PRIMARY KEY(`userID`, `serviceID`, `backupID`); + +UPDATE `settings` SET `data` = '2023082400' WHERE `settings`.`setting` = 'dbversion'; + +COMMIT; + + + /**** Revert DB ***** + START TRANSACTION; + ALTER TABLE `service_chiefs` DROP PRIMARY KEY; + +ALTER TABLE `service_chiefs` ADD PRIMARY KEY(`userID`, `groupID`); + +ALTER TABLE `service_chiefs` MODIFY `backupID` varchar(50); + +UPDATE `service_chiefs` +SET `backupID` = NULL +WHERE `backupID` = ""; + + UPDATE `settings` SET `data` = '2023072000' WHERE `settings`.`setting` = 'dbversion'; + COMMIT; + */ \ No newline at end of file From 1865bc8b24f7c9a974d8fab82d95543ff94b3251 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Wed, 16 Aug 2023 10:13:06 -0400 Subject: [PATCH 03/22] Leaf 3902 - remove error logging --- LEAF_Request_Portal/sources/System.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 0e516a4d3..894923ff0 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -114,9 +114,6 @@ public function updateService(int $serviceID): array // check if this service is also an ELT // if so, update groups table $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); - error_log(print_r($serviceID, true)); - error_log(print_r($quadID, true)); - error_log(print_r($tagged, true)); if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { $this->updateGroup($serviceID); From dccc3145df3850929ade18c83f5d3482181e5c18 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Wed, 16 Aug 2023 15:24:17 -0400 Subject: [PATCH 04/22] Leaf 3902 - PR comments update --- .../admin/templates/mod_groups.tpl | 24 ++--- .../admin/templates/mod_svcChief.tpl | 87 ++++++++++--------- LEAF_Request_Portal/sources/Group.php | 2 + LEAF_Request_Portal/sources/Service.php | 2 - LEAF_Request_Portal/sources/System.php | 6 +- 5 files changed, 65 insertions(+), 56 deletions(-) diff --git a/LEAF_Request_Portal/admin/templates/mod_groups.tpl b/LEAF_Request_Portal/admin/templates/mod_groups.tpl index 69a63fe94..d78b7d7c7 100644 --- a/LEAF_Request_Portal/admin/templates/mod_groups.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_groups.tpl @@ -395,16 +395,20 @@ function toTitleCase(str) { } function addAdmin(userID) { - $.ajax({ - type: 'POST', - url: "../api/group/" + 1 + "/members", - data: {'userID': userID, - 'CSRFToken': ''}, - success: function(response) { - getMembers(1); - }, - cache: false - }); + if (userID === '') { + return; + } else { + $.ajax({ + type: 'POST', + url: "../api/group/" + 1 + "/members", + data: {'userID': userID, + 'CSRFToken': ''}, + success: function(response) { + getMembers(1); + }, + cache: false + }); + } } function removeAdmin(userID) { diff --git a/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl b/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl index d0bcd87ac..d92daaa27 100644 --- a/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl @@ -216,29 +216,26 @@ function removeUser(groupID = -1, userID = '') { * @param {string} selectedUserName - Username being imported */ function importUser(serviceID = 0, selectedUserName = '') { - if (serviceID === 0) { - console.log('Invalid serviceID'); - } - if (selectedUserName === '') { - console.log('Invalid username'); + if (serviceID === 0 || selectedUserName === '') { + return; + } else { + $.ajax({ + type: 'POST', + url: '/api/employee/import/_' + selectedUserName, + data: {CSRFToken: ''}, + success: function(res) { + if (!isNaN(res)) { + addUser(serviceID, selectedUserName); // add identified user into portal. + } else { + alert(res); + } + }, + fail: function(err) { + console.log(err); + }, + cache: false + }); } - - $.ajax({ - type: 'POST', - url: '/api/employee/import/_' + selectedUserName, - data: {CSRFToken: ''}, - success: function(res) { - if (!isNaN(res)) { - addUser(serviceID, selectedUserName); // add identified user into portal. - } else { - alert(res); - } - }, - fail: function(err) { - console.log(err); - }, - cache: false - }); } /** @@ -419,28 +416,36 @@ function initiateModal(serviceID = 0, serviceName = '') { } function pruneMember(groupID, userID) { - console.log('pruneMember'); - $.ajax({ - type: 'POST', - url: "../api/service/" + groupID + "/members/_" + userID + "/prune", - data: {'CSRFToken': ''}, - fail: function(err) { - console.log(err); - }, - cache: false - }); + if (groupID === 0 || userID === '') { + return; + } else { + $.ajax({ + type: 'POST', + url: "../api/service/" + groupID + "/members/_" + userID + "/prune", + data: {'CSRFToken': ''}, + fail: function(err) { + console.log(err); + }, + cache: false + }); + } + } function reactivateMember(groupID, userID) { - $.ajax({ - type: 'POST', - url: "../api/service/" + groupID + "/members/_" + userID + "/reactivate", - data: {'CSRFToken': ''}, - fail: function(err) { - console.log(err); - }, - cache: false - }); + if (groupID === 0 || userID === '') { + return; + } else { + $.ajax({ + type: 'POST', + url: "../api/service/" + groupID + "/members/_" + userID + "/reactivate", + data: {'CSRFToken': ''}, + fail: function(err) { + console.log(err); + }, + cache: false + }); + } } /** diff --git a/LEAF_Request_Portal/sources/Group.php b/LEAF_Request_Portal/sources/Group.php index f40afdfc2..ded70f6f3 100644 --- a/LEAF_Request_Portal/sources/Group.php +++ b/LEAF_Request_Portal/sources/Group.php @@ -359,6 +359,8 @@ public function importUser(string $userID, int $groupID, string $backupID): arra * @param int $groupID * * @return void + * + * Created at: 8/16/2023, 3:01:10 PM (America/New_York) */ public function deactivateMember($member, $groupID): void { diff --git a/LEAF_Request_Portal/sources/Service.php b/LEAF_Request_Portal/sources/Service.php index 7ba59c1b3..edfc2733a 100644 --- a/LEAF_Request_Portal/sources/Service.php +++ b/LEAF_Request_Portal/sources/Service.php @@ -449,8 +449,6 @@ public function removeMember($groupID, $member) } /** - * [Description for removeChief] - * * @param int $serviceID * @param string $userID * @param string $backupID diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 894923ff0..8e55d9289 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -349,7 +349,7 @@ public function importGroup($groupID): string return $return_value; } - public function getServices() + public function getServices(): array { return $this->db->prepared_query('SELECT groupID as parentID, serviceID as groupID, @@ -364,7 +364,7 @@ public function getServices() * * @return string the current database version */ - public function getDatabaseVersion() + public function getDatabaseVersion(): string { $version = $this->db->prepared_query('SELECT data FROM settings WHERE setting = "dbVersion"', array()); if (count($version) > 0 && $version[0]['data'] !== null) @@ -375,7 +375,7 @@ public function getDatabaseVersion() return 'unknown'; } - public function getGroups() + public function getGroups(): array { return $this->db->prepared_query('SELECT * FROM `groups` WHERE groupID > 1 From f17f28652300a539bb845f8eb0e60fc3c7997b63 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 17 Aug 2023 15:13:12 -0400 Subject: [PATCH 05/22] Leaf 3902 - too many connects error --- LEAF_Request_Portal/sources/System.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 8e55d9289..5ff6c134f 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -116,7 +116,7 @@ public function updateService(int $serviceID): array $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { - $this->updateGroup($serviceID); + $this->updateGroup($serviceID, $oc_db); } else { // make sure this is not in the groups table? $this->removeGroup($serviceID); @@ -143,7 +143,7 @@ public function updateService(int $serviceID): array * * Created at: 6/30/2023, 1:24:51 PM (America/New_York) */ - public function updateGroup(int $groupID): array + public function updateGroup(int $groupID, ?\Leaf\Db $oc_db = null): array { if (!is_numeric($groupID)) { $return_value = array( @@ -160,7 +160,10 @@ public function updateGroup(int $groupID): array ) ); } else { - $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); + if ($oc_db === null) { + $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); + } + $group = new \Orgchart\Group($oc_db, $this->login); $position = new \Orgchart\Position($oc_db, $this->login); $tag = new \Orgchart\Tag($oc_db, $this->login); @@ -765,9 +768,10 @@ public function syncSystem(\Orgchart\Group $nexus_group): string } $groups = $this->getOrgchartImportTags($nexus_group); + $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); foreach ($groups as $group) { - $this->updateGroup($group['groupID']); + $this->updateGroup($group['groupID'], $oc_db); } return 'Syncing has finished. You are set to go.'; From 50294b48e6fd948611f56b3e8e00abd0d4244106 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Fri, 18 Aug 2023 13:30:26 -0400 Subject: [PATCH 06/22] Leaf 3902 - disappearing backups --- LEAF_Request_Portal/admin/ajaxIndex.php | 2 ++ .../admin/templates/mod_groups.tpl | 8 ++--- LEAF_Request_Portal/sources/Group.php | 35 ++++++------------- LEAF_Request_Portal/sources/Service.php | 10 +++--- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/LEAF_Request_Portal/admin/ajaxIndex.php b/LEAF_Request_Portal/admin/ajaxIndex.php index 887daed4b..849aabf5e 100644 --- a/LEAF_Request_Portal/admin/ajaxIndex.php +++ b/LEAF_Request_Portal/admin/ajaxIndex.php @@ -53,6 +53,7 @@ function checkToken() break; case 'remove_user_old': + // this should be deprecated as of 8/18/2023 checkToken(); $deleteList = Leaf\XSSHelpers::scrubObjectOrArray(json_decode($_POST['json'], true)); @@ -72,6 +73,7 @@ function checkToken() break; case 'remove_user': + // this should be deprecated as of 8/18/2023 checkToken(); $group = new Portal\Group($db, $login); diff --git a/LEAF_Request_Portal/admin/templates/mod_groups.tpl b/LEAF_Request_Portal/admin/templates/mod_groups.tpl index d78b7d7c7..cf70fd99c 100644 --- a/LEAF_Request_Portal/admin/templates/mod_groups.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_groups.tpl @@ -413,11 +413,9 @@ function addAdmin(userID) { function removeAdmin(userID) { $.ajax({ - type: 'POST', - url: "ajaxIndex.php?a=remove_user", - data: {'userID': userID, - 'groupID': 1, - 'CSRFToken': ''}, + type: 'DELETE', + url: "../api/group/1/members/" + userID, + data: {'CSRFToken': ''}, success: function(response) { getMembers(1); }, diff --git a/LEAF_Request_Portal/sources/Group.php b/LEAF_Request_Portal/sources/Group.php index ded70f6f3..a126e7f62 100644 --- a/LEAF_Request_Portal/sources/Group.php +++ b/LEAF_Request_Portal/sources/Group.php @@ -398,36 +398,23 @@ public function deactivateMember($member, $groupID): void */ public function removeMember($member, $groupID): void { - $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \PORTAL_CONFIG->phonedbName); - $employee = new \Orgchart\Employee($oc_db, $this->login); - - if (is_numeric($groupID) && $member != '') - { - $sql_vars = array(':userID' => $member, - ':groupID' => $groupID, ); - + if (is_numeric($groupID) && $member != '') { $this->dataActionLogger->logAction(\Leaf\DataActions::DELETE, \Leaf\LoggableTypes::EMPLOYEE, [ new \Leaf\LogItem("users", "userID", $member, $this->getEmployeeDisplay($member)), new \Leaf\LogItem("users", "groupID", $groupID, $this->getGroupName($groupID)) ]); - $this->db->prepared_query('DELETE FROM users WHERE userID=:userID AND groupID=:groupID', $sql_vars); - - // include the backups of employee - $emp = $employee->lookupLogin($member); - $backups = $employee->getBackups($emp[0]['empUID']); - foreach ($backups as $backup) { - $sql_vars = array(':userID' => $backup['userName'], - ':groupID' => $groupID, - ':backupID' => $member,); - - $res = $this->db->prepared_query('SELECT * FROM users WHERE userID=:userID AND groupID=:groupID AND backupID=:backupID', $sql_vars); + $vars = array(':userID' => $member, + ':groupID' => $groupID); + $sql = 'DELETE + FROM `users` + WHERE (`userID` = :userID + AND `groupID` = :groupID + AND `backupID` = "") + OR (`groupID` = :groupID + AND `backupID` = :userID)'; - // Check for locallyManaged users - if ($res[0]['locallyManaged'] == 0) { - $this->db->prepared_query('DELETE FROM users WHERE userID=:userID AND groupID=:groupID AND backupID=:backupID', $sql_vars); - } - } + $this->db->prepared_query($sql, $vars); } } diff --git a/LEAF_Request_Portal/sources/Service.php b/LEAF_Request_Portal/sources/Service.php index edfc2733a..b1937c932 100644 --- a/LEAF_Request_Portal/sources/Service.php +++ b/LEAF_Request_Portal/sources/Service.php @@ -257,7 +257,7 @@ public function importChief(int $serviceID, string $userID, string|null $backupI public function deactivateChief(int $groupID, string $member): array { if (is_numeric($groupID) && $member != '') { - $this->dataActionLogger->logAction(\Leaf\DataActions::MODIFY, \Leaf\LoggableTypes::EMPLOYEE, [ + $this->dataActionLogger->logAction(\Leaf\DataActions::MODIFY, \Leaf\LoggableTypes::SERVICE_CHIEF, [ new \Leaf\LogItem("users", "userID", $member, $this->getEmployeeDisplay($member)), new \Leaf\LogItem("users", "groupID", $groupID, $this->getServiceName($groupID)) ]); @@ -308,7 +308,7 @@ public function deactivateChief(int $groupID, string $member): array */ public function pruneChief(int $serviceID, string $userName): array { - $this->dataActionLogger->logAction(\Leaf\DataActions::DELETE, \Leaf\LoggableTypes::EMPLOYEE, [ + $this->dataActionLogger->logAction(\Leaf\DataActions::DELETE, \Leaf\LoggableTypes::SERVICE_CHIEF, [ new \Leaf\LogItem("users", "userID", $userName, $this->getEmployeeDisplay($userName)), new \Leaf\LogItem("users", "groupID", $serviceID, $this->getServiceName($serviceID)) ]); @@ -318,7 +318,8 @@ public function pruneChief(int $serviceID, string $userName): array $sql = 'DELETE FROM `service_chiefs` WHERE `serviceID` = :serviceID - AND (`userID` = :userID + AND ((`userID` = :userID + AND `backupID` = "") OR `backupID` = :userID)'; $return_value = $this->db->pdo_delete_query($sql, $vars); @@ -326,7 +327,8 @@ public function pruneChief(int $serviceID, string $userName): array $sql = 'DELETE FROM `users` WHERE `groupID` = :serviceID - AND (`userID` = :userID + AND ((`userID` = :userID + AND `backupID` = "") OR `backupID` = :userID)'; $return_value = $this->db->pdo_delete_query($sql, $vars); From c7e09dbef202faacf72b38c6a72acf653de8d77b Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Fri, 18 Aug 2023 14:23:37 -0400 Subject: [PATCH 07/22] Leaf 3902 - History update --- LEAF_Request_Portal/sources/Service.php | 8 ++++---- libs/logger/DataActionLogger.php | 3 --- libs/logger/formatters/ServiceChiefFormatter.php | 4 ++++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/LEAF_Request_Portal/sources/Service.php b/LEAF_Request_Portal/sources/Service.php index b1937c932..2205f5470 100644 --- a/LEAF_Request_Portal/sources/Service.php +++ b/LEAF_Request_Portal/sources/Service.php @@ -258,8 +258,8 @@ public function deactivateChief(int $groupID, string $member): array { if (is_numeric($groupID) && $member != '') { $this->dataActionLogger->logAction(\Leaf\DataActions::MODIFY, \Leaf\LoggableTypes::SERVICE_CHIEF, [ - new \Leaf\LogItem("users", "userID", $member, $this->getEmployeeDisplay($member)), - new \Leaf\LogItem("users", "groupID", $groupID, $this->getServiceName($groupID)) + new \Leaf\LogItem("service_chiefs", "userID", $member, $this->getEmployeeDisplay($member)), + new \Leaf\LogItem("service_chiefs", "serviceID", $groupID, $this->getServiceName($groupID)) ]); $vars = array(':userID' => $member, @@ -309,8 +309,8 @@ public function deactivateChief(int $groupID, string $member): array public function pruneChief(int $serviceID, string $userName): array { $this->dataActionLogger->logAction(\Leaf\DataActions::DELETE, \Leaf\LoggableTypes::SERVICE_CHIEF, [ - new \Leaf\LogItem("users", "userID", $userName, $this->getEmployeeDisplay($userName)), - new \Leaf\LogItem("users", "groupID", $serviceID, $this->getServiceName($serviceID)) + new \Leaf\LogItem("service_chiefs", "userID", $userName, $this->getEmployeeDisplay($userName)), + new \Leaf\LogItem("service_chiefs", "serviceID", $serviceID, $this->getServiceName($serviceID)) ]); $vars = array(':serviceID' => $serviceID, diff --git a/libs/logger/DataActionLogger.php b/libs/logger/DataActionLogger.php index e46f27eee..8630f8011 100644 --- a/libs/logger/DataActionLogger.php +++ b/libs/logger/DataActionLogger.php @@ -120,11 +120,8 @@ public function getAllHistoryIDs() public function fetchLogData($filterById, $filterByColumnName, $logType) { - $filterResults = isset($filterById) && isset($filterByColumnName); - - $sqlCreateTemp = " CREATE TEMPORARY TABLE group_logs diff --git a/libs/logger/formatters/ServiceChiefFormatter.php b/libs/logger/formatters/ServiceChiefFormatter.php index d1dfe9f96..210c1800b 100644 --- a/libs/logger/formatters/ServiceChiefFormatter.php +++ b/libs/logger/formatters/ServiceChiefFormatter.php @@ -10,6 +10,10 @@ class ServiceChiefFormatter "variables"=>"userID" ], DataActions::DELETE.'-'.LoggableTypes::SERVICE_CHIEF=> [ + "message"=>"pruned user: %s", + "variables"=>"userID" + ], + DataActions::MODIFY.'-'.LoggableTypes::SERVICE_CHIEF=> [ "message"=>"removed user: %s", "variables"=>"userID" ], From 12cac0ae3598edd1b8933a956dce5e56b375dffe Mon Sep 17 00:00:00 2001 From: Carrie Hanscom Date: Fri, 18 Aug 2023 14:34:58 -0400 Subject: [PATCH 08/22] LEAF 3947 get record userID not logged user for notify requestor events --- LEAF_Request_Portal/sources/FormWorkflow.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LEAF_Request_Portal/sources/FormWorkflow.php b/LEAF_Request_Portal/sources/FormWorkflow.php index 0251ba4d5..9cd9dc673 100644 --- a/LEAF_Request_Portal/sources/FormWorkflow.php +++ b/LEAF_Request_Portal/sources/FormWorkflow.php @@ -1071,7 +1071,7 @@ public function handleEvents(int $workflowID, int $stepID, string $actionType, s $dir = new VAMC_Directory; - $author = $dir->lookupLogin($this->login->getUserID()); + $author = $dir->lookupLogin($approvers[0]['userID']); $email->setSender($author[0]['Email']); // Get backups to requester so they can be notified as well @@ -1123,7 +1123,7 @@ public function handleEvents(int $workflowID, int $stepID, string $actionType, s $dir = new VAMC_Directory; - $author = $dir->lookupLogin($this->login->getUserID()); + $author = $dir->lookupLogin($approvers[0]['userID']); $email->setSender($author[0]['Email']); $eventData = json_decode($event['eventData']); From 108b2246b8d11b60411424672aca292f255756dc Mon Sep 17 00:00:00 2001 From: Shane O Date: Mon, 21 Aug 2023 14:51:42 -0500 Subject: [PATCH 09/22] LEAF-3951 - Remove the json stuff and swap back with the unserialize, add some checks to make sure data is set before trying to unserialize --- LEAF_Request_Portal/sources/FormWorkflow.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LEAF_Request_Portal/sources/FormWorkflow.php b/LEAF_Request_Portal/sources/FormWorkflow.php index 708a373dc..5e9092109 100644 --- a/LEAF_Request_Portal/sources/FormWorkflow.php +++ b/LEAF_Request_Portal/sources/FormWorkflow.php @@ -1223,8 +1223,8 @@ private function getFields(): array switch(true) { case (str_starts_with($format, "grid") != false): - if ($this->isJsonString($data) && is_array(json_decode($data))) { - $data = $this->buildGrid(json_decode($data)); + if(!empty($data) && is_array(unserialize($data))){ + $data = $this->buildGrid(unserialize($data)); } break; case (str_starts_with($format, "checkboxes") != false): From e3cc8c8783637e724f34d1b32ce5c8b99ba5b088 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Wed, 23 Aug 2023 14:03:44 -0400 Subject: [PATCH 10/22] Leaf 3902 - update to sysadmin and elt --- .../admin/templates/mod_groups.tpl | 22 +++++++++++-------- LEAF_Request_Portal/sources/System.php | 6 +---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/LEAF_Request_Portal/admin/templates/mod_groups.tpl b/LEAF_Request_Portal/admin/templates/mod_groups.tpl index cf70fd99c..5abd22cde 100644 --- a/LEAF_Request_Portal/admin/templates/mod_groups.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_groups.tpl @@ -412,15 +412,19 @@ function addAdmin(userID) { } function removeAdmin(userID) { - $.ajax({ - type: 'DELETE', - url: "../api/group/1/members/" + userID, - data: {'CSRFToken': ''}, - success: function(response) { - getMembers(1); - }, - cache: false - }); + if (userID === '') { + return; + } else { + $.ajax({ + type: 'DELETE', + url: "../api/group/" + 1 + "/members/_" + userID + "?" + + $.param({'CSRFToken': ''}), + success: function(response) { + getMembers(1); + }, + cache: false + }); + } } function unsetPrimaryAdmin() { diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 5ff6c134f..5917a656d 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -111,11 +111,7 @@ public function updateService(int $serviceID): array $backups = $this->addBackups($service['groupID'], false); if ($backups['status']['code'] == 2) { - // check if this service is also an ELT - // if so, update groups table - $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); - - if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { + if ($serviceID == $quadID) { $this->updateGroup($serviceID, $oc_db); } else { // make sure this is not in the groups table? From c31057b1e2fae316c85d4d5355137bc128f7a68e Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 15:22:17 -0400 Subject: [PATCH 11/22] Push errors addressed --- LEAF_Request_Portal/sources/System.php | 102 ++++++++++++++----------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 5ff6c134f..701ce9850 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -65,72 +65,82 @@ public function updateService(int $serviceID): array $leader = $position->findRootPositionByGroupTag($leader_id, $tag_parent); - $quadID = $leader[0]['groupID']; + if (isset($leader[0])) { + $quadID = $leader[0]['groupID']; - $service = $group->getGroup($serviceID)[0]; + $service = $group->getGroup($serviceID)[0]; - $abbrService = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; + $abbrService = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; - $insert_service = $this->insertService($service['groupID'], $service['groupTitle'], $abbrService, $quadID); + $insert_service = $this->insertService($service['groupID'], $service['groupTitle'], $abbrService, $quadID); - if ($insert_service['status']['code'] == 2) { - $delete_chief_backups = $this->deleteChiefs($service['groupID']); + if ($insert_service['status']['code'] == 2) { + $delete_chief_backups = $this->deleteChiefs($service['groupID']); - if ($delete_chief_backups['status']['code'] == 2) { - $leaderGroupID = $group->getGroupLeader($service['groupID']); + if ($delete_chief_backups['status']['code'] == 2) { + $leaderGroupID = $group->getGroupLeader($service['groupID']); - $resEmp = $position->getEmployees($leaderGroupID); + $resEmp = $position->getEmployees($leaderGroupID); - $return_value = array( - 'status' => array( - 'code' => 2, - 'message' => '' - ) - ); - - foreach ($resEmp as $emp) { - if ($emp['userName'] != '') { - $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); - - if ($insert_chief['status']['code'] == 2) { - // nothing to do here, just keep going - } else { - $return_value = array( - 'status' => array( - 'code' => 4, - 'message' => 'Chief unable to be added' - ) - ); + $return_value = array( + 'status' => array( + 'code' => 2, + 'message' => '' + ) + ); - break; + foreach ($resEmp as $emp) { + if ($emp['userName'] != '') { + $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); + + if ($insert_chief['status']['code'] == 2) { + // nothing to do here, just keep going + } else { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Chief unable to be added' + ) + ); + + break; + } } } - } - if ($return_value['status']['code'] == 2) { - $backups = $this->addBackups($service['groupID'], false); + if ($return_value['status']['code'] == 2) { + $backups = $this->addBackups($service['groupID'], false); - if ($backups['status']['code'] == 2) { - // check if this service is also an ELT - // if so, update groups table - $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); + if ($backups['status']['code'] == 2) { + // check if this service is also an ELT + // if so, update groups table + $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); - if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { - $this->updateGroup($serviceID, $oc_db); + if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { + $this->updateGroup($serviceID, $oc_db); + } else { + // make sure this is not in the groups table? + $this->removeGroup($serviceID); + } } else { - // make sure this is not in the groups table? - $this->removeGroup($serviceID); + $return_value = $backups; } - } else { - $return_value = $backups; } + } else { + $return_value = $delete_chief_backups; } } else { - $return_value = $delete_chief_backups; + $return_value = $insert_service; } } else { - $return_value = $insert_service; + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Chief unable to be added' + ) + ); } + } return $return_value; @@ -923,7 +933,7 @@ private function insertService(int $serviceID, string $title, string $abbr, int $sql = 'INSERT INTO `services` (`serviceID`, `service`, `abbreviatedService`, `groupID`) VALUES (:serviceID, :service, :abbrService, :groupID) - ON DUPLICATE KEY UPDATE `service` = :service, `groupID` = :groupID, + ON DUPLICATE KEY UPDATE `serviceID` = :serviceID, `groupID` = :groupID, `abbreviatedService` = :abbrService'; $return_value = $this->db->pdo_insert_query($sql, $vars); From 0e4c55297dd3b8f01f169ea2033be984da64eed6 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 15:47:19 -0400 Subject: [PATCH 12/22] query switched back --- LEAF_Request_Portal/sources/System.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 701ce9850..10858d205 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -933,7 +933,7 @@ private function insertService(int $serviceID, string $title, string $abbr, int $sql = 'INSERT INTO `services` (`serviceID`, `service`, `abbreviatedService`, `groupID`) VALUES (:serviceID, :service, :abbrService, :groupID) - ON DUPLICATE KEY UPDATE `serviceID` = :serviceID, `groupID` = :groupID, + ON DUPLICATE KEY UPDATE `service` = :service, `groupID` = :groupID, `abbreviatedService` = :abbrService'; $return_value = $this->db->pdo_insert_query($sql, $vars); From b3f69850488a5eb21adc43ecc569f8f97d3e3024 Mon Sep 17 00:00:00 2001 From: Pete Nerantzinis <17956374+pete-nerantzinis@users.noreply.github.com> Date: Thu, 24 Aug 2023 16:27:15 -0400 Subject: [PATCH 13/22] DB Fix --- .../Update_RMC_DB_2023082400-2023082401.sql | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023082400-2023082401.sql diff --git a/docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023082400-2023082401.sql b/docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023082400-2023082401.sql new file mode 100644 index 000000000..924ea10c4 --- /dev/null +++ b/docker/mysql/db/db_upgrade/portal/Update_RMC_DB_2023082400-2023082401.sql @@ -0,0 +1,18 @@ +START TRANSACTION; + +ALTER TABLE `services` DROP INDEX `service`; + +UPDATE `settings` SET `data` = '2023082401' WHERE `settings`.`setting` = 'dbversion'; + +COMMIT; + + +/**** Revert DB ***** +START TRANSACTION; + +ALTER TABLE `services` ADD UNIQUE `service` (`service`); + +UPDATE `settings` SET `data` = '2023082400' WHERE `settings`.`setting` = 'dbversion'; + +COMMIT; +*/ From fe8396be22835a40d5ae3a881b2e66ab90e2f8f9 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 16:28:26 -0400 Subject: [PATCH 14/22] removing services to start --- LEAF_Request_Portal/sources/System.php | 168 +++++++++++++++++-------- 1 file changed, 114 insertions(+), 54 deletions(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 10858d205..393590566 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -60,87 +60,95 @@ public function updateService(int $serviceID): array $position = new \Orgchart\Position($oc_db, $this->login); $tag = new \Orgchart\Tag($oc_db, $this->login); - $leader_id = $group->getGroupLeader($serviceID); - $tag_parent = $tag->getParent('service'); + $removeServices = $this->removeService($serviceID); - $leader = $position->findRootPositionByGroupTag($leader_id, $tag_parent); + if ($removeServices['status']['code'] == 2) { + $leader_id = $group->getGroupLeader($serviceID); + $tag_parent = $tag->getParent('service'); - if (isset($leader[0])) { - $quadID = $leader[0]['groupID']; + $leader = $position->findRootPositionByGroupTag($leader_id, $tag_parent); - $service = $group->getGroup($serviceID)[0]; + if (isset($leader[0])) { + $quadID = $leader[0]['groupID']; - $abbrService = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; + $service = $group->getGroup($serviceID)[0]; - $insert_service = $this->insertService($service['groupID'], $service['groupTitle'], $abbrService, $quadID); + $abbrService = isset($service['groupAbbreviation']) ? $service['groupAbbreviation'] : ''; - if ($insert_service['status']['code'] == 2) { - $delete_chief_backups = $this->deleteChiefs($service['groupID']); + $insert_service = $this->insertService($service['groupID'], $service['groupTitle'], $abbrService, $quadID); - if ($delete_chief_backups['status']['code'] == 2) { - $leaderGroupID = $group->getGroupLeader($service['groupID']); + if ($insert_service['status']['code'] == 2) { + $delete_chief_backups = $this->deleteChiefs($service['groupID']); - $resEmp = $position->getEmployees($leaderGroupID); + if ($delete_chief_backups['status']['code'] == 2) { + $leaderGroupID = $group->getGroupLeader($service['groupID']); - $return_value = array( - 'status' => array( - 'code' => 2, - 'message' => '' - ) - ); + $resEmp = $position->getEmployees($leaderGroupID); - foreach ($resEmp as $emp) { - if ($emp['userName'] != '') { - $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); + $return_value = array( + 'status' => array( + 'code' => 2, + 'message' => '' + ) + ); - if ($insert_chief['status']['code'] == 2) { - // nothing to do here, just keep going - } else { - $return_value = array( - 'status' => array( - 'code' => 4, - 'message' => 'Chief unable to be added' - ) - ); - - break; + foreach ($resEmp as $emp) { + if ($emp['userName'] != '') { + $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); + + if ($insert_chief['status']['code'] == 2) { + // nothing to do here, just keep going + } else { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Chief unable to be added' + ) + ); + + break; + } } } - } - if ($return_value['status']['code'] == 2) { - $backups = $this->addBackups($service['groupID'], false); + if ($return_value['status']['code'] == 2) { + $backups = $this->addBackups($service['groupID'], false); - if ($backups['status']['code'] == 2) { - // check if this service is also an ELT - // if so, update groups table - $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); + if ($backups['status']['code'] == 2) { + // check if this service is also an ELT + // if so, update groups table + $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); - if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { - $this->updateGroup($serviceID, $oc_db); + if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { + $this->updateGroup($serviceID, $oc_db); + } else { + // make sure this is not in the groups table? + $this->removeGroup($serviceID); + } } else { - // make sure this is not in the groups table? - $this->removeGroup($serviceID); + $return_value = $backups; } - } else { - $return_value = $backups; } + } else { + $return_value = $delete_chief_backups; } } else { - $return_value = $delete_chief_backups; + $return_value = $insert_service; } } else { - $return_value = $insert_service; + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Chief unable to be added' + ) + ); } } else { - $return_value = array( - 'status' => array( - 'code' => 4, - 'message' => 'Chief unable to be added' - ) - ); + $return_value = $removeServices; } + + } return $return_value; @@ -771,12 +779,14 @@ public function getHistory($filterById) public function syncSystem(\Orgchart\Group $nexus_group): string { // update services and service chiefs + $this->removeServices(); $services = $nexus_group->listGroupsByTag('service'); foreach ($services as $service) { $this->updateService($service['groupID']); } + $this->removeGroups(); $groups = $this->getOrgchartImportTags($nexus_group); $oc_db = new \Leaf\Db(\DIRECTORY_HOST, \DIRECTORY_USER, \DIRECTORY_PASS, \ORGCHART_DB); @@ -810,6 +820,40 @@ private function getOrgchartImportTags(\Orgchart\Group $group): array return $groups; } + private function removeService(int $serviceID): array + { + $vars = array(':serviceID' => $serviceID); + $sql = 'DELETE + FROM `services` + WHERE `serviceID` = :serviceID'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + $sql = 'DELETE + FROM `service_chiefs` + WHERE `serviceID` = :serviceID'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + return $return_value; + } + + private function removeServices(): array + { + $vars = array(); + $sql = 'DELETE + FROM `services`'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + $sql = 'DELETE + FROM `service_chiefs`'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + return $return_value; + } + private function removeGroup(int $groupID): array { $vars = array(':groupID' => $groupID); @@ -828,6 +872,22 @@ private function removeGroup(int $groupID): array return $return_value; } + private function removeGroups(): array + { + $vars = array(); + $sql = 'DELETE + FROM `groups`'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + $sql = 'DELETE + FROM `users`'; + + $return_value = $this->db->pdo_delete_query($sql, $vars); + + return $return_value; + } + /** * @param int $groupID * From 19a61acdf174ae5bbb62ae1e993004a0d8e3f89b Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 16:41:10 -0400 Subject: [PATCH 15/22] locallyManaged kept --- LEAF_Request_Portal/sources/System.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 393590566..4c8959ede 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -847,7 +847,8 @@ private function removeServices(): array $return_value = $this->db->pdo_delete_query($sql, $vars); $sql = 'DELETE - FROM `service_chiefs`'; + FROM `service_chiefs` + WHERE `locallyManaged` = 0'; $return_value = $this->db->pdo_delete_query($sql, $vars); @@ -881,7 +882,8 @@ private function removeGroups(): array $return_value = $this->db->pdo_delete_query($sql, $vars); $sql = 'DELETE - FROM `users`'; + FROM `users` + WHERE `locallyManaged` = 0'; $return_value = $this->db->pdo_delete_query($sql, $vars); From e941c218aeeb78e15f2d9c1861e06246c2de47d3 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 16:52:50 -0400 Subject: [PATCH 16/22] sysadmins stay --- LEAF_Request_Portal/sources/System.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 4c8959ede..2eb98a882 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -883,7 +883,8 @@ private function removeGroups(): array $sql = 'DELETE FROM `users` - WHERE `locallyManaged` = 0'; + WHERE `locallyManaged` = 0 + AND `groupID` <> 1'; $return_value = $this->db->pdo_delete_query($sql, $vars); From 5342d47282e382b6cf2e82b703e991bf0f67ca24 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 17:02:06 -0400 Subject: [PATCH 17/22] service chiefs locallymanaged --- LEAF_Request_Portal/sources/Service.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/LEAF_Request_Portal/sources/Service.php b/LEAF_Request_Portal/sources/Service.php index 2205f5470..cd5032b50 100644 --- a/LEAF_Request_Portal/sources/Service.php +++ b/LEAF_Request_Portal/sources/Service.php @@ -265,7 +265,8 @@ public function deactivateChief(int $groupID, string $member): array $vars = array(':userID' => $member, ':serviceID' => $groupID, ); $sql = 'UPDATE `service_chiefs` - SET `active` = 0 + SET `active` = 0, + `locallyManaged` = 1 WHERE `serviceID` = :serviceID AND (`userID` = :userID OR `backupID` = :userID)'; @@ -273,7 +274,8 @@ public function deactivateChief(int $groupID, string $member): array $this->db->prepared_query($sql, $vars); $sql = 'UPDATE `users` - SET `active` = 0 + SET `active` = 0, + `locallyManaged` = 1 WHERE `groupID` = :serviceID AND (`userID` = :userID OR `backupID` = :userID)'; From 64b3959db483c1e4cc36094fe5b0403c1319a4cd Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 17:17:04 -0400 Subject: [PATCH 18/22] keeping elt groups --- LEAF_Request_Portal/sources/System.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 2eb98a882..0c7e1bd26 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -877,7 +877,8 @@ private function removeGroups(): array { $vars = array(); $sql = 'DELETE - FROM `groups`'; + FROM `groups` + WHERE `groupID` < 2'; $return_value = $this->db->pdo_delete_query($sql, $vars); From 013941b0683d95b6c9124bed31e1c6eb0a50a88d Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 17:24:01 -0400 Subject: [PATCH 19/22] oops --- LEAF_Request_Portal/sources/System.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 0c7e1bd26..ac49a0431 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -878,7 +878,7 @@ private function removeGroups(): array $vars = array(); $sql = 'DELETE FROM `groups` - WHERE `groupID` < 2'; + WHERE `groupID` > 1'; $return_value = $this->db->pdo_delete_query($sql, $vars); From b96472318150f83bee1cd7a7bbd169587ad1c190 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 17:31:27 -0400 Subject: [PATCH 20/22] locally managed sc --- LEAF_Request_Portal/sources/System.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index ac49a0431..9204ccd31 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -831,7 +831,8 @@ private function removeService(int $serviceID): array $sql = 'DELETE FROM `service_chiefs` - WHERE `serviceID` = :serviceID'; + WHERE `serviceID` = :serviceID + AND `locallyManaged` = 0'; $return_value = $this->db->pdo_delete_query($sql, $vars); From 4a0b73e979ebcecb8239fde3c4791b6a14994de3 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Thu, 24 Aug 2023 18:00:22 -0400 Subject: [PATCH 21/22] dont delete chiefs --- LEAF_Request_Portal/sources/System.php | 76 ++++++++++++-------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/LEAF_Request_Portal/sources/System.php b/LEAF_Request_Portal/sources/System.php index 9204ccd31..2ca67c0e8 100644 --- a/LEAF_Request_Portal/sources/System.php +++ b/LEAF_Request_Portal/sources/System.php @@ -78,59 +78,53 @@ public function updateService(int $serviceID): array $insert_service = $this->insertService($service['groupID'], $service['groupTitle'], $abbrService, $quadID); if ($insert_service['status']['code'] == 2) { - $delete_chief_backups = $this->deleteChiefs($service['groupID']); + $leaderGroupID = $group->getGroupLeader($service['groupID']); - if ($delete_chief_backups['status']['code'] == 2) { - $leaderGroupID = $group->getGroupLeader($service['groupID']); + $resEmp = $position->getEmployees($leaderGroupID); - $resEmp = $position->getEmployees($leaderGroupID); + $return_value = array( + 'status' => array( + 'code' => 2, + 'message' => '' + ) + ); - $return_value = array( - 'status' => array( - 'code' => 2, - 'message' => '' - ) - ); + foreach ($resEmp as $emp) { + if ($emp['userName'] != '') { + $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); - foreach ($resEmp as $emp) { - if ($emp['userName'] != '') { - $insert_chief = $this->insertChief($emp['userName'], $service['groupID']); - - if ($insert_chief['status']['code'] == 2) { - // nothing to do here, just keep going - } else { - $return_value = array( - 'status' => array( - 'code' => 4, - 'message' => 'Chief unable to be added' - ) - ); - - break; - } + if ($insert_chief['status']['code'] == 2) { + // nothing to do here, just keep going + } else { + $return_value = array( + 'status' => array( + 'code' => 4, + 'message' => 'Chief unable to be added' + ) + ); + + break; } } + } - if ($return_value['status']['code'] == 2) { - $backups = $this->addBackups($service['groupID'], false); + if ($return_value['status']['code'] == 2) { + $backups = $this->addBackups($service['groupID'], false); - if ($backups['status']['code'] == 2) { - // check if this service is also an ELT - // if so, update groups table - $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); + if ($backups['status']['code'] == 2) { + // check if this service is also an ELT + // if so, update groups table + $tagged = $tag->groupIsTagged($serviceID, Config::$orgchartImportTags[0]); - if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { - $this->updateGroup($serviceID, $oc_db); - } else { - // make sure this is not in the groups table? - $this->removeGroup($serviceID); - } + if ($serviceID == $quadID && $tagged['status']['code'] == 2 && !empty($tagged['data'])) { + $this->updateGroup($serviceID, $oc_db); } else { - $return_value = $backups; + // make sure this is not in the groups table? + $this->removeGroup($serviceID); } + } else { + $return_value = $backups; } - } else { - $return_value = $delete_chief_backups; } } else { $return_value = $insert_service; From 312cf81db87f3abdadb5d81fdbd85c1e19026394 Mon Sep 17 00:00:00 2001 From: Jamie P Holcomb Date: Fri, 25 Aug 2023 09:53:42 -0400 Subject: [PATCH 22/22] async false needed --- LEAF_Request_Portal/admin/templates/mod_svcChief.tpl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl b/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl index d92daaa27..26ad3b02c 100644 --- a/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl +++ b/LEAF_Request_Portal/admin/templates/mod_svcChief.tpl @@ -199,6 +199,7 @@ function removeUser(groupID = -1, userID = '') { return; } else { $.ajax({ + async: false, type: 'POST', url: "../api/service/" + groupID + "/members/_" + userID, data: {'CSRFToken': ''}, @@ -420,6 +421,7 @@ function pruneMember(groupID, userID) { return; } else { $.ajax({ + async: false, type: 'POST', url: "../api/service/" + groupID + "/members/_" + userID + "/prune", data: {'CSRFToken': ''}, @@ -437,6 +439,7 @@ function reactivateMember(groupID, userID) { return; } else { $.ajax({ + async: false, type: 'POST', url: "../api/service/" + groupID + "/members/_" + userID + "/reactivate", data: {'CSRFToken': ''},