1
0
mirror of synced 2024-12-01 09:06:03 +03:00

Add setUserRepository methods to authenticators

This commit is contained in:
Кривич Сергей 2022-07-20 16:25:53 +03:00
parent 430c5d9d4a
commit 6243bd1261
5 changed files with 29 additions and 33 deletions

View File

@ -2,6 +2,7 @@
namespace RetailCrm\ServiceBundle\Security; namespace RetailCrm\ServiceBundle\Security;
use Doctrine\Persistence\ObjectRepository;
use RetailCrm\ServiceBundle\Models\Error; use RetailCrm\ServiceBundle\Models\Error;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory; use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -15,6 +16,8 @@ abstract class AbstractClientAuthenticator extends AbstractAuthenticator
{ {
public const AUTH_FIELD = 'clientId'; public const AUTH_FIELD = 'clientId';
protected ObjectRepository $userRepository;
public function __construct(private ErrorJsonResponseFactory $errorResponseFactory) public function __construct(private ErrorJsonResponseFactory $errorResponseFactory)
{ {
} }
@ -35,4 +38,9 @@ abstract class AbstractClientAuthenticator extends AbstractAuthenticator
{ {
return null; return null;
} }
public function setUserRepository(ObjectRepository $userRepository): void
{
$this->userRepository = $userRepository;
}
} }

View File

@ -2,8 +2,6 @@
namespace RetailCrm\ServiceBundle\Security; namespace RetailCrm\ServiceBundle\Security;
use Doctrine\Persistence\ObjectRepository;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; 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 class CallbackClientAuthenticator extends AbstractClientAuthenticator
{ {
public function __construct(
ErrorJsonResponseFactory $errorResponseFactory,
private ObjectRepository $repository,
) {
parent::__construct($errorResponseFactory);
}
public function supports(Request $request): bool public function supports(Request $request): bool
{ {
return $request->request->has(static::AUTH_FIELD) || $request->query->has(static::AUTH_FIELD); return $request->request->has(static::AUTH_FIELD) || $request->query->has(static::AUTH_FIELD);
@ -32,9 +23,10 @@ class CallbackClientAuthenticator extends AbstractClientAuthenticator
} }
return new SelfValidatingPassport( return new SelfValidatingPassport(
new UserBadge($identifier, function ($userIdentifier) { new UserBadge(
return $this->repository->findOneBy([static::AUTH_FIELD => $userIdentifier]); $identifier,
}), fn ($userIdentifier) => $this->userRepository->findOneBy([static::AUTH_FIELD => $userIdentifier])
),
[] []
); );
} }

View File

@ -2,7 +2,6 @@
namespace RetailCrm\ServiceBundle\Security; namespace RetailCrm\ServiceBundle\Security;
use Doctrine\Persistence\ObjectRepository;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory; use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AuthenticationException;
@ -17,7 +16,6 @@ class FrontApiClientAuthenticator extends AbstractClientAuthenticator
public function __construct( public function __construct(
ErrorJsonResponseFactory $errorResponseFactory, ErrorJsonResponseFactory $errorResponseFactory,
private Security $security, private Security $security,
private ObjectRepository $repository
) { ) {
parent::__construct($errorResponseFactory); parent::__construct($errorResponseFactory);
} }
@ -39,9 +37,10 @@ class FrontApiClientAuthenticator extends AbstractClientAuthenticator
} }
return new SelfValidatingPassport( return new SelfValidatingPassport(
new UserBadge($identifier, function ($userIdentifier) { new UserBadge(
return $this->repository->findOneBy([static::AUTH_FIELD => $userIdentifier]); $identifier,
}), fn ($userIdentifier) => $this->userRepository->findOneBy([static::AUTH_FIELD => $userIdentifier]),
),
[new RememberMeBadge()] [new RememberMeBadge()]
); );
} }

View File

