forked from fa/breadcrumb-the-shire
Move breadcrumb rendering from individual page templates into the core topbar. Each page now sets $breadcrumbs in its action .php file; the topbar renders it automatically via the shared partial. - Remove global back/forward buttons and app-nav-history.js component - Remove Alt+Arrow keyboard shortcuts for history navigation - Render breadcrumb in topbar-left section (replaces button area) - Clean up breadcrumb CSS: context-neutral base (flex, no margin) - Recalculate sticky titlebar offset in details container - Migrate all 41 pages (core + helpdesk, audit, addressbook, api-docs) - Add missing breadcrumbs to addressbook detail view - Update architecture contract tests (nav-history references removed) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
122 lines
5.0 KiB
PHP
122 lines
5.0 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Buffer;
|
|
use MintyPHP\Http\SessionStoreInterface;
|
|
use MintyPHP\Repository\Access\RoleRepository;
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Service\Access\PermissionAuthorizationPolicy;
|
|
use MintyPHP\Session;
|
|
use MintyPHP\Support\Flash;
|
|
use MintyPHP\Support\Guard;
|
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
|
Guard::requireLogin();
|
|
$request = requestInput();
|
|
$returnTarget = requestResolveReturnTarget();
|
|
$closeTarget = requestResolveReturnTarget('admin/permissions');
|
|
|
|
$currentUserId = (int) ($session['user']['id'] ?? 0);
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
|
|
|
$id = (int) ($id ?? 0);
|
|
$editTarget = requestPathWithReturnTarget("admin/permissions/edit/{$id}", $returnTarget);
|
|
$permission = $id > 0 ? app(\MintyPHP\Service\Access\PermissionService::class)->find($id) : null;
|
|
if (!$permission) {
|
|
Flash::error('Permission not found', $closeTarget, 'permission_not_found');
|
|
Router::redirect($closeTarget);
|
|
}
|
|
$isSystemPermission = (int) ($permission['is_system'] ?? 0) === 1;
|
|
$contextDecision = $authorizationService->authorize(PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_EDIT_CONTEXT, [
|
|
'actor_user_id' => $currentUserId,
|
|
'target_permission_id' => $id,
|
|
'target_is_system' => $isSystemPermission,
|
|
]);
|
|
if (!$contextDecision->isAllowed()) {
|
|
Guard::deny();
|
|
}
|
|
$capabilities = $contextDecision->attribute('capabilities', []);
|
|
$canUpdatePermission = is_array($capabilities) ? (bool) ($capabilities['can_update_permission'] ?? false) : false;
|
|
$canDeletePermission = is_array($capabilities) ? (bool) ($capabilities['can_delete_permission'] ?? false) : false;
|
|
|
|
$errorBag = formErrors();
|
|
$errors = [];
|
|
$form = $permission;
|
|
$roles = app(RoleRepository::class)->listActive();
|
|
$rolePermissionRepository = app(\MintyPHP\Repository\Access\RolePermissionRepository::class);
|
|
$userRoleRepository = app(\MintyPHP\Repository\Access\UserRoleRepository::class);
|
|
$userWriteRepository = app(\MintyPHP\Repository\User\UserWriteRepository::class);
|
|
$selectedRoleIds = $rolePermissionRepository->listRoleIdsByPermissionId($id);
|
|
$previousRoleIds = $selectedRoleIds;
|
|
|
|
if ($request->isMethod('POST') && !Session::checkCsrfToken()) {
|
|
Flash::error(t('Form expired, please try again'), $editTarget, 'csrf_expired');
|
|
Router::redirect($editTarget);
|
|
return;
|
|
}
|
|
|
|
if ($request->isMethod('POST')) {
|
|
$submitDecision = $authorizationService->authorize(PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_EDIT_SUBMIT, [
|
|
'actor_user_id' => $currentUserId,
|
|
'target_permission_id' => $id,
|
|
'target_is_system' => $isSystemPermission,
|
|
]);
|
|
if (!$submitDecision->isAllowed()) {
|
|
Guard::deny();
|
|
}
|
|
$result = app(\MintyPHP\Service\Access\PermissionService::class)->updateFromAdmin($id, $request->bodyAll());
|
|
$form = $result['form'] ?? $form;
|
|
$errorBag->merge($result['errors'] ?? []);
|
|
$selectedRoleIds = $request->body('role_ids', $selectedRoleIds);
|
|
if (!is_array($selectedRoleIds)) {
|
|
$selectedRoleIds = [$selectedRoleIds];
|
|
}
|
|
$selectedRoleIds = array_values(array_unique(array_map('intval', $selectedRoleIds)));
|
|
|
|
if (($result['ok'] ?? false) && !$errorBag->hasAny()) {
|
|
$roleIds = $request->body('role_ids', []);
|
|
if (!is_array($roleIds)) {
|
|
$roleIds = [$roleIds];
|
|
}
|
|
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
|
|
$syncSucceeded = false;
|
|
$dbSession = app(\MintyPHP\Repository\Support\DatabaseSessionRepository::class);
|
|
$dbSession->beginTransaction();
|
|
try {
|
|
$rolePermissionRepository->replaceForPermission($id, $roleIds);
|
|
$affectedRoleIds = array_values(array_unique(array_merge($previousRoleIds, $roleIds)));
|
|
$affectedUserIds = $userRoleRepository->listUserIdsByRoleIds($affectedRoleIds);
|
|
if ($affectedUserIds) {
|
|
$userWriteRepository->bumpAuthzVersionByUserIds($affectedUserIds);
|
|
}
|
|
$dbSession->commitTransaction();
|
|
$syncSucceeded = true;
|
|
} catch (\Throwable) {
|
|
try {
|
|
$dbSession->rollbackTransaction();
|
|
} catch (\Throwable) {
|
|
}
|
|
$errorBag->merge([t('Failed to update permission roles')]);
|
|
}
|
|
if ($syncSucceeded) {
|
|
$action = (string) $request->body('action', 'save');
|
|
if ($action === 'save_close') {
|
|
Flash::success('Permission updated', $closeTarget, 'permission_updated');
|
|
Router::redirect($closeTarget);
|
|
} else {
|
|
Flash::success('Permission updated', $editTarget, 'permission_updated');
|
|
Router::redirect($editTarget);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$validationSummaryErrors = $errorBag->toArray();
|
|
$errors = $errorBag->toFlatList();
|
|
$titleText = $canUpdatePermission ? t('Edit permission') : t('View permission');
|
|
Buffer::set('title', $titleText);
|
|
$breadcrumbs = [
|
|
['label' => t('Home'), 'path' => 'admin'],
|
|
['label' => t('Permissions'), 'path' => 'admin/permissions'],
|
|
['label' => $titleText],
|
|
];
|