Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ public function getAuthorizedAdminSettingClasses(): array {
$classes = explode(';', $this->reflector->getAnnotationParameter('AuthorizedAdminSetting', 'settings'));
}

$attribute = $this->reflector->getAttribute(AuthorizedAdminSetting::class);
if ($attribute !== null) {
foreach ($this->reflector->getAttributes(AuthorizedAdminSetting::class) as $attribute) {
/** @var AuthorizedAdminSetting $setting */
$setting = $attribute->newInstance();
$classes[] = $setting->getSettings();
Expand Down
14 changes: 13 additions & 1 deletion lib/private/AppFramework/Utility/ControllerMethodReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,25 @@ public function hasAnnotationOrAttribute(?string $annotationName, string $attrib
* @return ?\ReflectionAttribute<T>
*/
public function getAttribute(string $attributeClass): ?\ReflectionAttribute {
$attributes = $this->reflectionMethod->getAttributes($attributeClass);
$attributes = $this->getAttributes($attributeClass);
if (!empty($attributes)) {
return $attributes[0];
}
return null;
}

/**
* @template T
* @param class-string<T> $attributeClass
* @return list<\ReflectionAttribute<T>>
*/
public function getAttributes(string $attributeClass): array {
if ($this->reflectionMethod === null) {
return [];
}
return $this->reflectionMethod->getAttributes($attributeClass);
}

/**
* Check if a method contains an annotation
* @param string $name the name of the annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Test\AppFramework\Middleware\Security\Mock;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\Attribute\ExAppRequired;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
Expand Down Expand Up @@ -168,4 +169,21 @@ public function testAnnotationExAppRequired() {
#[ExAppRequired]
public function testAttributeExAppRequired() {
}

#[NoCSRFRequired]
#[AuthorizedAdminSetting(settings: FirstAuthorizedSetting::class)]
#[AuthorizedAdminSetting(settings: SecondAuthorizedSetting::class)]
public function testAttributeRepeatedAuthorizedAdminSetting() {
}

#[NoCSRFRequired]
#[AuthorizedAdminSetting(settings: FirstAuthorizedSetting::class)]
public function testAttributeSingleAuthorizedAdminSetting() {
}
}

class FirstAuthorizedSetting {
}

class SecondAuthorizedSetting {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\AppFramework\Middleware\Security\Mock\FirstAuthorizedSetting;
use Test\AppFramework\Middleware\Security\Mock\NormalController;
use Test\AppFramework\Middleware\Security\Mock\OCSController;
use Test\AppFramework\Middleware\Security\Mock\SecondAuthorizedSetting;
use Test\AppFramework\Middleware\Security\Mock\SecurityMiddlewareController;

class SecurityMiddlewareTest extends \Test\TestCase {
Expand Down Expand Up @@ -681,4 +683,33 @@ public function testExAppRequiredError(string $method): void {
$this->expectException(ExAppRequiredException::class);
$middleware->beforeController($this->controller, $method);
}

public function testRepeatedAuthorizedAdminSettingCollectsEveryAttribute(): void {
$this->reader->reflect($this->controller, 'testAttributeRepeatedAuthorizedAdminSetting');

$this->assertEqualsCanonicalizing(
[FirstAuthorizedSetting::class, SecondAuthorizedSetting::class],
$this->middleware->getAuthorizedAdminSettingClasses(),
);
}

public function testRepeatedAuthorizedAdminSettingAuthorizesTheSecondSetting(): void {
$middleware = $this->getMiddleware(true, false, false);
$this->reader->reflect($this->controller, 'testAttributeRepeatedAuthorizedAdminSetting');
$this->authorizedGroupMapper->method('findAllClassesForUser')
->willReturn([SecondAuthorizedSetting::class]);

$middleware->beforeController($this->controller, 'testAttributeRepeatedAuthorizedAdminSetting');
$this->addToAssertionCount(1);
}

public function testRepeatedAuthorizedAdminSettingDeniesAnUnauthorizedUser(): void {
$middleware = $this->getMiddleware(true, false, false);
$this->reader->reflect($this->controller, 'testAttributeRepeatedAuthorizedAdminSetting');
$this->authorizedGroupMapper->method('findAllClassesForUser')
->willReturn([]);

$this->expectException(NotAdminException::class);
$middleware->beforeController($this->controller, 'testAttributeRepeatedAuthorizedAdminSetting');
}
}
Loading