From cd9611a669fd37b524061e3d404dc7505c94b6f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 13 Sep 2023 10:09:42 +0200 Subject: [PATCH] Remove unused dependency on DefaultVoter (+ fix when throwing an exception) --- .../Security/Authorization/DefaultVoterHelper.php | 11 ++++++++--- .../Authorization/DefaultVoterHelperFactory.php | 5 ++--- .../Authorization/DefaultVoterHelperGenerator.php | 9 +++------ .../Security/Resolver/DefaultScopeResolver.php | 8 +++++--- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelper.php b/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelper.php index 8819f4f66..9b378231c 100644 --- a/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelper.php +++ b/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelper.php @@ -18,7 +18,10 @@ use function in_array; final readonly class DefaultVoterHelper implements VoterHelperInterface { - public function __construct(private AuthorizationHelper $authorizationHelper, private CenterResolverDispatcherInterface $centerResolverDispatcher, private array $configuration) {} + public function __construct( + private AuthorizationHelper $authorizationHelper, + private array $configuration + ) {} public function supports($attribute, $subject): bool { @@ -37,7 +40,9 @@ final readonly class DefaultVoterHelper implements VoterHelperInterface public function voteOnAttribute($attribute, $subject, $token): bool { - if (!$token->getUser() instanceof User) { + $user = $token->getUser(); + + if (!$user instanceof User) { return false; } @@ -46,7 +51,7 @@ final readonly class DefaultVoterHelper implements VoterHelperInterface } return $this->authorizationHelper->userHasAccess( - $token->getUser(), + $user, $subject, $attribute ); diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperFactory.php b/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperFactory.php index 78f99b436..b952d845e 100644 --- a/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperFactory.php +++ b/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperFactory.php @@ -15,13 +15,12 @@ use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface; class DefaultVoterHelperFactory implements VoterHelperFactoryInterface { - public function __construct(protected AuthorizationHelper $authorizationHelper, protected CenterResolverDispatcherInterface $centerResolverDispatcher) {} + public function __construct(protected AuthorizationHelper $authorizationHelper) {} public function generate($context): VoterGeneratorInterface { return new DefaultVoterHelperGenerator( - $this->authorizationHelper, - $this->centerResolverDispatcher + $this->authorizationHelper ); } } diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperGenerator.php b/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperGenerator.php index e0a6495c4..6a1fe5356 100644 --- a/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperGenerator.php +++ b/src/Bundle/ChillMainBundle/Security/Authorization/DefaultVoterHelperGenerator.php @@ -11,17 +11,15 @@ declare(strict_types=1); namespace Chill\MainBundle\Security\Authorization; -use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface; - final class DefaultVoterHelperGenerator implements VoterGeneratorInterface { private array $configuration = []; - public function __construct(private readonly AuthorizationHelper $authorizationHelper, private readonly CenterResolverDispatcherInterface $centerResolverDispatcher) {} + public function __construct(private readonly AuthorizationHelper $authorizationHelper) {} - public function addCheckFor(?string $subject, array $attributes): self + public function addCheckFor(?string $class, array $attributes): self { - $this->configuration[] = [$attributes, $subject]; + $this->configuration[] = [$attributes, $class]; return $this; } @@ -30,7 +28,6 @@ final class DefaultVoterHelperGenerator implements VoterGeneratorInterface { return new DefaultVoterHelper( $this->authorizationHelper, - $this->centerResolverDispatcher, $this->configuration ); } diff --git a/src/Bundle/ChillMainBundle/Security/Resolver/DefaultScopeResolver.php b/src/Bundle/ChillMainBundle/Security/Resolver/DefaultScopeResolver.php index 384272cdd..25697ac15 100644 --- a/src/Bundle/ChillMainBundle/Security/Resolver/DefaultScopeResolver.php +++ b/src/Bundle/ChillMainBundle/Security/Resolver/DefaultScopeResolver.php @@ -41,9 +41,11 @@ class DefaultScopeResolver implements ScopeResolverInterface } throw new UnexpectedValueException( - 'should be an instanceof %s or %s', - HasScopesInterface::class, - HasScopeInterface::class + sprintf( + 'should be an instanceof %s or %s', + HasScopesInterface::class, + HasScopeInterface::class + ) ); }