@ -28,8 +28,7 @@ class CallbackClientAuthenticatorTest extends TestCase
) )
); );
$userRepository = $this->createMock(ObjectRepository::class); $auth = new CallbackClientAuthenticator($errorResponseFactory);
$auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository);
$result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException()); $result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException());
static::assertInstanceOf(JsonResponse::class, $result); static::assertInstanceOf(JsonResponse::class, $result);
@ -40,8 +39,7 @@ class CallbackClientAuthenticatorTest extends TestCase
{ {
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$userRepository = $this->createMock(ObjectRepository::class); $auth = new CallbackClientAuthenticator($errorResponseFactory);
$auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository);
$result = $auth->supports(new Request([], [CallbackClientAuthenticator::AUTH_FIELD => '123'])); $result = $auth->supports(new Request([], [CallbackClientAuthenticator::AUTH_FIELD => '123']));
static::assertTrue($result); static::assertTrue($result);
@ -67,7 +65,8 @@ class CallbackClientAuthenticatorTest extends TestCase
->willReturn($user) ->willReturn($user)
; ;
$auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository); $auth = new CallbackClientAuthenticator($errorResponseFactory);
$auth->setUserRepository($userRepository);
$passport = $auth->authenticate(new Request([], [CallbackClientAuthenticator::AUTH_FIELD => '123'])); $passport = $auth->authenticate(new Request([], [CallbackClientAuthenticator::AUTH_FIELD => '123']));
$authUser = $passport->getUser(); $authUser = $passport->getUser();
@ -82,8 +81,7 @@ class CallbackClientAuthenticatorTest extends TestCase
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$request = $this->createMock(Request::class); $request = $this->createMock(Request::class);
$token = $this->createMock(TokenInterface::class); $token = $this->createMock(TokenInterface::class);
$userRepository = $this->createMock(ObjectRepository::class); $auth = new CallbackClientAuthenticator($errorResponseFactory);
$auth = new CallbackClientAuthenticator($errorResponseFactory, $userRepository);
$result = $auth->onAuthenticationSuccess($request, $token, 'key'); $result = $auth->onAuthenticationSuccess($request, $token, 'key');

View File

@ -29,8 +29,7 @@ class FrontApiClientAuthenticatorTest extends TestCase
) )
); );
$security = $this->createMock(Security::class); $security = $this->createMock(Security::class);
$userRepository = $this->createMock(ObjectRepository::class); $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
$result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException()); $result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException());
static::assertInstanceOf(JsonResponse::class, $result); static::assertInstanceOf(JsonResponse::class, $result);
@ -42,8 +41,8 @@ class FrontApiClientAuthenticatorTest extends TestCase
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class); $security = $this->createMock(Security::class);
$security->method('getUser')->willReturn(new User()); $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()); $result = $auth->supports(new Request());
static::assertFalse($result); static::assertFalse($result);
@ -54,8 +53,7 @@ class FrontApiClientAuthenticatorTest extends TestCase
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class); $errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class); $security = $this->createMock(Security::class);
$security->method('getUser')->willReturn(null); $security->method('getUser')->willReturn(null);
$userRepository = $this->createMock(ObjectRepository::class); $auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
$result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123'])); $result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
static::assertTrue($result); static::assertTrue($result);
@ -74,7 +72,8 @@ class FrontApiClientAuthenticatorTest extends TestCase
->with([FrontApiClientAuthenticator::AUTH_FIELD => '123']) ->with([FrontApiClientAuthenticator::AUTH_FIELD => '123'])
->willReturn($user) ->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'])); $passport = $auth->authenticate(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
$authUser = $passport->getUser(); $authUser = $passport->getUser();
@ -90,8 +89,8 @@ class FrontApiClientAuthenticatorTest extends TestCase
$security = $this->createMock(Security::class); $security = $this->createMock(Security::class);
$request = $this->createMock(Request::class); $request = $this->createMock(Request::class);
$token = $this->createMock(TokenInterface::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'); $result = $auth->onAuthenticationSuccess($request, $token, 'key');