2026-02-24 08:49:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Tests\Service\Access;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Service\Access\PermissionService;
|
|
|
|
|
use MintyPHP\Service\Access\TenantAuthorizationPolicy;
|
2026-03-13 11:31:33 +01:00
|
|
|
use MintyPHP\Service\Tenant\TenantScopeService;
|
2026-02-24 08:49:40 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class TenantAuthorizationPolicyTest extends TestCase
|
|
|
|
|
{
|
2026-03-04 15:56:58 +01:00
|
|
|
use AuthorizationPolicyTestSupport;
|
|
|
|
|
|
2026-02-24 08:49:40 +01:00
|
|
|
public function testViewRequiresTenantsViewPermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$permissionService->expects($this->once())
|
2026-02-24 08:49:40 +01:00
|
|
|
->method('userHas')
|
|
|
|
|
->with(11, PermissionService::TENANTS_VIEW)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->never())->method('getUserTenantIds');
|
|
|
|
|
$scopeGateway->expects($this->never())->method('isStrict');
|
|
|
|
|
$scopeGateway->expects($this->never())->method('hasGlobalAccess');
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_VIEW, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertForbiddenDecision($decision);
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testViewReturnsCapabilitiesAndScopeData(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
11 => [PermissionService::TENANTS_VIEW, PermissionService::TENANTS_CREATE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(11)
|
|
|
|
|
->willReturn([2, 3]);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('isStrict')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('hasGlobalAccess')
|
|
|
|
|
->with(11)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_VIEW, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['can_create_tenant'] ?? false));
|
|
|
|
|
$this->assertSame([2, 3], $capabilities['allowed_tenant_ids'] ?? []);
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['is_strict_scope'] ?? false));
|
|
|
|
|
$this->assertFalse((bool) ($capabilities['has_global_access'] ?? true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateRequiresTenantsCreatePermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$permissionService->expects($this->once())
|
2026-02-24 08:49:40 +01:00
|
|
|
->method('userHas')
|
|
|
|
|
->with(11, PermissionService::TENANTS_CREATE)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
|
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_CREATE, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'forbidden');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateReturnsSsoAndCustomFieldCapabilities(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
11 => [PermissionService::TENANTS_CREATE, PermissionService::TENANTS_SSO_MANAGE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
|
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_CREATE, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['can_manage_sso'] ?? false));
|
|
|
|
|
$this->assertFalse((bool) ($capabilities['can_manage_custom_fields'] ?? true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditContextAllowsInScopeViewer(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
11 => [PermissionService::TENANTS_VIEW],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 22, 11)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
'target_tenant_id' => 22,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['can_view_page'] ?? false));
|
|
|
|
|
$this->assertFalse((bool) ($capabilities['can_update_tenant'] ?? true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditContextDeniesOutOfScopeTenant(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 22, 11)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
'target_tenant_id' => 22,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'permission_denied');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditSubmitRequiresTenantsUpdatePermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
11 => [PermissionService::TENANTS_VIEW],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 22, 11)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_SUBMIT, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
'target_tenant_id' => 22,
|
|
|
|
|
'input' => ['description' => 'Tenant A'],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertForbiddenDecision($decision);
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditSubmitDeniesSsoFieldsWithoutSsoPermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
11 => [PermissionService::TENANTS_VIEW, PermissionService::TENANTS_UPDATE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 22, 11)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_SUBMIT, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
'target_tenant_id' => 22,
|
|
|
|
|
'input' => ['microsoft_enabled' => '1'],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertForbiddenDecision($decision);
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-10 22:48:10 +01:00
|
|
|
public function testEditSubmitDeniesAutoRememberModeWithoutSsoPermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-10 22:48:10 +01:00
|
|
|
11 => [PermissionService::TENANTS_VIEW, PermissionService::TENANTS_UPDATE],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-03-10 22:48:10 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 22, 11)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-03-10 22:48:10 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_SUBMIT, [
|
|
|
|
|
'actor_user_id' => 11,
|
|
|
|
|
'target_tenant_id' => 22,
|
|
|
|
|
'input' => ['microsoft_auto_remember_mode' => '1'],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertForbiddenDecision($decision);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 08:49:40 +01:00
|
|
|
public function testCustomFieldsManageRequiresPermissionAndScope(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
9 => [PermissionService::CUSTOM_FIELDS_MANAGE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 5, 9)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_CUSTOM_FIELDS_MANAGE, [
|
|
|
|
|
'actor_user_id' => 9,
|
|
|
|
|
'target_tenant_id' => 5,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCustomFieldsManageDeniedWhenOutOfScope(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
9 => [PermissionService::CUSTOM_FIELDS_MANAGE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 5, 9)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_CUSTOM_FIELDS_MANAGE, [
|
|
|
|
|
'actor_user_id' => 9,
|
|
|
|
|
'target_tenant_id' => 5,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'permission_denied');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAvatarViewAllowsTenantViewerInScope(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
15 => [PermissionService::TENANTS_VIEW],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 9, 15)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_AVATAR_VIEW, [
|
|
|
|
|
'actor_user_id' => 15,
|
|
|
|
|
'target_tenant_id' => 9,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMediaUpdateRequiresTenantsUpdatePermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->never())->method('canAccess');
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_MEDIA_UPDATE, [
|
|
|
|
|
'actor_user_id' => 15,
|
|
|
|
|
'target_tenant_id' => 9,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'forbidden');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteRequiresPermissionAndScope(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
5 => [PermissionService::TENANTS_DELETE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('tenants', 17, 5)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new TenantAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_DELETE, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
'target_tenant_id' => 17,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
}
|
|
|
|
|
}
|