diff --git a/Security/AbstractClientAuthenticator.php b/Security/AbstractClientAuthenticator.php index 500f65d..f7a809b 100644 --- a/Security/AbstractClientAuthenticator.php +++ b/Security/AbstractClientAuthenticator.php @@ -2,6 +2,7 @@ namespace RetailCrm\ServiceBundle\Security; +use Doctrine\Persistence\ObjectRepository; use RetailCrm\ServiceBundle\Models\Error; use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory; use Symfony\Component\HttpFoundation\Request; @@ -15,6 +16,8 @@ abstract class AbstractClientAuthenticator extends AbstractAuthenticator { public const AUTH_FIELD = 'clientId'; + protected ObjectRepository $userRepository; + public function __construct(private ErrorJsonResponseFactory $errorResponseFactory) { } @@ -35,4 +38,9 @@ abstract class AbstractClientAuthenticator extends AbstractAuthenticator { return null; } + + public function setUserRepository(ObjectRepository $userRepository): void + { + $this->userRepository = $userRepository; + } } diff --git a/Security/CallbackClientAuthenticator.php b/Security/CallbackClientAuthenticator.php index ece82be..eb3711f 100644 --- a/Security/CallbackClientAuthenticator.php +++ b/Security/CallbackClientAuthenticator.php @@ -2,8 +2,6 @@ namespace RetailCrm\ServiceBundle\Security; -use Doctrine\Persistence\ObjectRepository; -use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; @@ -12,13 +10,6 @@ use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPasspor class CallbackClientAuthenticator extends AbstractClientAuthenticator { - public function __construct( - ErrorJsonResponseFactory $errorResponseFactory, - private ObjectRepository $repository, - ) { - parent::__construct($errorResponseFactory); - } - public function supports(Request $request): bool { return $request->request->has(static::AUTH_FIELD) || $request->query->has(static::AUTH_FIELD); @@ -32,9 +23,10 @@ class CallbackClientAuthenticator extends AbstractClientAuthenticator } return new SelfValidatingPassport( - new UserBadge($identifier, function ($userIdentifier) { - return $this->repository->findOneBy([static::AUTH_FIELD => $userIdentifier]); - }), + new UserBadge( + $identifier, + fn ($userIdentifier) => $this->userRepository->findOneBy([static::AUTH_FIELD => $userIdentifier]) + ), [] ); } diff --git a/Security/FrontApiClientAuthenticator.php b/Security/FrontApiClientAuthenticator.php index a2a2e67..b92f3f9 100644 --- a/Security/FrontApiClientAuthenticator.php +++ b/Security/FrontApiClientAuthenticator.php @@ -2,7 +2,6 @@ namespace RetailCrm\ServiceBundle\Security; -use Doctrine\Persistence\ObjectRepository; use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Exception\AuthenticationException; @@ -17,7 +16,6 @@ class FrontApiClientAuthenticator extends AbstractClientAuthenticator public function __construct( ErrorJsonResponseFactory $errorResponseFactory, private Security $security, - private ObjectRepository $repository ) { parent::__construct($errorResponseFactory); } @@ -39,9 +37,10 @@ class FrontApiClientAuthenticator extends AbstractClientAuthenticator } return new SelfValidatingPassport( - new UserBadge($identifier, function ($userIdentifier) { - return $this->repository->findOneBy([static::AUTH_FIELD => $userIdentifier]); - }), + new UserBadge( + $identifier, + fn ($userIdentifier) => $this->userRepository->findOneBy([static::AUTH_FIELD => $userIdentifier]), + ), [new RememberMeBadge()] ); } diff --git a/Tests/Security/CallbackClientAuthenticatorTest.php b/Tests/Security/CallbackClientAuthenticatorTest.php index a604a21..9a642f5 100644 --- a/Tests/Security/CallbackClientAuthenticatorTest.php +++ b/Tests/Security/CallbackClientAuthenticatorTest.php @@ -28,8 +28,7 @@ class CallbackClientAuthenticatorTest extends TestCase ) ); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository); + $auth = new CallbackClientAuthenticator($errorResponseFactory); $result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException()); static::assertInstanceOf(JsonResponse::class, $result); @@ -40,8 +39,7 @@ class CallbackClientAuthenticatorTest extends TestCase { $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository); + $auth = new CallbackClientAuthenticator($errorResponseFactory); $result = $auth->supports(new Request([], [CallbackClientAuthenticator::AUTH_FIELD => '123'])); static::assertTrue($result); @@ -67,7 +65,8 @@ class CallbackClientAuthenticatorTest extends TestCase ->willReturn($user) ; - $auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository); + $auth = new CallbackClientAuthenticator($errorResponseFactory); + $auth->setUserRepository($userRepository); $passport = $auth->authenticate(new Request([], [CallbackClientAuthenticator::AUTH_FIELD => '123'])); $authUser = $passport->getUser(); @@ -82,8 +81,7 @@ class CallbackClientAuthenticatorTest extends TestCase $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $request = $this->createMock(Request::class); $token = $this->createMock(TokenInterface::class); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository); + $auth = new CallbackClientAuthenticator($errorResponseFactory); $result = $auth->onAuthenticationSuccess($request, $token, 'key'); diff --git a/Tests/Security/FrontApiClientAuthenticatorTest.php b/Tests/Security/FrontApiClientAuthenticatorTest.php index 219fb6a..58c420e 100644 --- a/Tests/Security/FrontApiClientAuthenticatorTest.php +++ b/Tests/Security/FrontApiClientAuthenticatorTest.php @@ -29,8 +29,7 @@ class FrontApiClientAuthenticatorTest extends TestCase ) ); $security = $this->createMock(Security::class); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository); + $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security); $result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException()); static::assertInstanceOf(JsonResponse::class, $result); @@ -42,8 +41,8 @@ class FrontApiClientAuthenticatorTest extends TestCase $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $security = $this->createMock(Security::class); $security->method('getUser')->willReturn(new User()); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository); + + $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security); $result = $auth->supports(new Request()); static::assertFalse($result); @@ -54,8 +53,7 @@ class FrontApiClientAuthenticatorTest extends TestCase $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $security = $this->createMock(Security::class); $security->method('getUser')->willReturn(null); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository); + $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security); $result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123'])); static::assertTrue($result); @@ -74,7 +72,8 @@ class FrontApiClientAuthenticatorTest extends TestCase ->with([FrontApiClientAuthenticator::AUTH_FIELD => '123']) ->willReturn($user) ; - $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository); + $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security); + $auth->setUserRepository($userRepository); $passport = $auth->authenticate(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123'])); $authUser = $passport->getUser(); @@ -90,8 +89,8 @@ class FrontApiClientAuthenticatorTest extends TestCase $security = $this->createMock(Security::class); $request = $this->createMock(Request::class); $token = $this->createMock(TokenInterface::class); - $userRepository = $this->createMock(ObjectRepository::class); - $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository); + + $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security); $result = $auth->onAuthenticationSuccess($request, $token, 'key');