Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/Controller/ContextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions lib/Controller/ContextPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 13 additions & 2 deletions lib/Middleware/Attribute/RequirePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
) {
Expand All @@ -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;
}
}
75 changes: 75 additions & 0 deletions tests/unit/Middleware/PermissionMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Tables\Tests\Unit\Middleware;

use OCA\Tables\AppInfo\Application;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Middleware\Attribute\RequirePermission;
use OCA\Tables\Middleware\PermissionMiddleware;
use OCA\Tables\Service\ContextService;
use OCA\Tables\Service\PermissionsService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class PermissionMiddlewareTest extends TestCase {
private PermissionsService&MockObject $permissionsService;
private PermissionMiddleware $middleware;
private Controller $controller;
/** @var array<string, mixed> */
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);
}
}
Loading