update function for rewrite permissions

This commit is contained in:
Lamrani Abdel
2023-03-16 10:40:18 +01:00
parent 2a7583ca14
commit 6fa3e7417d
2 changed files with 94 additions and 134 deletions

View File

@@ -2146,6 +2146,7 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Rights")), null, 'errors');
}
$id = GETPOST('id', 'alpha');
$label = GETPOST('label', 'alpha');
$objectForPerms = strtolower(GETPOST('permissionObj', 'alpha'));
$crud = GETPOST('crud', 'alpha');
@@ -2168,8 +2169,6 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
$counter = 0;
$permsForObject =array();
$permissions = $moduleobj->rights;
$firstRight = 0;
$existRight = 0;
$allObject = array();
$countPerms = count($permissions);
@@ -2188,55 +2187,34 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
$countPermsObj = count($permsForObject);
for ($j = 0; $j<$countPermsObj; $j++) {
if (in_array($label, $permsForObject[$j])) {
$existRight++;
$error++;
setEventMessages($langs->trans("ErrorExistingPermission", $langs->transnoentities($label), $langs->transnoentities($objectForPerms)), null, 'errors');
}
}
// if not found permission for the object
if (!in_array($objectForPerms, array_unique($allObject))) {
$firstRight++;
$existRight++;
}
if (!$error) {
$key = $countPerms + 1;
//prepare right to add
$rightToAdd = [
0=> $id,
1=>$label,
4=>$objectForPerms,
5=>$crud
];
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
//rewriting all permissions after add a right
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, 1);
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
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('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings');
}
//prepare stirng to add
$rightToAdd = "
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = '$label';
\$this->rights[\$r][4] = '$objectForPerms';
\$this->rights[\$r][5] = '$crud';
\$r++;
";
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
//var_dump($existRight.' '.$firstRight);exit;
if (!$existRight) {
dolReplaceInFile($moduledescriptorfile, array('/*END '.strtoupper($objectForPerms).'*/' => $rightToAdd.'/*END '.strtoupper($objectForPerms).'*/'));
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
}
if ($firstRight > 0) {
$filecontentbefore = file_get_contents($moduledescriptorfile);
$result = dolReplaceInFile($moduledescriptorfile, array('/* END MODULEBUILDER PERMISSIONS */' => '/*'.strtoupper($objectForPerms).'*/'.$rightToAdd."/*END ".strtoupper($objectForPerms).'*/'."\n\t\t".'/* END MODULEBUILDER PERMISSIONS */'));
$filecontentafter = file_get_contents($moduledescriptorfile);
if ($filecontentbefore != $filecontentafter) {
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
} else {
setEventMessages($langs->trans('FailedToAddCodeIntoDescriptor', 'END MODULEBUILDER PERMISSIONS'), null, 'warnings');
}
}
rewritingAllPermissions($moduledescriptorfile, $objectForPerms, $right, '', 1);
}
clearstatcache(true);
@@ -2293,11 +2271,11 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
}
$permissions = $moduleobj->rights;
$r =(int) GETPOST('counter');
$key =(int) GETPOST('counter')-1;
//get permission want to delete from permissions array
$x1 = $permissions[$r-1][1];
$x2 = $permissions[$r-1][4];
$x3 = $permissions[$r-1][5];
$x1 = $permissions[$key][1];
$x2 = $permissions[$key][4];
$x3 = $permissions[$key][5];
//check existing object permission
$counter = 0;
$permsForObject =array();
@@ -2327,24 +2305,6 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
}
}
// TODO ALI Update of permission must be done by rewriting completely the permission section
//prepare right want to delete
$right = "
\$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++;
";
// right after editing
$rightUpdated = "
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = '$label';
\$this->rights[\$r][4] = '$objectForPerms';
\$this->rights[\$r][5] = '$crud';
\$r++;
";
if (!$error) {
if (isModEnabled(strtolower($module))) {
$result = unActivateModule(strtolower($module));
@@ -2352,13 +2312,12 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
if ($result) {
setEventMessages($result, null, 'errors');
}
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module);
setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings');
}
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
rewritingAllPermissions($moduledescriptorfile, $x2, $right, $rightUpdated, 2);
// rewriting all permissions after update permission needed
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightUpdated, 2);
setEventMessages($langs->trans('PermissionUpdatedSuccesfuly'), null);
@@ -2389,27 +2348,13 @@ if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('p
$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';
//reriting all permissions
rewritingAllPermissions($moduledescriptorfile, $x2, $rightTodelete, '', 0);
if (!$error) {
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
// rewriting all permissions
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, '', 0);
// check if module is enabled
if (isModEnabled(strtolower($module))) {
$result = unActivateModule(strtolower($module));