forked from fa/breadcrumb-the-shire
refactor(departments-edit): migrate to actionEditContext (step 2)
Pilot migration of pages/admin/departments/edit($id).php onto the actionEditContext aggregator introduced in step 1. The CONTEXT-stage vorspiel (lookup → authorize → tenant-scope → can_view_page → viewAuth) collapses into a single declarative call; the POST branch (CSRF → SUBMIT-authorize → can_update gate → service call → PRG) stays callsite-specific as planned. Three deliberate touches beyond a 1:1 lift: * Additive aggregator extension: actionEditContext gains an optional notFoundFlashScopeKey arg so the dedup scope-key 'department_not_found' is preserved without widening the frozen actionResolveModelOrFail building-block signature. Pattern is documented as the forward-compatibility mechanism for future cluster migrations. * t() consistency: the not-found message now flows through t() via the aggregator. To avoid a partial-translation mix, Flash::success calls for 'Department updated' (×2) are also wrapped — German users now see fully translated messages instead of a German/English mix. * Defensive scope consumption: the action now consults $tenantScope['scope'] before falling through to the strict-mode fallback. The Departments policy never emits can_manage_all_tenants today (so behavior is identical), but the action is now resilient to future policies that might. New ActionContextCsrfPairingContractTest enforces actionRequireCsrf() before any POST body access for actions that use the aggregators — preventing CSRF-pairing regressions during the cluster-wide rollout (step 3). The step-1 testNoProductionCallSitesYet guard is removed, since departments-edit is now the first legitimate caller; the new pairing test takes over its protective role with a more substantive guarantee. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -24,41 +24,37 @@ $directoryScopeGateway = app(\MintyPHP\Service\Tenant\TenantScopeService::class)
|
||||
|
||||
$uuid = trim((string) ($id ?? ''));
|
||||
$editTarget = requestPathWithReturnTarget("admin/departments/edit/{$uuid}", $returnTarget);
|
||||
|
||||
// Resolve the department BEFORE the aggregator so the authorize-context can
|
||||
// carry the real target_department_id (matches today's order: lookup → CONTEXT
|
||||
// authorize → can_view_page). The aggregator receives a trivial finder that
|
||||
// returns the already-loaded model so its not-found branch fires only when
|
||||
// findByUuid returned null.
|
||||
$department = $uuid !== '' ? $departmentService->findByUuid($uuid) : null;
|
||||
if (!$department) {
|
||||
Flash::error('Department not found', $closeTarget, 'department_not_found');
|
||||
Router::redirect($closeTarget);
|
||||
}
|
||||
|
||||
$departmentId = (int) ($department['id'] ?? 0);
|
||||
$contextDecision = $authorizationService->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT, [
|
||||
'actor_user_id' => $currentUserId,
|
||||
'target_department_id' => $departmentId,
|
||||
]);
|
||||
if (!$contextDecision->isAllowed()) {
|
||||
Router::redirect('error/forbidden');
|
||||
return;
|
||||
}
|
||||
|
||||
$capabilities = $contextDecision->attribute('capabilities', []);
|
||||
if (!is_array($capabilities)) {
|
||||
$capabilities = [];
|
||||
}
|
||||
$canViewPage = (bool) ($capabilities['can_view_page'] ?? false);
|
||||
$context = actionEditContext([
|
||||
'finder' => static fn (string $_id): mixed => $department,
|
||||
'rawId' => $uuid,
|
||||
'notFoundFlashKey' => 'Department not found',
|
||||
'notFoundRedirectPath' => $closeTarget,
|
||||
'notFoundFlashScopeKey' => 'department_not_found',
|
||||
'abilityKey' => DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT,
|
||||
'context' => [
|
||||
'actor_user_id' => $currentUserId,
|
||||
'target_department_id' => $departmentId,
|
||||
],
|
||||
'viewAuthFlags' => ['can_update_department', 'can_delete_department'],
|
||||
'forbiddenStrategy' => 'redirect',
|
||||
]);
|
||||
$department = is_array($context['model']) ? $context['model'] : $department;
|
||||
$capabilities = $context['capabilities'];
|
||||
$tenantScope = $context['tenantScope'];
|
||||
$viewAuth = $context['viewAuth'];
|
||||
$canUpdateDepartment = (bool) ($capabilities['can_update_department'] ?? false);
|
||||
$canDeleteDepartment = (bool) ($capabilities['can_delete_department'] ?? false);
|
||||
$viewAuth['page'] = [
|
||||
'can_update_department' => $canUpdateDepartment,
|
||||
'can_delete_department' => $canDeleteDepartment,
|
||||
];
|
||||
$allowedTenantIdsRaw = $capabilities['allowed_tenant_ids'] ?? [];
|
||||
$allowedTenantIds = is_array($allowedTenantIdsRaw)
|
||||
? array_values(array_unique(array_filter(array_map('intval', $allowedTenantIdsRaw), static fn (int $tenantId): bool => $tenantId > 0)))
|
||||
: [];
|
||||
if (!$canViewPage) {
|
||||
Router::redirect('error/forbidden');
|
||||
return;
|
||||
}
|
||||
$canManageAllTenants = $tenantScope['scope'] === 'all';
|
||||
$allowedTenantIds = $tenantScope['ids'];
|
||||
|
||||
app(\MintyPHP\Service\Audit\AuditMetadataEnricherInterface::class)->enrich($department);
|
||||
|
||||
@@ -67,7 +63,9 @@ $errors = [];
|
||||
$warnings = [];
|
||||
$form = $department;
|
||||
$tenants = $tenantService->list();
|
||||
if ($allowedTenantIds) {
|
||||
if ($canManageAllTenants) {
|
||||
// No filtering — actor may pick any tenant.
|
||||
} elseif ($allowedTenantIds) {
|
||||
$tenants = array_values(array_filter($tenants, static function (array $tenant) use ($allowedTenantIds): bool {
|
||||
$tenantId = (int) ($tenant['id'] ?? 0);
|
||||
return $tenantId > 0 && in_array($tenantId, $allowedTenantIds, true);
|
||||
@@ -110,7 +108,9 @@ if ($request->isMethod('POST')) {
|
||||
}
|
||||
|
||||
$selectedTenantId = $request->bodyInt('tenant_id');
|
||||
if ($allowedTenantIds) {
|
||||
if ($canManageAllTenants) {
|
||||
// No restriction — submitted tenant_id passes through.
|
||||
} elseif ($allowedTenantIds) {
|
||||
$selectedTenantId = in_array($selectedTenantId, $allowedTenantIds, true) ? $selectedTenantId : 0;
|
||||
} elseif ($directoryScopeGateway->isStrict()) {
|
||||
$selectedTenantId = 0;
|
||||
@@ -134,13 +134,13 @@ if ($request->isMethod('POST')) {
|
||||
if ($warnings) {
|
||||
Flash::info(implode(' ', $warnings), $closeTarget, 'department_warning');
|
||||
}
|
||||
Flash::success('Department updated', $closeTarget, 'department_updated');
|
||||
Flash::success(t('Department updated'), $closeTarget, 'department_updated');
|
||||
Router::redirect($closeTarget);
|
||||
} else {
|
||||
if ($warnings) {
|
||||
Flash::info(implode(' ', $warnings), $editTarget, 'department_warning');
|
||||
}
|
||||
Flash::success('Department updated', $editTarget, 'department_updated');
|
||||
Flash::success(t('Department updated'), $editTarget, 'department_updated');
|
||||
Router::redirect($editTarget);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user