1
0
Files
breadcrumb-the-shire/core/Service/Access/DepartmentAuthorizationPolicy.php
fs d6be536fbf refactor(departments-create): migrate to actionCreateContext (step 9)
First production use of actionCreateContext — the second aggregator
introduced in step 1 and unit-tested at the building-block level, but
not yet exercised against a real caller. Helper file stays 0-diff for
the sixth consecutive migration.

The migration uncovers one real API gap and resolves it at the policy
layer rather than at the helper:

* actionCreateContext always calls actionEnforceCanViewPage. The
  Departments create-decision was the only Departments authorize
  branch that did not emit can_view_page (View and EditContext both
  did). Adding 'can_view_page' => true to the create-capabilities
  map is tautological — every actor that survives the deny() guards
  at lines 69-70 and 75-76 can by definition see the page. No new
  forbidden path is created. View, Create, and EditContext now share
  the same capability shape.

Three drift decisions reproduced where applicable:
* notFoundFlashScopeKey is N/A (no model lookup in create flow).
* t() consistency: all three Flash::success('Department created', …)
  calls now flow through t().
* Defensive scope consumption: $canManageAllTenants reads
  $tenantScope['scope'] === 'all', mirroring the edit-action pattern.
  The GET tenant filter rewrites from is_array($allowedTenantIds) to
  the three-way scope-tuple form.

AuthzAdminMasterDataContractTest gets a single-line assertion update
(AuthorizationService::class → actionCreateContext() pattern). The
aggregator wraps the same authorize call internally, so this is a
pattern-rename, not a semantic shift.

ActionContextCsrfPairingContractTest now covers six callers (five
edits + departments-create) and stays green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 14:44:09 +02:00

149 lines
6.2 KiB
PHP

<?php
namespace MintyPHP\Service\Access;
use MintyPHP\Service\Tenant\TenantScopeService;
class DepartmentAuthorizationPolicy implements AuthorizationPolicyInterface
{
use AuthorizationPolicyContextTrait;
public const ABILITY_ADMIN_DEPARTMENTS_VIEW = 'admin.departments.view';
public const ABILITY_ADMIN_DEPARTMENTS_CREATE = 'admin.departments.create';
public const ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT = 'admin.departments.edit.context';
public const ABILITY_ADMIN_DEPARTMENTS_EDIT_SUBMIT = 'admin.departments.edit.submit';
public const ABILITY_ADMIN_DEPARTMENTS_DELETE = 'admin.departments.delete';
public function __construct(
private readonly PermissionService $permissionService,
private readonly TenantScopeService $scopeGateway
) {
}
public function supports(string $ability): bool
{
return in_array($ability, [
self::ABILITY_ADMIN_DEPARTMENTS_VIEW,
self::ABILITY_ADMIN_DEPARTMENTS_CREATE,
self::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT,
self::ABILITY_ADMIN_DEPARTMENTS_EDIT_SUBMIT,
self::ABILITY_ADMIN_DEPARTMENTS_DELETE,
], true);
}
public function authorize(string $ability, array $context = []): AuthorizationDecision
{
return match ($ability) {
self::ABILITY_ADMIN_DEPARTMENTS_VIEW => $this->authorizeAdminDepartmentsView($context),
self::ABILITY_ADMIN_DEPARTMENTS_CREATE => $this->authorizeAdminDepartmentsCreate($context),
self::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT => $this->authorizeAdminDepartmentsEditContext($context),
self::ABILITY_ADMIN_DEPARTMENTS_EDIT_SUBMIT => $this->authorizeAdminDepartmentsEditSubmit($context),
self::ABILITY_ADMIN_DEPARTMENTS_DELETE => $this->authorizeAdminDepartmentsDelete($context),
default => AuthorizationDecision::deny(500, 'authorization_ability_not_supported'),
};
}
private function authorizeAdminDepartmentsView(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
if (!$this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_VIEW)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
$allowedTenantIds = $this->scopeGateway->getUserTenantIds($actorUserId);
$isStrictScope = $this->scopeGateway->isStrict();
return AuthorizationDecision::allow([
'capabilities' => [
'can_view_page' => true,
'can_create_department' => $this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_CREATE),
'allowed_tenant_ids' => $allowedTenantIds,
'is_strict_scope' => $isStrictScope,
],
]);
}
private function authorizeAdminDepartmentsCreate(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
if (!$this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_CREATE)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
$allowedTenantIds = $this->scopeGateway->getUserTenantIds($actorUserId);
$isStrictScope = $this->scopeGateway->isStrict();
if ($isStrictScope && !$allowedTenantIds) {
return AuthorizationDecision::deny(403, 'permission_denied');
}
return AuthorizationDecision::allow([
'capabilities' => [
'can_view_page' => true,
'allowed_tenant_ids' => $allowedTenantIds,
'is_strict_scope' => $isStrictScope,
],
]);
}
private function authorizeAdminDepartmentsEditContext(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
$targetDepartmentId = $this->targetDepartmentId($context);
if ($actorUserId <= 0 || $targetDepartmentId <= 0) {
return AuthorizationDecision::deny(403, 'forbidden');
}
if (!$this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_VIEW)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
if (!$this->scopeGateway->canAccess('departments', $targetDepartmentId, $actorUserId)) {
return AuthorizationDecision::deny(403, 'permission_denied');
}
return AuthorizationDecision::allow([
'capabilities' => [
'can_view_page' => true,
'can_update_department' => $this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_UPDATE),
'can_delete_department' => $this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_DELETE),
'allowed_tenant_ids' => $this->scopeGateway->getUserTenantIds($actorUserId),
],
]);
}
private function authorizeAdminDepartmentsEditSubmit(array $context): AuthorizationDecision
{
$contextDecision = $this->authorizeAdminDepartmentsEditContext($context);
if (!$contextDecision->isAllowed()) {
return $contextDecision;
}
$capabilities = $this->capabilitiesFromDecision($contextDecision);
if (!($capabilities['can_update_department'] ?? false)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
return AuthorizationDecision::allow(['capabilities' => $capabilities]);
}
private function authorizeAdminDepartmentsDelete(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
$targetDepartmentId = $this->targetDepartmentId($context);
if ($actorUserId <= 0 || $targetDepartmentId <= 0) {
return AuthorizationDecision::deny(403, 'forbidden');
}
if (!$this->hasPermission($actorUserId, PermissionService::DEPARTMENTS_DELETE)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
if (!$this->scopeGateway->canAccess('departments', $targetDepartmentId, $actorUserId)) {
return AuthorizationDecision::deny(403, 'permission_denied');
}
return AuthorizationDecision::allow();
}
private function targetDepartmentId(array $context): int
{
return (int) ($context['target_department_id'] ?? 0);
}
}