Merge pull request #23772 from lamrani002/DeleteRightManually

NEW functionality "Delete right manually" on ModuleBuilder
This commit is contained in:
Laurent Destailleur 2023-02-08 16:02:29 +01:00 committed by GitHub
commit 2dd4aac4c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1994,8 +1994,9 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
$existRight = 0;
$allObject = array();
$nbOfPermissions = count($permissions);
for ($i =0; $i<$nbOfPermissions; $i++) {
$countPerms = count($permissions);
for ($i =0; $i<$countPerms; $i++) {
if ($permissions[$i][4] == $objectForPerms) {
$counter++;
if (count($permsForObject) < 3) {
@ -2004,9 +2005,10 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
}
$allObject[] = $permissions[$i][4];
}
$nbOfpermsInObj = count($permsForObject);
// check if label of object already exists
for ($j = 0; $j<$nbOfpermsInObj; $j++) {
$countPermsObj = count($permsForObject);
for ($j = 0; $j<$countPermsObj; $j++) {
if (in_array($label, $permsForObject[$j])) {
$existRight++;
setEventMessages($langs->trans("ErrorExistingPermission", $langs->transnoentities($label), $langs->transnoentities($objectForPerms)), null, 'errors');
@ -2037,6 +2039,7 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
";
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
if (!$existRight) {
//var_dump(1);exit;
dolReplaceInFile($moduledescriptorfile, array('/*END '.strtoupper($objectForPerms).'*/' => $rightToAdd.'/*END '.strtoupper($objectForPerms).'*/'));
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
}
@ -2099,13 +2102,18 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
$x1 = $permissions[$r-1][1];
$x2 = $permissions[$r-1][4];
$x3 = $permissions[$r-1][5];
//check existing object permission
$permsForObject =array();
//check existing object permission
$counter = 0;
$permsForObject =array();
$permissions = $moduleobj->rights;
$firstRight = 0;
$existRight = 0;
$allObject = array();
$allObject = array();
$nbOfPermissions = count($permissions);
for ($i =0; $i<$nbOfPermissions; $i++) {
$countPerms = count($permissions);
for ($i =0; $i<$countPerms; $i++) {
if ($permissions[$i][4] == $objectForPerms) {
$counter++;
if (count($permsForObject) < 3) {
$permsForObject[] = $permissions[$i];
}
@ -2114,8 +2122,8 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
}
if ($label != $x1 && $crud != $x3) {
$x = count($permsForObject);
for ($j = 0; $j<$x; $j++) {
$countPermsObj = count($permsForObject);
for ($j = 0; $j<$countPermsObj; $j++) {
if (in_array($label, $permsForObject[$j])) {
$error++;
setEventMessages($langs->trans("ErrorExistingPermission", $langs->transnoentities($label), $langs->transnoentities($objectForPerms)), null, 'errors');
@ -2158,6 +2166,70 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
exit;
}
}
// Delete permission
if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('permskey', 'int')) {
$error = 0;
// load class and check if right exist
$pathtofile = $listofmodules[strtolower($module)]['moduledescriptorrelpath'];
dol_include_once($pathtofile);
$class = 'mod'.$module;
if (class_exists($class)) {
try {
$moduleobj = new $class($db);
} catch (Exception $e) {
$error++;
dol_print_error($db, $e->getMessage());
}
}
$permissions = $moduleobj->rights;
$key = (int) GETPOST('permskey', 'int')-1;
//get permission want to delete from permissions array
$x1 = $permissions[$key][1];
$x2 = $permissions[$key][4];
$x3 = $permissions[$key][5];
//prepare right want to delete
$rightTodelete = "
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = '$x1';
\$this->rights[\$r][4] = '$x2';
\$this->rights[\$r][5] = '$x3';
\$r++;
";
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
$check = dolReplaceInFile($moduledescriptorfile, array($rightTodelete => ''."\n\t\t"));
if ($check > 0) {
//check if all permissions of object was deleted
$permsForObj = array();
foreach ($permissions as $perms) {
$permsForObj[] = $perms[4];
}
$permsForObj = array_count_values($permsForObj);
if ($permsForObj[$permissions[$key][4]] == 1) {
$delObjStart = dolReplaceInFile($moduledescriptorfile, array('/*'.strtoupper($permissions[$key][4].'*/') => '','/*END '.strtoupper($permissions[$key][4].'*/') => ''));
}
}
if (!$error) {
// check if module is enabled
if (isModEnabled(strtolower($module))) {
$result = unActivateModule(strtolower($module));
dolibarr_set_const($db, "MAIN_IHM_PARAMS_REV", (int) $conf->global->MAIN_IHM_PARAMS_REV + 1, 'chaine', 0, '', $conf->entity);
if ($result) {
setEventMessages($result, null, 'errors');
}
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module);
setEventMessages($langs->trans('PermissionDeletedSuccesfuly'), null);
setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings');
exit;
} else {
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module);
setEventMessages($langs->trans('PermissionDeletedSuccesfuly'), null);
exit;
}
}
}
// Save file
if ($action == 'savefile' && empty($cancel)) {
$relofcustom = basename($dirins);