2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Buffer;
|
2026-03-06 11:28:22 +01:00
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
2026-02-04 23:31:53 +01:00
|
|
|
use MintyPHP\Router;
|
2026-02-24 08:49:40 +01:00
|
|
|
use MintyPHP\Service\Access\RoleAuthorizationPolicy;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\Support\Flash;
|
|
|
|
|
use MintyPHP\Support\Guard;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-06 11:28:22 +01:00
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
2026-02-04 23:31:53 +01:00
|
|
|
Guard::requireLogin();
|
2026-03-04 15:56:58 +01:00
|
|
|
$request = requestInput();
|
2026-03-11 23:32:11 +01:00
|
|
|
$returnTarget = requestResolveReturnTarget();
|
|
|
|
|
$closeTarget = requestResolveReturnTarget('admin/roles');
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-06 11:28:22 +01:00
|
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
2026-03-04 15:56:58 +01:00
|
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
|
|
|
|
$permissionRepository = app(\MintyPHP\Repository\Access\PermissionRepository::class);
|
|
|
|
|
$rolePermissionRepository = app(\MintyPHP\Repository\Access\RolePermissionRepository::class);
|
|
|
|
|
$userRoleRepository = app(\MintyPHP\Repository\Access\UserRoleRepository::class);
|
|
|
|
|
$userWriteRepository = app(\MintyPHP\Repository\User\UserWriteRepository::class);
|
2026-02-04 23:31:53 +01:00
|
|
|
$uuid = trim((string) ($id ?? ''));
|
2026-03-11 23:32:11 +01:00
|
|
|
$editTarget = requestPathWithReturnTarget("admin/roles/edit/{$uuid}", $returnTarget);
|
2026-03-04 15:56:58 +01:00
|
|
|
$role = $uuid !== '' ? app(\MintyPHP\Service\Access\RoleService::class)->findByUuid($uuid) : null;
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$role) {
|
2026-03-11 23:32:11 +01:00
|
|
|
Flash::error('Role not found', $closeTarget, 'role_not_found');
|
|
|
|
|
Router::redirect($closeTarget);
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$roleId = (int) ($role['id'] ?? 0);
|
2026-02-24 08:49:40 +01:00
|
|
|
$contextDecision = $authorizationService->authorize(RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => $currentUserId,
|
|
|
|
|
'target_role_id' => $roleId,
|
|
|
|
|
]);
|
|
|
|
|
if (!$contextDecision->isAllowed()) {
|
|
|
|
|
Guard::deny();
|
|
|
|
|
}
|
|
|
|
|
$capabilities = $contextDecision->attribute('capabilities', []);
|
|
|
|
|
$canUpdateRole = is_array($capabilities) ? (bool) ($capabilities['can_edit_role'] ?? false) : false;
|
|
|
|
|
$canDeleteRole = is_array($capabilities) ? (bool) ($capabilities['can_delete_role'] ?? false) : false;
|
|
|
|
|
|
2026-03-26 10:03:27 +01:00
|
|
|
app(\MintyPHP\Service\Audit\AuditMetadataEnricherInterface::class)->enrich($role);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$errorBag = formErrors();
|
2026-02-04 23:31:53 +01:00
|
|
|
$errors = [];
|
2026-03-22 18:25:33 +01:00
|
|
|
$warnings = [];
|
2026-02-04 23:31:53 +01:00
|
|
|
$form = $role;
|
2026-02-23 12:58:19 +01:00
|
|
|
$permissions = $permissionRepository->listActive();
|
|
|
|
|
$selectedPermissionIds = $rolePermissionRepository->listPermissionIdsByRoleId($roleId);
|
2026-03-13 21:11:48 +01:00
|
|
|
$assignableRoleService = app(\MintyPHP\Service\Access\AssignableRoleService::class);
|
|
|
|
|
$assignableRoleRepo = app(\MintyPHP\Repository\Access\RoleAssignableRoleRepository::class);
|
|
|
|
|
$allRolesForAssignable = app(\MintyPHP\Service\Access\RoleService::class)->listActive();
|
|
|
|
|
$selectedAssignableRoleIds = $assignableRoleRepo->listAssignableRoleIdsByRoleId($roleId);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-04-24 18:31:17 +02:00
|
|
|
if ($request->isMethod('POST') && !actionRequireCsrf($editTarget, $editTarget, 'csrf_expired')) {
|
2026-03-10 15:40:07 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 18:25:33 +01:00
|
|
|
if ($request->isMethod('POST')) {
|
2026-02-24 08:49:40 +01:00
|
|
|
$submitDecision = $authorizationService->authorize(RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_EDIT_SUBMIT, [
|
|
|
|
|
'actor_user_id' => $currentUserId,
|
|
|
|
|
'target_role_id' => $roleId,
|
|
|
|
|
]);
|
|
|
|
|
if (!$submitDecision->isAllowed()) {
|
|
|
|
|
Guard::deny();
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
2026-03-04 15:56:58 +01:00
|
|
|
$result = app(\MintyPHP\Service\Access\RoleService::class)->updateFromAdmin($roleId, $request->bodyAll(), $currentUserId);
|
2026-02-04 23:31:53 +01:00
|
|
|
$form = $result['form'] ?? $form;
|
2026-03-22 18:25:33 +01:00
|
|
|
$warnings = $result['warnings'] ?? [];
|
2026-03-04 15:56:58 +01:00
|
|
|
$errorBag->merge($result['errors'] ?? []);
|
|
|
|
|
$selectedPermissionIds = $request->body('permission_ids', $selectedPermissionIds);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!is_array($selectedPermissionIds)) {
|
|
|
|
|
$selectedPermissionIds = [$selectedPermissionIds];
|
|
|
|
|
}
|
|
|
|
|
$selectedPermissionIds = array_values(array_unique(array_map('intval', $selectedPermissionIds)));
|
2026-03-13 21:11:48 +01:00
|
|
|
$selectedAssignableRoleIds = $request->body('assignable_role_ids', $selectedAssignableRoleIds);
|
|
|
|
|
if (!is_array($selectedAssignableRoleIds)) {
|
|
|
|
|
$selectedAssignableRoleIds = [$selectedAssignableRoleIds];
|
|
|
|
|
}
|
|
|
|
|
$selectedAssignableRoleIds = array_values(array_unique(array_map('intval', $selectedAssignableRoleIds)));
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
if (($result['ok'] ?? false) && !$errorBag->hasAny()) {
|
|
|
|
|
$permissionIds = $request->body('permission_ids', []);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!is_array($permissionIds)) {
|
|
|
|
|
$permissionIds = [$permissionIds];
|
|
|
|
|
}
|
|
|
|
|
$permissionIds = array_values(array_unique(array_map('intval', $permissionIds)));
|
2026-03-09 19:54:58 +01:00
|
|
|
$syncSucceeded = false;
|
2026-03-09 19:16:26 +01:00
|
|
|
$dbSession = app(\MintyPHP\Repository\Support\DatabaseSessionRepository::class);
|
|
|
|
|
$dbSession->beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$rolePermissionRepository->replaceForRole($roleId, $permissionIds);
|
2026-03-13 21:11:48 +01:00
|
|
|
$assignableRoleService->replaceAssignableRoles($roleId, $selectedAssignableRoleIds, $currentUserId);
|
2026-03-09 19:16:26 +01:00
|
|
|
$affectedUserIds = $userRoleRepository->listUserIdsByRoleIds([$roleId]);
|
|
|
|
|
if ($affectedUserIds) {
|
|
|
|
|
$userWriteRepository->bumpAuthzVersionByUserIds($affectedUserIds);
|
|
|
|
|
}
|
|
|
|
|
$dbSession->commitTransaction();
|
2026-03-09 19:54:58 +01:00
|
|
|
$syncSucceeded = true;
|
2026-03-09 19:16:26 +01:00
|
|
|
} catch (\Throwable) {
|
|
|
|
|
try {
|
|
|
|
|
$dbSession->rollbackTransaction();
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
}
|
|
|
|
|
$errorBag->merge([t('Failed to update role permissions')]);
|
2026-03-04 15:56:58 +01:00
|
|
|
}
|
2026-03-09 19:54:58 +01:00
|
|
|
if ($syncSucceeded) {
|
|
|
|
|
$action = (string) $request->body('action', 'save');
|
|
|
|
|
if ($action === 'save_close') {
|
2026-03-22 18:25:33 +01:00
|
|
|
if ($warnings) {
|
|
|
|
|
Flash::info(implode(' ', $warnings), $closeTarget, 'role_warning');
|
|
|
|
|
}
|
2026-03-11 23:32:11 +01:00
|
|
|
Flash::success('Role updated', $closeTarget, 'role_updated');
|
|
|
|
|
Router::redirect($closeTarget);
|
2026-03-09 19:54:58 +01:00
|
|
|
} else {
|
2026-03-22 18:25:33 +01:00
|
|
|
if ($warnings) {
|
|
|
|
|
Flash::info(implode(' ', $warnings), $editTarget, 'role_warning');
|
|
|
|
|
}
|
2026-03-11 23:32:11 +01:00
|
|
|
Flash::success('Role updated', $editTarget, 'role_updated');
|
|
|
|
|
Router::redirect($editTarget);
|
2026-03-09 19:54:58 +01:00
|
|
|
}
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$validationSummaryErrors = $errorBag->toArray();
|
|
|
|
|
$errors = $errorBag->toFlatList();
|
2026-04-05 17:17:06 +02:00
|
|
|
$titleText = $canUpdateRole ? t('Edit role') : t('View role');
|
|
|
|
|
Buffer::set('title', $titleText);
|
|
|
|
|
$breadcrumbs = [
|
|
|
|
|
['label' => t('Home'), 'path' => 'admin'],
|
|
|
|
|
['label' => t('Roles'), 'path' => 'admin/roles'],
|
|
|
|
|
['label' => $titleText],
|
|
|
|
|
];
|