Skip to content

Commit

Permalink
refactored login after steps to remove dry and to easeup future imple…
Browse files Browse the repository at this point in the history
…mentation of different logins
  • Loading branch information
TomasLudvik committed Dec 19, 2024
1 parent f0c5ef2 commit 512f327
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 57 deletions.
42 changes: 14 additions & 28 deletions src/Model/Mutation/Login/LoginMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -51,6 +52,7 @@ public function __construct(
protected readonly LoginResultDataFactory $loginResultDataFactory,
protected readonly CustomerUserLoginTypeFacade $customerUserLoginTypeFacade,
protected readonly CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory,
protected readonly LoginAsUserFacade $loginAsUserFacade,
) {
}

Expand All @@ -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,
);
}
}
50 changes: 50 additions & 0 deletions src/Model/Security/LoginAsUserFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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,
);
}
}
38 changes: 9 additions & 29 deletions src/Model/SocialNetwork/SocialNetworkFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 512f327

Please sign in to comment.