orgnize function for permissions & simplify the code in index

This commit is contained in:
Lamrani Abdel
2023-05-03 15:33:58 +02:00
parent 4fd94e2ba2
commit 2450819a95
4 changed files with 56 additions and 60 deletions

View File

@@ -496,12 +496,14 @@ function compareFirstValue($a, $b)
* Rewriting all permissions after any actions
* @param string $file filename or path
* @param array $permissions permissions existing in file
* @param int|null $key key for permission needed
* @param int|null $key key for permission needed
* @param array|null $right $right to update or add
* @param int $action 0 for delete, 1 for add, 2 for update
* @param string|null $objectname name of object
* @param string|null $module name of module
* @param int $action 0 for delete, 1 for add, 2 for update, -1 when delete object completly, -2 for generate rights after add
* @return int 1 if OK,-1 if KO
*/
function reWriteAllPermissions($file, $permissions, $key, $right, $action)
function reWriteAllPermissions($file, $permissions, $key, $right, $objectname, $module, $action)
{
$error = 0;
$rights = array();
@@ -513,6 +515,42 @@ function reWriteAllPermissions($file, $permissions, $key, $right, $action)
} elseif ($action == 2 && !empty($right)) {
// update right from permissions array
array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
} elseif ($action == -1 && !empty($objectname)) {
// when delete object
$key = null;
$right = null;
foreach ($permissions as $perms) {
if ($perms[4] === strtolower($objectname)) {
array_splice($permissions, array_search($perms, $permissions), 1);
}
}
} elseif ($action == -2 && !empty($objectname) && !empty($module)) {
$key= null;
$right = null;
$objectOfRights = array();
//check if object already declared in rights file
foreach ($permissions as $right) {
$objectOfRights[]= $right[4];
}
if (in_array(strtolower($objectname), $objectOfRights)) {
$error++;
} else {
$permsToadd = array();
$perms = array(
'read' => 'Read objects of '.ucfirst($module),
'write' => 'Create/Update objects of '.ucfirst($module),
'delete' => 'Delete objects of '.ucfirst($module)
);
$i = 0;
foreach ($perms as $index => $value) {
$permsToadd[$i][0] = '';
$permsToadd[$i][1] = $value;
$permsToadd[$i][4] = strtolower($objectname);
$permsToadd[$i][5] = $index;
array_push($permissions, $permsToadd[$i]);
$i++;
}
}
} else {
$error++;
}

View File

@@ -352,6 +352,7 @@ WarningThemeForcedTo=Warning, theme has been forced to <b>%s</b> by hidden const
WarningPagesWillBeDeleted=Warning, this will also delete all existing pages/containers of the website. You should export your website before, so you have a backup to re-import it later.
WarningAutoValNotPossibleWhenStockIsDecreasedOnInvoiceVal=Automatic validation is disabled when option to decrease stock is set on "Invoice validation".
WarningModuleNeedRefrech = Module <b>%s</b> has been disabled. Don't forget to enable it
WarningPermissionAlreadyExist=Existing permissions for this object
# Validate
RequireValidValue = Value not valid

View File

@@ -350,6 +350,7 @@ WarningThemeForcedTo=Attention, le choix du thème a été forcé à <b> %s </b>
WarningPagesWillBeDeleted=Attention, cela supprimera également toutes les pages/conteneurs existants du site. Vous devriez exporter votre site Web avant, afin d'avoir une sauvegarde pour le réimporter plus tard.
WarningAutoValNotPossibleWhenStockIsDecreasedOnInvoiceVal=La validation automatique est désactivée lorsque l'option de diminution du stock est définie sur "Validation de la facture".
WarningModuleNeedRefrech = Le module <b>%s</b> a été désactivé. N'oubliez pas de le réactiver
WarningPermissionAlreadyExist=Permissions déja éxistante pour cet objet
# Validate
RequireValidValue = Valeur non valide

View File

