Skip to content
Merged
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
10 changes: 6 additions & 4 deletions src/Attribute/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
final class Security extends Attribute
{
/**
* @param array<string>|string|null $handlers
* @param array<string>|string|null $handlers
* @param array<string>|null $roles
* @param class-string<\RuntimeException> $exception
*/
public function __construct(
public readonly ?string $expression = null,
public readonly ?string $expression = null,
public array|string|null $handlers = null,
public ?string $message = null,
public ?string $exception = null,
public ?string $message = null,
public ?string $exception = null,
public ?array $roles = null,
) {
parent::__construct();
$this->setHandlers($handlers);
Expand Down
19 changes: 17 additions & 2 deletions src/Interceptor/Impl/SecurityInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,25 @@ public function prefix(Instance $instance): Response
return new Response();
}

$rolesExpressions = null;
if ($attribute->roles !== null) {
if ($attribute->expression !== null) {
throw new \RuntimeException('You cannot use both roles and expression in the Security attribute.');
}
$rolesExpressions = array_map(
static fn (string $role) => "is_granted('{$role}')",
$attribute->roles
);
}

$expression = $attribute->expression;
if ($expression === null) {
$role = $this->guessRoleName($instance);
$expression = "is_granted('{$role}')";
if ($rolesExpressions !== null) {
$expression = implode(' or ', $rolesExpressions);
} else {
$role = $this->guessRoleName($instance);
$expression = "is_granted('{$role}')";
}
}
$handlers = $this->getHandlers(SecurityHandler::class, $attribute);
foreach ($handlers as $handler) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Double/Stub/Security/SecurityAnnotatedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,19 @@ public function accessDeniedWithMessage(): void
public function accessDeniedWithException(): void
{
}

#[Security(roles: ['ROLE_1'])]
public function rolesParamOne(): void
{
}

#[Security(roles: ['ROLE_3', 'ROLE_1'])]
public function rolesParamMultiple(): void
{
}

#[Security("is_granted('ROLE_1')", roles: ['ROLE_1', 'ROLE_2'])]
public function conflict(): void
{
}
}
18 changes: 18 additions & 0 deletions tests/Interceptor/SecurityInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,22 @@ public function testAccessDeniedWithException(): void
$this->expectExceptionMessage('Invalid argument.');
$this->proxy->accessDeniedWithException();
}

public function testWithRolesParamOne(): void
{
$this->proxy->rolesParamOne();
$this->assertSame(['ROLE_1'], $this->handler->attributes);
}

public function testWithRolesParamMultiple(): void
{
$this->proxy->rolesParamMultiple();
$this->assertSame(['ROLE_3', 'ROLE_1'], $this->handler->attributes);
}

public function testWithRolesAndExpressionShouldThrow(): void
{
$this->expectException(\RuntimeException::class);
$this->proxy->conflict();
}
}