diff --git a/lib/Controller/ContextController.php b/lib/Controller/ContextController.php index 52a543735a..e13b58dd56 100644 --- a/lib/Controller/ContextController.php +++ b/lib/Controller/ContextController.php @@ -226,7 +226,7 @@ public function destroy(int $contextId): DataResponse { * @psalm-param int<0, 0> $newOwnerType */ #[NoAdminRequired] - #[RequirePermission(permission: Application::PERMISSION_OWNER, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_OWNER, type: 'context', idParam: 'contextId')] public function transfer(int $contextId, string $newOwnerId, int $newOwnerType = 0): DataResponse { try { return new DataResponse($this->contextService->transfer($contextId, $newOwnerId, $newOwnerType)->jsonSerialize()); @@ -285,7 +285,7 @@ public function updateContentOrder(int $contextId, int $pageId, array $content): * 404: Not found */ #[NoAdminRequired] - #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] public function exportScheme(int $contextId): DataResponse { try { $contextScheme = $this->contextService->getScheme($contextId, $this->userId); @@ -313,7 +313,7 @@ public function exportScheme(int $contextId): DataResponse { * 404: Not found */ #[NoAdminRequired] - #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] public function previewSchemeChanges(int $contextId, array $updateScheme): DataResponse { try { $changes = $this->contextService->compareSchemeChanges($contextId, $updateScheme, $this->userId); @@ -347,7 +347,7 @@ public function previewSchemeChanges(int $contextId, array $updateScheme): DataR * 404: Not found */ #[NoAdminRequired] - #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] public function importScheme(int $contextId, string $name, string $iconName, string $description, array $nodes, array $tables): DataResponse { try { $this->db->beginTransaction(); diff --git a/lib/Controller/ContextPageController.php b/lib/Controller/ContextPageController.php index aa94ee4c3a..e883da85c8 100644 --- a/lib/Controller/ContextPageController.php +++ b/lib/Controller/ContextPageController.php @@ -55,7 +55,7 @@ public function __construct( * 404: Not found, also when the user has no access to the application */ #[NoAdminRequired] - #[RequirePermission(permission: Application::PERMISSION_READ, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_READ, type: 'context', idParam: 'contextId')] #[ApiRoute(verb: 'GET', url: '/api/2/contexts/{contextId}/pages/{pageId}', requirements: ['contextId' => '\d+', 'pageId' => '\d+'])] public function show(int $contextId, int $pageId): DataResponse { try { @@ -78,7 +78,7 @@ public function show(int $contextId, int $pageId): DataResponse { * 404: Not found, also when the user has no access to the application */ #[NoAdminRequired] - #[RequirePermission(permission: Application::PERMISSION_READ, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_READ, type: 'context', idParam: 'contextId')] #[ApiRoute(verb: 'GET', url: '/api/2/contexts/{contextId}/pages/{slug}', requirements: ['contextId' => '\d+', 'slug' => '[a-z][a-z0-9_]*'])] public function showBySlug(int $contextId, string $slug): DataResponse { try { @@ -107,7 +107,7 @@ public function showBySlug(int $contextId, string $slug): DataResponse { */ #[NoAdminRequired] #[UserRateLimit(limit: 20, period: 60)] - #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] #[ApiRoute(verb: 'POST', url: '/api/2/contexts/{contextId}/pages', requirements: ['contextId' => '\d+'])] public function createGridPage(int $contextId, string $title, ?string $slug = null, ?string $icon = null, ?string $description = null): DataResponse { try { @@ -152,7 +152,7 @@ public function createGridPage(int $contextId, string $title, ?string $slug = nu */ #[NoAdminRequired] #[UserRateLimit(limit: 60, period: 60)] - #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] #[ApiRoute(verb: 'PATCH', url: '/api/2/contexts/{contextId}/pages/{pageId}', requirements: ['contextId' => '\d+', 'pageId' => '\d+'])] public function update(int $contextId, int $pageId, ?string $title = null, ?string $slug = null, ?string $icon = null, ?string $description = null, ?array $widgets = null, ?int $version = null): DataResponse { try { @@ -184,7 +184,7 @@ public function update(int $contextId, int $pageId, ?string $title = null, ?stri */ #[NoAdminRequired] #[UserRateLimit(limit: 20, period: 60)] - #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] #[ApiRoute(verb: 'DELETE', url: '/api/2/contexts/{contextId}/pages/{pageId}', requirements: ['contextId' => '\d+', 'pageId' => '\d+'])] public function destroy(int $contextId, int $pageId): DataResponse { try { diff --git a/lib/Middleware/Attribute/RequirePermission.php b/lib/Middleware/Attribute/RequirePermission.php index 6bac20afce..7b9bf079bd 100644 --- a/lib/Middleware/Attribute/RequirePermission.php +++ b/lib/Middleware/Attribute/RequirePermission.php @@ -9,11 +9,19 @@ use Attribute; +/** + * A permission the user needs on a node before the controller method runs. The type of + * the node is either fixed by `type`, a node type constant or `'context'` for an + * application, or read from the request parameter named by `typeParam`. + */ #[Attribute(Attribute::TARGET_METHOD)] class RequirePermission { + /** + * @param int|'context'|null $type a fixed node type, which the request cannot change + */ public function __construct( protected int $permission, - protected ?int $type = null, + protected int|string|null $type = null, protected string $typeParam = 'nodeType', protected string $idParam = 'nodeId', ) { @@ -31,7 +39,10 @@ public function getIdParam(): string { return $this->idParam; } - public function getType(): ?int { + /** + * @return int|'context'|null + */ + public function getType(): int|string|null { return $this->type; } } diff --git a/tests/unit/Middleware/PermissionMiddlewareTest.php b/tests/unit/Middleware/PermissionMiddlewareTest.php new file mode 100644 index 0000000000..46c9b1cd0e --- /dev/null +++ b/tests/unit/Middleware/PermissionMiddlewareTest.php @@ -0,0 +1,75 @@ + */ + private array $params = []; + + protected function setUp(): void { + parent::setUp(); + $request = $this->createMock(IRequest::class); + $request->method('getParam')->willReturnCallback(fn (string $key, mixed $default = null): mixed => $this->params[$key] ?? $default); + $this->permissionsService = $this->createMock(PermissionsService::class); + $this->middleware = new PermissionMiddleware($this->createMock(IControllerMethodReflector::class), $this->permissionsService, $request, 'alice', $this->createMock(ContextService::class)); + $this->controller = new class('tables', $request) extends Controller { + #[RequirePermission(permission: Application::PERMISSION_MANAGE, type: 'context', idParam: 'contextId')] + public function changeApplication(): void { + } + }; + } + + public static function typesFromTheRequest(): array { + return [ + 'none' => [null], + 'a table' => ['table'], + 'a table by number' => [(string)Application::NODE_TYPE_TABLE], + 'zero' => ['0'], + ]; + } + + /** + * @dataProvider typesFromTheRequest + */ + public function testAFixedTypeChecksTheApplicationWhateverTheRequestSays(?string $context): void { + $this->params = ['contextId' => '42', 'context' => $context]; + $this->permissionsService->method('canAccessContextById')->with(42, 'alice')->willReturn(true); + $this->permissionsService->expects($this->once())->method('canManageContextById')->with(42, 'alice')->willReturn(false); + $this->permissionsService->expects($this->never())->method('canAccessNodeById'); + $this->permissionsService->expects($this->never())->method('canManageNodeById'); + + $this->expectException(PermissionError::class); + $this->middleware->beforeController($this->controller, 'changeApplication'); + } + + public function testAManagerOfTheApplicationPasses(): void { + $this->params = ['contextId' => '42', 'context' => 'table']; + $this->permissionsService->method('canAccessContextById')->willReturn(true); + $this->permissionsService->method('canManageContextById')->with(42, 'alice')->willReturn(true); + + $this->middleware->beforeController($this->controller, 'changeApplication'); + $this->addToAssertionCount(1); + } +}