Files
breadcrumb-the-shire/core/Service/Access/PermissionAuthorizationPolicy.php
fs 1bd4607b66 refactor(creates-batch): migrate roles/permissions/tenants-create (step 10)
Cluster-7 batch-replay of the departments-create pilot (step 9). All
three remaining create actions follow the same shape with minor
domain-specific variations.

Each migration touches one action and one policy:

* roles-create + RoleAuthorizationPolicy::authorizeAdminRolesCreate —
  policy previously returned bare allow() with no capabilities; now
  emits ['can_view_page' => true]. Action passes viewAuthFlags: [].
* permissions-create + PermissionAuthorizationPolicy::authorizeAdminPermissionsCreate —
  same pattern as roles-create.
* tenants-create + TenantAuthorizationPolicy::authorizeAdminTenantsCreate —
  policy already emitted can_manage_sso + can_manage_custom_fields;
  can_view_page is added as the first capability. Action passes
  viewAuthFlags: ['can_manage_sso', 'can_manage_custom_fields'] and
  materializes both booleans from the aggregator capabilities.

All three policy updates are tautological — every actor that survives
the deny() branches in each policy can by definition see the page.
View, Create, and EditContext now share a consistent capability shape
across all four core master-data domains (departments, roles,
permissions, tenants).

Three drift decisions reproduced:
* notFoundFlashScopeKey is N/A (no model lookup in create flows).
* t() consistency: Flash::success('Role created' / 'Permission created'
  / 'Tenant created') now flow through t().
* Defensive scope consumption: $canManageAllTenants reads
  $tenantScope['scope'] === 'all' as a resilient hook even where the
  policy emits no manage-all flag (roles/permissions are global,
  tenants-create has no filter logic). Inline comments document the
  intentional non-consumption of $tenantScope['ids'].

Two contract-test pattern updates (AuthzAdminMasterDataContractTest +
AuthzAdminTenantsContractTest) shift the assertion targets from
AuthorizationService::class to actionCreateContext( — semantically
equivalent because the aggregator wraps the same authorize call
internally.

ActionContextCsrfPairingContractTest now covers nine callers
(five edits + four creates) and stays green. Helper file
core/Support/helpers/action_context.php is 0-diff for the seventh
consecutive migration.

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

121 lines
4.8 KiB
PHP

<?php
namespace MintyPHP\Service\Access;
class PermissionAuthorizationPolicy implements AuthorizationPolicyInterface
{
use AuthorizationPolicyContextTrait;
public const ABILITY_ADMIN_PERMISSIONS_VIEW = 'admin.permissions.view';
public const ABILITY_ADMIN_PERMISSIONS_CREATE = 'admin.permissions.create';
public const ABILITY_ADMIN_PERMISSIONS_EDIT_CONTEXT = 'admin.permissions.edit.context';
public const ABILITY_ADMIN_PERMISSIONS_EDIT_SUBMIT = 'admin.permissions.edit.submit';
public const ABILITY_ADMIN_PERMISSIONS_DELETE = 'admin.permissions.delete';
public function __construct(
private readonly PermissionService $permissionService
) {
}
public function supports(string $ability): bool
{
return in_array($ability, [
self::ABILITY_ADMIN_PERMISSIONS_VIEW,
self::ABILITY_ADMIN_PERMISSIONS_CREATE,
self::ABILITY_ADMIN_PERMISSIONS_EDIT_CONTEXT,
self::ABILITY_ADMIN_PERMISSIONS_EDIT_SUBMIT,
self::ABILITY_ADMIN_PERMISSIONS_DELETE,
], true);
}
public function authorize(string $ability, array $context = []): AuthorizationDecision
{
return match ($ability) {
self::ABILITY_ADMIN_PERMISSIONS_VIEW => $this->authorizeAdminPermissionsView($context),
self::ABILITY_ADMIN_PERMISSIONS_CREATE => $this->authorizeAdminPermissionsCreate($context),
self::ABILITY_ADMIN_PERMISSIONS_EDIT_CONTEXT => $this->authorizeAdminPermissionsEditContext($context),
self::ABILITY_ADMIN_PERMISSIONS_EDIT_SUBMIT => $this->authorizeAdminPermissionsEditSubmit($context),
self::ABILITY_ADMIN_PERMISSIONS_DELETE => $this->authorizeAdminPermissionsDelete($context),
default => AuthorizationDecision::deny(500, 'authorization_ability_not_supported'),
};
}
private function authorizeAdminPermissionsView(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
if (!$this->hasPermission($actorUserId, PermissionService::PERMISSIONS_VIEW)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
return AuthorizationDecision::allow([
'capabilities' => [
'can_view_page' => true,
'can_create_permission' => $this->hasPermission($actorUserId, PermissionService::PERMISSIONS_CREATE),
],
]);
}
private function authorizeAdminPermissionsCreate(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
if (!$this->hasPermission($actorUserId, PermissionService::PERMISSIONS_CREATE)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
return AuthorizationDecision::allow([
'capabilities' => [
'can_view_page' => true,
],
]);
}
private function authorizeAdminPermissionsEditContext(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
$targetPermissionId = (int) ($context['target_permission_id'] ?? 0);
if ($actorUserId <= 0 || $targetPermissionId <= 0) {
return AuthorizationDecision::deny(403, 'forbidden');
}
if (!$this->hasPermission($actorUserId, PermissionService::PERMISSIONS_VIEW)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
$targetIsSystem = (bool) ($context['target_is_system'] ?? false);
return AuthorizationDecision::allow([
'capabilities' => [
'can_view_page' => true,
'can_update_permission' => $this->hasPermission($actorUserId, PermissionService::PERMISSIONS_UPDATE),
'can_delete_permission' => $this->hasPermission($actorUserId, PermissionService::PERMISSIONS_DELETE) && !$targetIsSystem,
],
]);
}
private function authorizeAdminPermissionsEditSubmit(array $context): AuthorizationDecision
{
$contextDecision = $this->authorizeAdminPermissionsEditContext($context);
if (!$contextDecision->isAllowed()) {
return $contextDecision;
}
$capabilities = $this->capabilitiesFromDecision($contextDecision);
if (!($capabilities['can_update_permission'] ?? false)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
return AuthorizationDecision::allow(['capabilities' => $capabilities]);
}
private function authorizeAdminPermissionsDelete(array $context): AuthorizationDecision
{
$actorUserId = $this->actorUserId($context);
if (!$this->hasPermission($actorUserId, PermissionService::PERMISSIONS_DELETE)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
return AuthorizationDecision::allow();
}
}