@@ -1283,41 +1283,11 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
}
}
$rights = $moduleobj->rights;
$obj = array();
$existRight = 0;
foreach ($rights as $right) {
$obj[]= $right[4];
}
$moduledescriptorfile = $destdir.'/core/modules/mod'.$module.'.class.php';
if (in_array(strtolower($firstobjectname), $obj)) {
$rightToadd = preg_replace('/myobject/', $objectname, $rightToadd);
}
if (in_array(strtolower($objectname), $obj)) {
$existRight++;
setEventMessages($langs->trans("PermissionAlreadyExist", $langs->transnoentities($objectname)), null, 'errors');
}
if ($objectname != $firstobjectname) {
$rightToadd = "
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = 'Read objects of ".$module."';
\$this->rights[\$r][4] = '".strtolower($objectname)."';
\$this->rights[\$r][5] = 'read';
\$r++;
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = 'Create/Update objects of ".$module."';
\$this->rights[\$r][4] = '".strtolower($objectname)."';
\$this->rights[\$r][5] = 'write';
\$r++;
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = 'Delete objects of ".$module."';
\$this->rights[\$r][4] = '".strtolower($objectname)."';
\$this->rights[\$r][5] = 'delete';
\$r++;
";
$moduledescriptorfile = $destdir.'/core/modules/mod'.$module.'.class.php';
if (!$existRight) {
dolReplaceInFile($moduledescriptorfile, array('/* END MODULEBUILDER PERMISSIONS */' => '/*'.strtoupper($objectname).'*/'.$rightToadd."/*END ".strtoupper($objectname).'*/'."\n\t\t".'/* END MODULEBUILDER PERMISSIONS */'));
}
$generatePerms = reWriteAllPermissions($moduledescriptorfile, $rights, null, null, $objectname, $module, -2);
if ($generatePerms < 0) {
setEventMessages($langs->trans("WarningPermissionAlreadyExist", $langs->transnoentities($objectname)), null, 'warnings');
}
}
@@ -1923,26 +1893,12 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) {
$check = dolReplaceInFile($moduledescriptorfile, array('/*LEFTMENU '.strtoupper($objectname).'*/'."\n" => '',"\t\t".'/*END LEFTMENU '.strtoupper($objectname).'*/'."\n" => ''));
// regenerate permissions and delete them
$rights = "
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = 'Read objects of ".$module."';
\$this->rights[\$r][4] = '".strtolower($objectname)."';
\$this->rights[\$r][5] = 'read';
\$r++;
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = 'Create/Update objects of ".$module."';
\$this->rights[\$r][4] = '".strtolower($objectname)."';
\$this->rights[\$r][5] = 'write';
\$r++;
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
\$this->rights[\$r][1] = 'Delete objects of ".$module."';
\$this->rights[\$r][4] = '".strtolower($objectname)."';
\$this->rights[\$r][5] = 'delete';
\$r++;
";
$deleteright = dolReplaceInFile($moduledescriptorfile, array('/*'.strtoupper($objectname).'*/' => '', $rights => '', "/*END ".strtoupper($objectname).'*/'."\n\t\t" => "\n\t\t"));
$permissions = $moduleobj->rights;
reWriteAllPermissions($moduledescriptorfile, $permissions, null, null, $objectname, '', -1);
clearstatcache(true);
if (function_exists('opcache_invalidate')) {
opcache_reset();
}
$resultko = 0;
foreach ($filetodelete as $tmpfiletodelete) {
$resulttmp = dol_delete_file($dir.'/'.$tmpfiletodelete, 0, 0, 1);
@@ -2137,7 +2093,7 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
//rewriting all permissions after add a right
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, 1);
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, '', '', 1);
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
if (isModEnabled(strtolower($module))) {
@@ -2250,7 +2206,7 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
// rewriting all permissions after update permission needed
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightUpdated, 2);
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightUpdated, '', '', 2);
setEventMessages($langs->trans('PermissionUpdatedSuccesfuly'), null);
@@ -2286,7 +2242,7 @@ if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('p
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
// rewriting all permissions
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, '', 0);
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, null, '', '', 0);
// check if module is enabled
if (isModEnabled(strtolower($module))) {