getRolesList(); // Start of the file $phpCode = " $values) { $phpCode .= " '{$key}' => [\n"; foreach ($values as $subkey => $value) { $phpCode .= " '{$subkey}' => '{$value}',\n"; } $phpCode .= " ],\n"; } // Close the array $phpCode .= "];\n"; // Write PHP code to a file $filePath = APPPATH . "Config/RBAC/roles.php"; file_put_contents($filePath, $phpCode); } } if (!function_exists('generate_php_permissions_constant')) { function generate_php_permissions_constant() { // Generate the array of keys $array = (new PermisosModel())->getPermissionsList(); // Start of the file $phpCode = " $value) { // Using single quotes to ensure keys/values are emitted as literals $phpCode .= "\t'{$key}' => '{$value}',\n"; } // Close the array $phpCode .= "];\n"; // Write PHP code to a file $filePath = APPPATH . "Config/RBAC/permissions.php"; file_put_contents($filePath, $phpCode); } } if (!function_exists('generate_php_permissions_matrix_constant')) { function generate_php_permissions_matrix_constant() { $matrix = (new GroupModel())->getRolesPermissionMatrix(); // Start of the file $phpCode = " $permissions) { $phpCode .= "\t\"{$role}\" => [\n"; foreach ($permissions as $permission) { $phpCode .= "\t\t\"{$permission}\",\n"; } $phpCode .= "\t],\n"; } // Close the array $phpCode .= "];\n"; // Write PHP code to a file $filePath = APPPATH . "Config/RBAC/permissionMatrix.php"; file_put_contents($filePath, $phpCode); } } if (!function_exists('checkPermission')) { /** * Checks if the user has the required permission and redirects accordingly. * * @param string $sectionPermission The permission to check. * @param string|null $redirectRoute The route to redirect to if the permission is not granted. * @return RedirectResponse|null Redirect response if the user does not have permission, null otherwise. */ function checkPermission(string $sectionPermission, string $redirectRoute = null) { $session = \Config\Services::session(); $response = \Config\Services::response(); if (!auth()->user()->can($sectionPermission)) { $session->setFlashdata('errorMessage', "No tiene permisos de acceso"); $route = $redirectRoute ?? 'home'; return $response->redirect(route_to($route)); } return null; } }