diff --git a/workflow/engine/classes/LdapAdvanced.php b/workflow/engine/classes/LdapAdvanced.php index d8daa3ebc..0e98c2e66 100644 --- a/workflow/engine/classes/LdapAdvanced.php +++ b/workflow/engine/classes/LdapAdvanced.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use ProcessMaker\BusinessModel\User; +use ProcessMaker\Model\Department; use ProcessMaker\Model\Groupwf; /** @@ -2593,6 +2594,45 @@ class LdapAdvanced return true; } + /** + * Get department Uid by title. + * @param string $title + * @return string + */ + public function getDepartmentUidByTitle(string $title): string + { + try { + $department = Department::query() + ->where('DEP_STATUS', '=', 'ACTIVE') + ->where('DEP_TITLE', '=', $title) + ->first(); + if (!empty($department)) { + return $department->DEP_UID; + } + } catch (Exception $e) { + $message = $e->getMessage(); + Log::channel(':ldapSynchronizeGroups')->error($message, Bootstrap::context()); + } + return ""; + } + + /** + * Check duplicate titles in DEPARTMENT table. + * @return bool + */ + public function checkDuplicateDepartmentTitles(): bool + { + $sql = "" + . "select DEP_TITLE,count(DEP_TITLE) " + . "from DEPARTMENT " + . "group by DEP_TITLE having count(DEP_TITLE)>1"; + $results = DB::select(DB::raw($sql)); + if (empty($results)) { + return false; + } + return true; + } + /** * Get number of Users in each Group from the Database * diff --git a/workflow/engine/content/translations/english/processmaker.en.po b/workflow/engine/content/translations/english/processmaker.en.po index b35e25d01..445b66900 100755 --- a/workflow/engine/content/translations/english/processmaker.en.po +++ b/workflow/engine/content/translations/english/processmaker.en.po @@ -11105,6 +11105,12 @@ msgstr "is not registered!" msgid "is required" msgstr "is required" +# TRANSLATION +# LABEL/ID_IT_WAS_IDENTIFIED_DUPLICATED_DEPARTMENTS_PLEASE_REMOVE_THESE_DEPARTMENTS +#: LABEL/ID_IT_WAS_IDENTIFIED_DUPLICATED_DEPARTMENTS_PLEASE_REMOVE_THESE_DEPARTMENTS +msgid "It was identified in the workspace duplicated departments, please remove manually these departments." +msgstr "It was identified in the workspace duplicated departments, please remove manually these departments." + # TRANSLATION # LABEL/ID_IT_WAS_IDENTIFIED_DUPLICATED_GROUPS_PLEASE_REMOVE_THESE_GROUPS #: LABEL/ID_IT_WAS_IDENTIFIED_DUPLICATED_GROUPS_PLEASE_REMOVE_THESE_GROUPS diff --git a/workflow/engine/data/mysql/insert.sql b/workflow/engine/data/mysql/insert.sql index bc84e68d3..6ade26f4a 100755 --- a/workflow/engine/data/mysql/insert.sql +++ b/workflow/engine/data/mysql/insert.sql @@ -58714,6 +58714,7 @@ INSERT INTO TRANSLATION (TRN_CATEGORY,TRN_ID,TRN_LANG,TRN_VALUE,TRN_UPDATE_DATE ( 'LABEL','ID_ISSUED_TO','en','Issued to','2014-09-18') , ( 'LABEL','ID_IS_NOT_REGISTERED','en','is not registered!','2014-01-15') , ( 'LABEL','ID_IS_REQUIRED','en','is required','2014-01-15') , +( 'LABEL','ID_IT_WAS_IDENTIFIED_DUPLICATED_DEPARTMENTS_PLEASE_REMOVE_THESE_DEPARTMENTS','en','It was identified in the workspace duplicated departments, please remove manually these departments.','2022-11-23') , ( 'LABEL','ID_IT_WAS_IDENTIFIED_DUPLICATED_GROUPS_PLEASE_REMOVE_THESE_GROUPS','en','It was identified in the workspace duplicated groups, please remove manually these groups.','2020-12-02') , ( 'LABEL','ID_IUD','en','#','2014-01-15') , ( 'LABEL','ID_JAVASCRIPTS','en','JavaScripts','2014-01-15') , diff --git a/workflow/engine/methods/authSources/authSourcesSynchronizeAjax.php b/workflow/engine/methods/authSources/authSourcesSynchronizeAjax.php index e396a22a8..6954ad409 100644 --- a/workflow/engine/methods/authSources/authSourcesSynchronizeAjax.php +++ b/workflow/engine/methods/authSources/authSourcesSynchronizeAjax.php @@ -1,27 +1,4 @@ . - * - * For more information, contact Colosa Inc, 2566 Le Jeune Rd., - * Coral Gables, FL, 33134, USA, or email info@colosa.com. - * - **/ class treeNode extends stdclass { @@ -62,87 +39,82 @@ try { foreach ($depsToCheck as $departmentDn) { $departmentUid = $ldapAdvanced->getDepUidIfExistsDN($departmentDn); - if ($departmentUid == '') { if (strcasecmp($departmentDn, $authenticationSource['AUTH_SOURCE_BASE_DN']) == 0) { $departmentTitle = 'ROOT (' . $authenticationSource['AUTH_SOURCE_BASE_DN'] . ')'; $parentUid = ''; } else { - $arrayAux = custom_ldap_explode_dn($departmentDn); - $departmentCurrent = array_shift($arrayAux); - $parentDn = implode(',', $arrayAux); - - $arrayAux = explode('=', $departmentCurrent); - $departmentTitle = trim($arrayAux[1]); + $ous = custom_ldap_explode_dn($departmentDn); + $departmentCurrent = array_shift($ous); + $parentDn = implode(',', $ous); + $ous = explode('=', $departmentCurrent); + $departmentTitle = trim($ous[1]); $parentUid = $ldapAdvanced->getDepUidIfExistsDN($parentDn); - - if (str_ireplace($authenticationSource['AUTH_SOURCE_BASE_DN'], '', $parentDn) != '' && - $parentUid == '' - ) { + if (str_ireplace($authenticationSource['AUTH_SOURCE_BASE_DN'], '', $parentDn) != '' && $parentUid == '') { $response = new stdClass(); $response->status = 'ERROR'; $response->message = G::LoadTranslation( 'ID_DEPARTMENT_CHECK_PARENT_DEPARTMENT', [$parentDn, $departmentTitle] ); - echo json_encode($response); exit(0); } } + $departmentUid = $ldapAdvanced->getDepartmentUidByTitle($departmentTitle); $department = new Department(); - - $departmentUid = $department->create([ - 'DEP_TITLE' => stripslashes($departmentTitle), - 'DEP_PARENT' => $parentUid, - 'DEP_LDAP_DN' => $departmentDn, - 'DEP_REF_CODE' => '' - ]); - - if ($departmentUid === false) { - $response = new stdClass(); - $response->status = 'ERROR'; - $response->message = G::LoadTranslation('ID_DEPARTMENT_ERROR_CREATE'); - - echo json_encode($response); - exit(0); + if ($departmentUid === '') { + $data = [ + 'DEP_TITLE' => stripslashes($departmentTitle), + 'DEP_PARENT' => $parentUid, + 'DEP_LDAP_DN' => $departmentDn, + 'DEP_REF_CODE' => '' + ]; + $departmentUid = $department->create($data); + if ($departmentUid === false) { + $response = new stdClass(); + $response->status = 'ERROR'; + $response->message = G::LoadTranslation('ID_DEPARTMENT_ERROR_CREATE'); + echo json_encode($response); + exit(0); + } + } else { + $data = $department->Load($departmentUid); + $data['DEP_LDAP_DN'] = $departmentDn; + $department->update($data); } } } - if (!empty($depsToUncheck)) { + if (count($depsToUncheck) > 0) { $baseDnLength = strlen($authenticationSource['AUTH_SOURCE_BASE_DN']); - foreach ($depsToUncheck as $departmentDn) { $departmentUid = $ldapAdvanced->getDepUidIfExistsDN($departmentDn); - - if ($departmentUid != '' && + if ($departmentUid != '' && strcasecmp( - substr($departmentDn, strlen($departmentDn) - $baseDnLength), + substr($departmentDn, strlen($departmentDn) - $baseDnLength), $authenticationSource['AUTH_SOURCE_BASE_DN'] ) == 0 ) { $department = new Department(); - - $arrayDepartmentData = $department->Load($departmentUid); - $arrayDepartmentData['DEP_LDAP_DN'] = ''; - - $result = $department->update($arrayDepartmentData); - + $data = $department->Load($departmentUid); + $data['DEP_LDAP_DN'] = ''; + $department->update($data); if (!isset($authenticationSource['AUTH_SOURCE_DATA']['DEPARTMENTS_TO_UNASSIGN'])) { $authenticationSource['AUTH_SOURCE_DATA']['DEPARTMENTS_TO_UNASSIGN'] = []; } - $authenticationSource['AUTH_SOURCE_DATA']['DEPARTMENTS_TO_UNASSIGN'][] = $departmentUid; } } - $RBAC->authSourcesObj->update($authenticationSource); } $response = new stdclass(); $response->status = "OK"; + if ($ldapAdvanced->checkDuplicateDepartmentTitles()) { + $response->warning = G::LoadTranslation("ID_IT_WAS_IDENTIFIED_DUPLICATED_DEPARTMENTS_PLEASE_REMOVE_THESE_DEPARTMENTS"); + } die(json_encode($response)); break; case "loadGroups": @@ -203,7 +175,7 @@ try { $group["GRP_LDAP_DN"] = ""; $groupwf->update($group); if (!isset($authenticationSource["AUTH_SOURCE_DATA"]["GROUPS_TO_UNASSIGN"])) { - $authenticationSource["AUTH_SOURCE_DATA"]["GROUPS_TO_UNASSIGN"] = array(); + $authenticationSource["AUTH_SOURCE_DATA"]["GROUPS_TO_UNASSIGN"] = []; } $authenticationSource["AUTH_SOURCE_DATA"]["GROUPS_TO_UNASSIGN"][] = $groupUid; }