Merge pull request #24679 from lamrani002/orgnizePermissions
Include all actions that manage permissions in a single function
This commit is contained in:
commit
daa99b989f
@ -496,12 +496,14 @@ function compareFirstValue($a, $b)
|
|||||||
* Rewriting all permissions after any actions
|
* Rewriting all permissions after any actions
|
||||||
* @param string $file filename or path
|
* @param string $file filename or path
|
||||||
* @param array $permissions permissions existing in file
|
* @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 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
|
* @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;
|
$error = 0;
|
||||||
$rights = array();
|
$rights = array();
|
||||||
@ -513,6 +515,42 @@ function reWriteAllPermissions($file, $permissions, $key, $right, $action)
|
|||||||
} elseif ($action == 2 && !empty($right)) {
|
} elseif ($action == 2 && !empty($right)) {
|
||||||
// update right from permissions array
|
// update right from permissions array
|
||||||
array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
|
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 {
|
} else {
|
||||||
$error++;
|
$error++;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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.
|
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".
|
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
|
WarningModuleNeedRefrech = Module <b>%s</b> has been disabled. Don't forget to enable it
|
||||||
|
WarningPermissionAlreadyExist=Existing permissions for this object
|
||||||
|
|
||||||
# Validate
|
# Validate
|
||||||
RequireValidValue = Value not valid
|
RequireValidValue = Value not valid
|
||||||
|
|||||||
@ -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.
|
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".
|
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
|
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
|
# Validate
|
||||||
RequireValidValue = Valeur non valide
|
RequireValidValue = Valeur non valide
|
||||||
|
|||||||
@ -1283,41 +1283,11 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$rights = $moduleobj->rights;
|
$rights = $moduleobj->rights;
|
||||||
$obj = array();
|
$moduledescriptorfile = $destdir.'/core/modules/mod'.$module.'.class.php';
|
||||||
$existRight = 0;
|
|
||||||
foreach ($rights as $right) {
|
|
||||||
$obj[]= $right[4];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (in_array(strtolower($firstobjectname), $obj)) {
|
$generatePerms = reWriteAllPermissions($moduledescriptorfile, $rights, null, null, $objectname, $module, -2);
|
||||||
$rightToadd = preg_replace('/myobject/', $objectname, $rightToadd);
|
if ($generatePerms < 0) {
|
||||||
}
|
setEventMessages($langs->trans("WarningPermissionAlreadyExist", $langs->transnoentities($objectname)), null, 'warnings');
|
||||||
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 */'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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" => ''));
|
$check = dolReplaceInFile($moduledescriptorfile, array('/*LEFTMENU '.strtoupper($objectname).'*/'."\n" => '',"\t\t".'/*END LEFTMENU '.strtoupper($objectname).'*/'."\n" => ''));
|
||||||
|
|
||||||
// regenerate permissions and delete them
|
// regenerate permissions and delete them
|
||||||
$rights = "
|
$permissions = $moduleobj->rights;
|
||||||
\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1);
|
reWriteAllPermissions($moduledescriptorfile, $permissions, null, null, $objectname, '', -1);
|
||||||
\$this->rights[\$r][1] = 'Read objects of ".$module."';
|
clearstatcache(true);
|
||||||
\$this->rights[\$r][4] = '".strtolower($objectname)."';
|
if (function_exists('opcache_invalidate')) {
|
||||||
\$this->rights[\$r][5] = 'read';
|
opcache_reset();
|
||||||
\$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"));
|
|
||||||
|
|
||||||
$resultko = 0;
|
$resultko = 0;
|
||||||
foreach ($filetodelete as $tmpfiletodelete) {
|
foreach ($filetodelete as $tmpfiletodelete) {
|
||||||
$resulttmp = dol_delete_file($dir.'/'.$tmpfiletodelete, 0, 0, 1);
|
$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';
|
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
|
||||||
//rewriting all permissions after add a right
|
//rewriting all permissions after add a right
|
||||||
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, 1);
|
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, '', '', 1);
|
||||||
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
|
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
|
||||||
|
|
||||||
if (isModEnabled(strtolower($module))) {
|
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';
|
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
|
||||||
// rewriting all permissions after update permission needed
|
// rewriting all permissions after update permission needed
|
||||||
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightUpdated, 2);
|
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightUpdated, '', '', 2);
|
||||||
|
|
||||||
setEventMessages($langs->trans('PermissionUpdatedSuccesfuly'), null);
|
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';
|
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
|
||||||
|
|
||||||
// rewriting all permissions
|
// rewriting all permissions
|
||||||
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, '', 0);
|
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, null, '', '', 0);
|
||||||
|
|
||||||
// check if module is enabled
|
// check if module is enabled
|
||||||
if (isModEnabled(strtolower($module))) {
|
if (isModEnabled(strtolower($module))) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user