diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index a60e9a3a78a..5f5dd041ccb 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -416,3 +416,71 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = ' return $error ? -1 : 1; } + +/** + * Rewriting all permissions after updating file + * @param string $file file with path + * @param string $objectname name of the permission object + * @param string $right permission of object + * @param string $rightUpdated permission of object updated + * @param int $action 0 for delete, 1 for add, 2 fo update + * @return void + */ +function rewritingAllPermissions($file, $objectname, $right, $rightUpdated, $action) +{ + global $langs; + $start = "/* BEGIN MODULEBUILDER PERMISSIONS */"; + $end = "/* END MODULEBUILDER PERMISSIONS */"; + $perms_after_action = ''; + $error = 0; + if (empty($right)) { + $error++; + } + if ($action == 2 && empty($rightUpdated)) { + $error++; + } + if (!$error) { + // Open the file and read line by line + $handle = fopen($file, "r"); + $i = 1; + $lines = array(); + + while (($line = fgets($handle)) !== false) { + //search line begin + if (strpos($line, $start) !== false) { + $start_line = $i; + + // Copy lines until the end on array + while (($line = fgets($handle)) !== false) { + if (strpos($line, $end) !== false) { + $end_line = $i; + break; + } + $lines[] = $line; + $i++; + } + break; + } + $i++; + } + $allContent = implode("", $lines); + if (str_contains($allContent, $right)) { + if ($action == 0) { + $perms_after_action = str_replace($right, "\n\t\t", $allContent); + if (!str_contains($perms_after_action, $objectname)) { + $perms_after_action = str_replace(['/*'.strtoupper($objectname).'*/','/*END '.strtoupper($objectname).'*/'], "", $perms_after_action); + } + } + if ($action == 2) { + $perms_after_action = str_replace($right, $rightUpdated."\n\t\t", $allContent); + } + if ($action == 1 && !empty($right)) { + $perms_after_action = str_replace("", $right, $allContent); + var_dump($perms_after_action);exit; + } + dolReplaceInFile($file, array($allContent => '')); + dolReplaceInFile($file, array('/* BEGIN MODULEBUILDER PERMISSIONS */' => '/* BEGIN MODULEBUILDER PERMISSIONS */'."\n".$perms_after_action)); + } + fclose($handle); + } +} diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 04d0f290732..9a5aff0e8d6 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -2235,6 +2235,8 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) { setEventMessages($langs->trans('FailedToAddCodeIntoDescriptor', 'END MODULEBUILDER PERMISSIONS'), null, 'warnings'); } } + + rewritingAllPermissions($moduledescriptorfile, $objectForPerms, $right, '', 1); } clearstatcache(true); @@ -2355,7 +2357,8 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e } $moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php'; - $check = dolReplaceInFile($moduledescriptorfile, array($right => $rightUpdated)); + + rewritingAllPermissions($moduledescriptorfile, $x2, $right, $rightUpdated, 2); setEventMessages($langs->trans('PermissionUpdatedSuccesfuly'), null); @@ -2400,21 +2403,12 @@ if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('p "; $moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php'; + //reriting all permissions + rewritingAllPermissions($moduledescriptorfile, $x2, $rightTodelete, '', 0); + + - // TODO ALI The delete must be done by rewriting all content between /* BEGIN MODULEBUILDER PERMISSIONS */ and /* END MODULEBUILDER PERMISSIONS */ - $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))) {