diff --git a/src/Model/Mutation/Login/LoginMutation.php b/src/Model/Mutation/Login/LoginMutation.php index 37ba8e5db..d58e6be03 100644 --- a/src/Model/Mutation/Login/LoginMutation.php +++ b/src/Model/Mutation/Login/LoginMutation.php @@ -5,7 +5,6 @@ namespace Shopsys\FrontendApiBundle\Model\Mutation\Login; use Overblog\GraphQLBundle\Definition\Argument; -use Ramsey\Uuid\Uuid; use Shopsys\FrameworkBundle\Model\Customer\User\FrontendCustomerUserProvider; use Shopsys\FrameworkBundle\Model\Product\List\ProductListFacade; use Shopsys\FrontendApiBundle\Model\Cart\MergeCartFacade; @@ -15,6 +14,7 @@ use Shopsys\FrontendApiBundle\Model\Mutation\AbstractMutation; use Shopsys\FrontendApiBundle\Model\Mutation\Customer\User\Exception\InvalidCredentialsUserError; use Shopsys\FrontendApiBundle\Model\Mutation\Customer\User\Exception\TooManyLoginAttemptsUserError; +use Shopsys\FrontendApiBundle\Model\Security\LoginAsUserFacade; use Shopsys\FrontendApiBundle\Model\Security\LoginResultData; use Shopsys\FrontendApiBundle\Model\Security\LoginResultDataFactory; use Shopsys\FrontendApiBundle\Model\Security\TokensDataFactory; @@ -38,6 +38,7 @@ class LoginMutation extends AbstractMutation * @param \Shopsys\FrontendApiBundle\Model\Security\LoginResultDataFactory $loginResultDataFactory * @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeFacade $customerUserLoginTypeFacade * @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory + * @param \Shopsys\FrontendApiBundle\Model\Security\LoginAsUserFacade $loginAsUserFacade */ public function __construct( protected readonly FrontendCustomerUserProvider $frontendCustomerUserProvider, @@ -51,6 +52,7 @@ public function __construct( protected readonly LoginResultDataFactory $loginResultDataFactory, protected readonly CustomerUserLoginTypeFacade $customerUserLoginTypeFacade, protected readonly CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory, + protected readonly LoginAsUserFacade $loginAsUserFacade, ) { } @@ -71,41 +73,25 @@ public function loginMutation(Argument $argument): LoginResultData } try { - $user = $this->frontendCustomerUserProvider->loadUserByUsername($input['email']); - } catch (UserNotFoundException $e) { + $customerUser = $this->frontendCustomerUserProvider->loadUserByUsername($input['email']); + } catch (UserNotFoundException) { throw new InvalidCredentialsUserError('Log in failed.'); } - if (!$this->userPasswordHasher->isPasswordValid($user, $input['password'])) { + if (!$this->userPasswordHasher->isPasswordValid($customerUser, $input['password'])) { throw new InvalidCredentialsUserError('Log in failed.'); } - if (array_key_exists('cartUuid', $input) && $input['cartUuid'] !== null) { - if (array_key_exists('shouldOverwriteCustomerUserCart', $input) && $input['shouldOverwriteCustomerUserCart']) { - $this->mergeCartFacade->overwriteCustomerCartWithCartByUuid($input['cartUuid'], $user); - } else { - $this->mergeCartFacade->mergeCartByUuidToCustomerCart($input['cartUuid'], $user); - } - } - - $deviceId = Uuid::uuid4()->toString(); - $this->loginRateLimiter->reset($this->requestStack->getCurrentRequest()); - if (array_key_exists('productListsUuids', $input) && $input['productListsUuids']) { - $this->productListFacade->mergeProductListsToCustomerUser($input['productListsUuids'], $user); - } - - $this->customerUserLoginTypeFacade->updateCustomerUserLoginTypes( - $this->customerUserLoginTypeDataFactory->create($user, LoginTypeEnum::WEB), - ); - - return $this->loginResultDataFactory->create( - $this->tokensDataFactory->create( - $this->tokenFacade->createAccessTokenAsString($user, $deviceId), - $this->tokenFacade->createRefreshTokenAsString($user, $deviceId), - ), - $this->mergeCartFacade->shouldShowCartMergeInfo(), + return $this->loginAsUserFacade->runLoginSteps( + $customerUser, + LoginTypeEnum::WEB, + false, + $input['productListsUuids'] ?? [], + $input['shouldOverwriteCustomerUserCart'] ?? false, + $input['cartUuid'] ?? null, + null, ); } } diff --git a/src/Model/Security/LoginAsUserFacade.php b/src/Model/Security/LoginAsUserFacade.php index ee7be0f98..e30e24f1d 100644 --- a/src/Model/Security/LoginAsUserFacade.php +++ b/src/Model/Security/LoginAsUserFacade.php @@ -10,7 +10,9 @@ use Shopsys\FrameworkBundle\Model\Administrator\Security\AdministratorFrontSecurityFacade; use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser; use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserRepository; +use Shopsys\FrameworkBundle\Model\Product\List\ProductListFacade; use Shopsys\FrameworkBundle\Model\Security\Exception\LoginAsRememberedUserException; +use Shopsys\FrontendApiBundle\Model\Cart\MergeCartFacade; use Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeDataFactory; use Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeFacade; use Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\LoginTypeEnum; @@ -31,6 +33,9 @@ class LoginAsUserFacade * @param \Shopsys\FrontendApiBundle\Model\Security\TokensDataFactory $tokensDataFactory * @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeFacade $customerUserLoginTypeFacade * @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory + * @param \Shopsys\FrontendApiBundle\Model\Security\LoginResultDataFactory $loginResultDataFactory + * @param \Shopsys\FrameworkBundle\Model\Product\List\ProductListFacade $productListFacade + * @param \Shopsys\FrontendApiBundle\Model\Cart\MergeCartFacade $mergeCartFacade */ public function __construct( protected readonly CustomerUserRepository $customerUserRepository, @@ -42,6 +47,9 @@ public function __construct( protected readonly TokensDataFactory $tokensDataFactory, protected readonly CustomerUserLoginTypeFacade $customerUserLoginTypeFacade, protected readonly CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory, + protected readonly LoginResultDataFactory $loginResultDataFactory, + protected readonly ProductListFacade $productListFacade, + protected readonly MergeCartFacade $mergeCartFacade, ) { } @@ -108,4 +116,46 @@ public function loginAndReturnAccessAndRefreshToken(CustomerUser $customerUser): $this->tokenFacade->createRefreshTokenAsString($customerUser, $deviceId), ); } + + /** + * @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser + * @param string $loginType + * @param bool $isRegistration + * @param array $productListsUuids + * @param bool $shouldOverwriteCustomerUserCart + * @param string|null $cartUuid + * @param string|null $externalId + * @return \Shopsys\FrontendApiBundle\Model\Security\LoginResultData + */ + public function runLoginSteps( + CustomerUser $customerUser, + string $loginType, + bool $isRegistration, + array $productListsUuids, + bool $shouldOverwriteCustomerUserCart, + ?string $cartUuid, + ?string $externalId, + ): LoginResultData { + if ($cartUuid !== null) { + if ($shouldOverwriteCustomerUserCart) { + $this->mergeCartFacade->overwriteCustomerCartWithCartByUuid($cartUuid, $customerUser); + } else { + $this->mergeCartFacade->mergeCartByUuidToCustomerCart($cartUuid, $customerUser); + } + } + + if (count($productListsUuids) > 0) { + $this->productListFacade->mergeProductListsToCustomerUser($productListsUuids, $customerUser); + } + + $this->customerUserLoginTypeFacade->updateCustomerUserLoginTypes( + $this->customerUserLoginTypeDataFactory->create($customerUser, $loginType, $externalId), + ); + + return $this->loginResultDataFactory->create( + $this->loginAndReturnAccessAndRefreshToken($customerUser), + $this->mergeCartFacade->shouldShowCartMergeInfo(), + $isRegistration, + ); + } } diff --git a/src/Model/SocialNetwork/SocialNetworkFacade.php b/src/Model/SocialNetwork/SocialNetworkFacade.php index 96b73bc5a..0ffbef2b4 100644 --- a/src/Model/SocialNetwork/SocialNetworkFacade.php +++ b/src/Model/SocialNetwork/SocialNetworkFacade.php @@ -92,40 +92,20 @@ public function login(string $type, string $redirectUrl, SessionInterface $sessi } $adapter->disconnect(); - $cartUuid = $session->get(SocialNetworkController::CART_UUID); - $shouldOverwriteCustomerUserCart = $session->get(SocialNetworkController::SHOULD_OVERWRITE_CART); - - $showCartMergeInfo = false; - - if ($cartUuid !== null) { - if ($shouldOverwriteCustomerUserCart) { - $this->mergeCartFacade->overwriteCustomerCartWithCartByUuid($cartUuid, $customerUser); - } else { - $this->mergeCartFacade->mergeCartByUuidToCustomerCart($cartUuid, $customerUser); - $showCartMergeInfo = true; - } - } - - $productListsUuids = $session->get(SocialNetworkController::PRODUCT_LIST_UUIDS); - - if ($productListsUuids !== null) { - $this->productListFacade->mergeProductListsToCustomerUser(explode(',', $productListsUuids), $customerUser); - } + $loginResultData = $this->loginAsUserFacade->runLoginSteps( + $customerUser, + $type, + $isRegistration, + explode(',', $session->get(SocialNetworkController::PRODUCT_LIST_UUIDS)), + $session->get(SocialNetworkController::SHOULD_OVERWRITE_CART), + $session->get(SocialNetworkController::CART_UUID), + (string)$userProfile->identifier, + ); $session->remove(SocialNetworkController::CART_UUID); $session->remove(SocialNetworkController::SHOULD_OVERWRITE_CART); $session->remove(SocialNetworkController::PRODUCT_LIST_UUIDS); - $loginResultData = $this->loginResultDataFactory->create( - $this->loginAsUserFacade->loginAndReturnAccessAndRefreshToken($customerUser), - $showCartMergeInfo, - $isRegistration, - ); - - $this->customerUserLoginTypeFacade->updateCustomerUserLoginTypes( - $this->customerUserLoginTypeDataFactory->create($customerUser, $type, (string)$userProfile->identifier), - ); - return $loginResultData; } catch (InvalidArgumentException | UnexpectedValueException $exception) { $message = sprintf('Login via %s doesn\'t work', $type);