Files
breadcrumb-the-shire/core/Service/Access/TenantAuthorizationPolicy.php

221 lines
9.2 KiB
PHP
Raw Normal View History

<?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';
feat(tenant): per-theme logos + file-upload + button UI polish Replace the single tenant avatar with a pair of theme-scoped brand logos. Render only the theme-matching <img> server-side and swap src on theme toggle via a JS hook — no reload, no double request, no CSS tricks. Tenant logos - TenantLogoService (ImageUploadTrait) with theme whitelist and per-theme storage storage/tenants/{uuid}/logo/{light|dark}/, SIZES 128/256/512 - Public serving endpoint auth/tenant-logo-file so login can show the logo pre-auth; matching authenticated admin preview endpoint - appTenantLogoUrl(?size, ?theme) with 4-step fallback cascade; PDF + mail always request 'light' - Admin tenant edit: avatar block replaced by "Tenant logos" details block inside the Master-data tab, two side-by-side slots via Pico .grid with the core app-file-upload partial - Policy rename ABILITY_ADMIN_TENANTS_AVATAR_VIEW -> LOGO_VIEW, action routes logo / logo-delete / logo-file with theme body/query param - API endpoint path kept (backward compat), internals on new service - CLI tenant:logo-migrate-avatars moves legacy avatar/ -> logo/light/ idempotently (--dry-run, --yes, --cleanup) - i18n "Tenant image" removed, 12 new keys synced across de/en File upload component - Full-width preview + filename/actions below (3D stack layout) - Fixed 16:9 aspect ratio with 1rem inner padding for consistent preview size across any logo aspect - Transparency checker pattern as background so black logos stay visible on dark mode and white logos on light mode - form="" + deleteFormId support so the partial works with barrier forms inside another form Buttons - width:100% dropped from button[type="submit"]; scoped back via .login-main for the auth-flow primary CTA - .outline base rule now tints background via color-mix of --app-color so secondary/primary/danger outlines all gain a subtle surface - .outline.secondary restyled Stripe-style in both themes: solid white chip with soft shadow in light, solid elevated dark chip with white text in dark; neutral border replaces role-colored border - .app-action-success/.app-action-danger outlines get color-mix bg + theme-aware outline-text tokens for stronger contrast - Filled .primary/.app-action-success/.app-action-danger get raised box-shadow (inset highlight + drop) — opt-in via class so chrome buttons stay flat - Dropped the legacy .secondary utility that was clobbering the custom-property cascade with a hardcoded muted color Theme swap - Logo img carries data-src-light + data-src-dark; theme-toggle JS swaps src when data-theme changes, keeping the topbar/login logo in sync without a page reload Quality gates: PHPUnit (2045), PHPStan L5, CS-Fixer, docs link/drift, codex skills sync — all green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 20:25:53 +02:00
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,
feat(tenant): per-theme logos + file-upload + button UI polish Replace the single tenant avatar with a pair of theme-scoped brand logos. Render only the theme-matching <img> server-side and swap src on theme toggle via a JS hook — no reload, no double request, no CSS tricks. Tenant logos - TenantLogoService (ImageUploadTrait) with theme whitelist and per-theme storage storage/tenants/{uuid}/logo/{light|dark}/, SIZES 128/256/512 - Public serving endpoint auth/tenant-logo-file so login can show the logo pre-auth; matching authenticated admin preview endpoint - appTenantLogoUrl(?size, ?theme) with 4-step fallback cascade; PDF + mail always request 'light' - Admin tenant edit: avatar block replaced by "Tenant logos" details block inside the Master-data tab, two side-by-side slots via Pico .grid with the core app-file-upload partial - Policy rename ABILITY_ADMIN_TENANTS_AVATAR_VIEW -> LOGO_VIEW, action routes logo / logo-delete / logo-file with theme body/query param - API endpoint path kept (backward compat), internals on new service - CLI tenant:logo-migrate-avatars moves legacy avatar/ -> logo/light/ idempotently (--dry-run, --yes, --cleanup) - i18n "Tenant image" removed, 12 new keys synced across de/en File upload component - Full-width preview + filename/actions below (3D stack layout) - Fixed 16:9 aspect ratio with 1rem inner padding for consistent preview size across any logo aspect - Transparency checker pattern as background so black logos stay visible on dark mode and white logos on light mode - form="" + deleteFormId support so the partial works with barrier forms inside another form Buttons - width:100% dropped from button[type="submit"]; scoped back via .login-main for the auth-flow primary CTA - .outline base rule now tints background via color-mix of --app-color so secondary/primary/danger outlines all gain a subtle surface - .outline.secondary restyled Stripe-style in both themes: solid white chip with soft shadow in light, solid elevated dark chip with white text in dark; neutral border replaces role-colored border - .app-action-success/.app-action-danger outlines get color-mix bg + theme-aware outline-text tokens for stronger contrast - Filled .primary/.app-action-success/.app-action-danger get raised box-shadow (inset highlight + drop) — opt-in via class so chrome buttons stay flat - Dropped the legacy .secondary utility that was clobbering the custom-property cascade with a hardcoded muted color Theme swap - Logo img carries data-src-light + data-src-dark; theme-toggle JS swaps src when data-theme changes, keeping the topbar/login logo in sync without a page reload Quality gates: PHPUnit (2045), PHPStan L5, CS-Fixer, docs link/drift, codex skills sync — all green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 20:25:53 +02:00
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),
feat(tenant): per-theme logos + file-upload + button UI polish Replace the single tenant avatar with a pair of theme-scoped brand logos. Render only the theme-matching <img> server-side and swap src on theme toggle via a JS hook — no reload, no double request, no CSS tricks. Tenant logos - TenantLogoService (ImageUploadTrait) with theme whitelist and per-theme storage storage/tenants/{uuid}/logo/{light|dark}/, SIZES 128/256/512 - Public serving endpoint auth/tenant-logo-file so login can show the logo pre-auth; matching authenticated admin preview endpoint - appTenantLogoUrl(?size, ?theme) with 4-step fallback cascade; PDF + mail always request 'light' - Admin tenant edit: avatar block replaced by "Tenant logos" details block inside the Master-data tab, two side-by-side slots via Pico .grid with the core app-file-upload partial - Policy rename ABILITY_ADMIN_TENANTS_AVATAR_VIEW -> LOGO_VIEW, action routes logo / logo-delete / logo-file with theme body/query param - API endpoint path kept (backward compat), internals on new service - CLI tenant:logo-migrate-avatars moves legacy avatar/ -> logo/light/ idempotently (--dry-run, --yes, --cleanup) - i18n "Tenant image" removed, 12 new keys synced across de/en File upload component - Full-width preview + filename/actions below (3D stack layout) - Fixed 16:9 aspect ratio with 1rem inner padding for consistent preview size across any logo aspect - Transparency checker pattern as background so black logos stay visible on dark mode and white logos on light mode - form="" + deleteFormId support so the partial works with barrier forms inside another form Buttons - width:100% dropped from button[type="submit"]; scoped back via .login-main for the auth-flow primary CTA - .outline base rule now tints background via color-mix of --app-color so secondary/primary/danger outlines all gain a subtle surface - .outline.secondary restyled Stripe-style in both themes: solid white chip with soft shadow in light, solid elevated dark chip with white text in dark; neutral border replaces role-colored border - .app-action-success/.app-action-danger outlines get color-mix bg + theme-aware outline-text tokens for stronger contrast - Filled .primary/.app-action-success/.app-action-danger get raised box-shadow (inset highlight + drop) — opt-in via class so chrome buttons stay flat - Dropped the legacy .secondary utility that was clobbering the custom-property cascade with a hardcoded muted color Theme swap - Logo img carries data-src-light + data-src-dark; theme-toggle JS swaps src when data-theme changes, keeping the topbar/login logo in sync without a page reload Quality gates: PHPUnit (2045), PHPStan L5, CS-Fixer, docs link/drift, codex skills sync — all green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 20:25:53 +02:00
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_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);
}
feat(tenant): per-theme logos + file-upload + button UI polish Replace the single tenant avatar with a pair of theme-scoped brand logos. Render only the theme-matching <img> server-side and swap src on theme toggle via a JS hook — no reload, no double request, no CSS tricks. Tenant logos - TenantLogoService (ImageUploadTrait) with theme whitelist and per-theme storage storage/tenants/{uuid}/logo/{light|dark}/, SIZES 128/256/512 - Public serving endpoint auth/tenant-logo-file so login can show the logo pre-auth; matching authenticated admin preview endpoint - appTenantLogoUrl(?size, ?theme) with 4-step fallback cascade; PDF + mail always request 'light' - Admin tenant edit: avatar block replaced by "Tenant logos" details block inside the Master-data tab, two side-by-side slots via Pico .grid with the core app-file-upload partial - Policy rename ABILITY_ADMIN_TENANTS_AVATAR_VIEW -> LOGO_VIEW, action routes logo / logo-delete / logo-file with theme body/query param - API endpoint path kept (backward compat), internals on new service - CLI tenant:logo-migrate-avatars moves legacy avatar/ -> logo/light/ idempotently (--dry-run, --yes, --cleanup) - i18n "Tenant image" removed, 12 new keys synced across de/en File upload component - Full-width preview + filename/actions below (3D stack layout) - Fixed 16:9 aspect ratio with 1rem inner padding for consistent preview size across any logo aspect - Transparency checker pattern as background so black logos stay visible on dark mode and white logos on light mode - form="" + deleteFormId support so the partial works with barrier forms inside another form Buttons - width:100% dropped from button[type="submit"]; scoped back via .login-main for the auth-flow primary CTA - .outline base rule now tints background via color-mix of --app-color so secondary/primary/danger outlines all gain a subtle surface - .outline.secondary restyled Stripe-style in both themes: solid white chip with soft shadow in light, solid elevated dark chip with white text in dark; neutral border replaces role-colored border - .app-action-success/.app-action-danger outlines get color-mix bg + theme-aware outline-text tokens for stronger contrast - Filled .primary/.app-action-success/.app-action-danger get raised box-shadow (inset highlight + drop) — opt-in via class so chrome buttons stay flat - Dropped the legacy .secondary utility that was clobbering the custom-property cascade with a hardcoded muted color Theme swap - Logo img carries data-src-light + data-src-dark; theme-toggle JS swaps src when data-theme changes, keeping the topbar/login logo in sync without a page reload Quality gates: PHPUnit (2045), PHPStan L5, CS-Fixer, docs link/drift, codex skills sync — all green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 20:25:53 +02:00
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);
}
}