forked from fa/breadcrumb-the-shire
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>
222 lines
9.2 KiB
PHP
222 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Service\Access;
|
|
|
|
use MintyPHP\Service\Tenant\TenantScopeService;
|
|
|
|
class TenantAuthorizationPolicy implements AuthorizationPolicyInterface
|
|
{
|
|
use AuthorizationPolicyContextTrait;
|
|
|
|
public const ABILITY_ADMIN_TENANTS_VIEW = 'admin.tenants.view';
|
|
public const ABILITY_ADMIN_TENANTS_CREATE = 'admin.tenants.create';
|
|
public const ABILITY_ADMIN_TENANTS_EDIT_CONTEXT = 'admin.tenants.edit.context';
|
|
public const ABILITY_ADMIN_TENANTS_EDIT_SUBMIT = 'admin.tenants.edit.submit';
|
|
public const ABILITY_ADMIN_TENANTS_DELETE = 'admin.tenants.delete';
|
|
public const ABILITY_ADMIN_TENANTS_CUSTOM_FIELDS_MANAGE = 'admin.tenants.custom_fields.manage';
|
|
public const ABILITY_ADMIN_TENANTS_LOGO_VIEW = 'admin.tenants.logo.view';
|
|
public const ABILITY_ADMIN_TENANTS_MEDIA_UPDATE = 'admin.tenants.media.update';
|
|
|
|
public function __construct(
|
|
private readonly PermissionService $permissionService,
|
|
private readonly TenantScopeService $scopeGateway
|
|
) {
|
|
}
|
|
|
|
public function supports(string $ability): bool
|
|
{
|
|
return in_array($ability, [
|
|
self::ABILITY_ADMIN_TENANTS_VIEW,
|
|
self::ABILITY_ADMIN_TENANTS_CREATE,
|
|
self::ABILITY_ADMIN_TENANTS_EDIT_CONTEXT,
|
|
self::ABILITY_ADMIN_TENANTS_EDIT_SUBMIT,
|
|
self::ABILITY_ADMIN_TENANTS_DELETE,
|
|
self::ABILITY_ADMIN_TENANTS_CUSTOM_FIELDS_MANAGE,
|
|
self::ABILITY_ADMIN_TENANTS_LOGO_VIEW,
|
|
self::ABILITY_ADMIN_TENANTS_MEDIA_UPDATE,
|
|
], true);
|
|
}
|
|
|
|
public function authorize(string $ability, array $context = []): AuthorizationDecision
|
|
{
|
|
return match ($ability) {
|
|
self::ABILITY_ADMIN_TENANTS_VIEW => $this->authorizeAdminTenantsView($context),
|
|
self::ABILITY_ADMIN_TENANTS_CREATE => $this->authorizeAdminTenantsCreate($context),
|
|
self::ABILITY_ADMIN_TENANTS_EDIT_CONTEXT => $this->authorizeAdminTenantsEditContext($context),
|
|
self::ABILITY_ADMIN_TENANTS_EDIT_SUBMIT => $this->authorizeAdminTenantsEditSubmit($context),
|
|
self::ABILITY_ADMIN_TENANTS_DELETE => $this->authorizeAdminTenantsDelete($context),
|
|
self::ABILITY_ADMIN_TENANTS_CUSTOM_FIELDS_MANAGE => $this->authorizeAdminTenantCustomFieldsManage($context),
|
|
self::ABILITY_ADMIN_TENANTS_LOGO_VIEW => $this->authorizeAdminTenantLogoView($context),
|
|
self::ABILITY_ADMIN_TENANTS_MEDIA_UPDATE => $this->authorizeAdminTenantMediaUpdate($context),
|
|
default => AuthorizationDecision::deny(500, 'authorization_ability_not_supported'),
|
|
};
|
|
}
|
|
|
|
private function authorizeAdminTenantsEditContext(array $context): AuthorizationDecision
|
|
{
|
|
$actorUserId = $this->actorUserId($context);
|
|
$targetTenantId = $this->targetTenantId($context);
|
|
if ($actorUserId <= 0 || $targetTenantId <= 0) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
if (!$this->scopeGateway->canAccess('tenants', $targetTenantId, $actorUserId)) {
|
|
return AuthorizationDecision::deny(403, 'permission_denied');
|
|
}
|
|
|
|
$canViewTenant = $this->hasPermission($actorUserId, PermissionService::TENANTS_VIEW);
|
|
$canUpdateTenant = $this->hasPermission($actorUserId, PermissionService::TENANTS_UPDATE);
|
|
$canViewPage = $canViewTenant || $canUpdateTenant;
|
|
if (!$canViewPage) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
$capabilities = [
|
|
'can_view_page' => $canViewPage,
|
|
'can_update_tenant' => $canUpdateTenant,
|
|
'can_manage_custom_fields' => $this->hasPermission($actorUserId, PermissionService::CUSTOM_FIELDS_MANAGE),
|
|
'can_manage_sso' => $this->hasPermission($actorUserId, PermissionService::TENANTS_SSO_MANAGE),
|
|
'can_delete_tenant' => $this->hasPermission($actorUserId, PermissionService::TENANTS_DELETE),
|
|
];
|
|
|
|
return AuthorizationDecision::allow(['capabilities' => $capabilities]);
|
|
}
|
|
|
|
private function authorizeAdminTenantsView(array $context): AuthorizationDecision
|
|
{
|
|
$actorUserId = $this->actorUserId($context);
|
|
if (!$this->hasPermission($actorUserId, PermissionService::TENANTS_VIEW)) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
return AuthorizationDecision::allow([
|
|
'capabilities' => [
|
|
'can_view_page' => true,
|
|
'can_create_tenant' => $this->hasPermission($actorUserId, PermissionService::TENANTS_CREATE),
|
|
'can_manage_custom_fields' => $this->hasPermission($actorUserId, PermissionService::CUSTOM_FIELDS_MANAGE),
|
|
'can_manage_sso' => $this->hasPermission($actorUserId, PermissionService::TENANTS_SSO_MANAGE),
|
|
'allowed_tenant_ids' => $this->scopeGateway->getUserTenantIds($actorUserId),
|
|
'is_strict_scope' => $this->scopeGateway->isStrict(),
|
|
'has_global_access' => $this->scopeGateway->hasGlobalAccess($actorUserId),
|
|
],
|
|
]);
|
|
}
|
|
|
|
private function authorizeAdminTenantsCreate(array $context): AuthorizationDecision
|
|
{
|
|
$actorUserId = $this->actorUserId($context);
|
|
if (!$this->hasPermission($actorUserId, PermissionService::TENANTS_CREATE)) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
return AuthorizationDecision::allow([
|
|
'capabilities' => [
|
|
'can_view_page' => true,
|
|
'can_manage_custom_fields' => $this->hasPermission($actorUserId, PermissionService::CUSTOM_FIELDS_MANAGE),
|
|
'can_manage_sso' => $this->hasPermission($actorUserId, PermissionService::TENANTS_SSO_MANAGE),
|
|
],
|
|
]);
|
|
}
|
|
|
|
private function authorizeAdminTenantsEditSubmit(array $context): AuthorizationDecision
|
|
{
|
|
$contextDecision = $this->authorizeAdminTenantsEditContext($context);
|
|
if (!$contextDecision->isAllowed()) {
|
|
return $contextDecision;
|
|
}
|
|
|
|
$capabilities = $this->capabilitiesFromDecision($contextDecision);
|
|
if (!($capabilities['can_update_tenant'] ?? false)) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
$input = is_array($context['input'] ?? null) ? $context['input'] : [];
|
|
if ($this->containsSsoFields($input) && !($capabilities['can_manage_sso'] ?? false)) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
return AuthorizationDecision::allow(['capabilities' => $capabilities]);
|
|
}
|
|
|
|
private function authorizeAdminTenantsDelete(array $context): AuthorizationDecision
|
|
{
|
|
return $this->authorizeTenantWithPermission($context, PermissionService::TENANTS_DELETE);
|
|
}
|
|
|
|
private function authorizeAdminTenantCustomFieldsManage(array $context): AuthorizationDecision
|
|
{
|
|
return $this->authorizeTenantWithPermission($context, PermissionService::CUSTOM_FIELDS_MANAGE);
|
|
}
|
|
|
|
private function authorizeAdminTenantLogoView(array $context): AuthorizationDecision
|
|
{
|
|
$actorUserId = $this->actorUserId($context);
|
|
if (!$this->hasPermission($actorUserId, PermissionService::TENANTS_VIEW)
|
|
&& !$this->hasPermission($actorUserId, PermissionService::TENANTS_UPDATE)) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
return $this->authorizeTenantScopeOnly($context);
|
|
}
|
|
|
|
private function authorizeAdminTenantMediaUpdate(array $context): AuthorizationDecision
|
|
{
|
|
return $this->authorizeTenantWithPermission($context, PermissionService::TENANTS_UPDATE);
|
|
}
|
|
|
|
private function authorizeTenantWithPermission(array $context, string $permissionKey): AuthorizationDecision
|
|
{
|
|
$actorUserId = $this->actorUserId($context);
|
|
if (!$this->hasPermission($actorUserId, $permissionKey)) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
return $this->authorizeTenantScopeOnly($context);
|
|
}
|
|
|
|
private function authorizeTenantScopeOnly(array $context): AuthorizationDecision
|
|
{
|
|
$actorUserId = $this->actorUserId($context);
|
|
$targetTenantId = $this->targetTenantId($context);
|
|
if ($actorUserId <= 0 || $targetTenantId <= 0) {
|
|
return AuthorizationDecision::deny(403, 'forbidden');
|
|
}
|
|
|
|
if (!$this->scopeGateway->canAccess('tenants', $targetTenantId, $actorUserId)) {
|
|
return AuthorizationDecision::deny(403, 'permission_denied');
|
|
}
|
|
|
|
return AuthorizationDecision::allow();
|
|
}
|
|
|
|
private function containsSsoFields(array $input): bool
|
|
{
|
|
$ssoFields = [
|
|
'microsoft_enabled',
|
|
'enforce_microsoft_login',
|
|
'sync_profile_on_login',
|
|
'sync_profile_fields',
|
|
'entra_tenant_id',
|
|
'allowed_domains',
|
|
'use_shared_app',
|
|
'client_id_override',
|
|
'client_secret_override',
|
|
'clear_client_secret_override',
|
|
'microsoft_auto_remember_mode',
|
|
];
|
|
|
|
foreach ($ssoFields as $field) {
|
|
if (array_key_exists($field, $input)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function targetTenantId(array $context): int
|
|
{
|
|
return (int) ($context['target_tenant_id'] ?? 0);
|
|
}
|
|
}
|