Skip to content

Commit c9e5071

Browse files
authored
feat(symfony): deprecate Symfony Security AccessDeniedException (#8318)
1 parent c3fd6dd commit c9e5071

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/Symfony/Security/Exception/AccessDeniedException.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@
1313

1414
namespace ApiPlatform\Symfony\Security\Exception;
1515

16+
use ApiPlatform\Metadata\Exception\AccessDeniedException as MetadataAccessDeniedException;
1617
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
1718
use Symfony\Component\Security\Core\Exception\AccessDeniedException as ExceptionAccessDeniedException;
1819

1920
/**
20-
* TODO: deprecate in favor of Metadata.
21+
* @deprecated since API Platform 4.4, use {@see MetadataAccessDeniedException} instead
2122
*/
2223
final class AccessDeniedException extends ExceptionAccessDeniedException implements HttpExceptionInterface
2324
{
25+
public function __construct(string $message = 'Access Denied.', ?\Throwable $previous = null, int $code = 403, bool $triggerDeprecation = true)
26+
{
27+
if ($triggerDeprecation) {
28+
trigger_deprecation('api-platform/core', '4.4', 'The "%s" class is deprecated, use "%s" instead.', self::class, MetadataAccessDeniedException::class);
29+
}
30+
31+
parent::__construct($message, $previous, $code);
32+
}
33+
2434
public function getStatusCode(): int
2535
{
2636
return 403;

src/Symfony/Security/State/AccessCheckerProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
9898
}
9999

100100
if (!$this->resourceAccessChecker->isGranted($operation->getClass(), $isGranted, $resourceAccessCheckerContext)) {
101-
$operation instanceof GraphQlOperation ? throw new AccessDeniedHttpException($message ?? 'Access Denied.') : throw new AccessDeniedException($message ?? 'Access Denied.');
101+
$operation instanceof GraphQlOperation ? throw new AccessDeniedHttpException($message ?? 'Access Denied.') : throw new AccessDeniedException($message ?? 'Access Denied.', null, 403, false);
102102
}
103103

104104
return 'pre_read' === $this->event ? $this->decorated->provide($operation, $uriVariables, $context) : $body;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Symfony\Security\Exception;
15+
16+
use ApiPlatform\Symfony\Security\Exception\AccessDeniedException;
17+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class AccessDeniedExceptionTest extends TestCase
21+
{
22+
#[IgnoreDeprecations]
23+
public function testInstantiationTriggersDeprecation(): void
24+
{
25+
$this->expectUserDeprecationMessage('Since api-platform/core 4.4: The "ApiPlatform\Symfony\Security\Exception\AccessDeniedException" class is deprecated, use "ApiPlatform\Metadata\Exception\AccessDeniedException" instead.');
26+
27+
new AccessDeniedException();
28+
}
29+
30+
#[IgnoreDeprecations]
31+
public function testKeepsBaseExceptionBehavior(): void
32+
{
33+
$previous = new \RuntimeException('previous');
34+
$exception = new AccessDeniedException('Custom message', $previous, 403);
35+
36+
$this->assertSame('Custom message', $exception->getMessage());
37+
$this->assertSame($previous, $exception->getPrevious());
38+
$this->assertSame(403, $exception->getStatusCode());
39+
$this->assertSame([], $exception->getHeaders());
40+
}
41+
}

0 commit comments

Comments
 (0)