Skip to content

Commit

Permalink
Merge pull request #98 from ihorvansach/refactor-code-microservices
Browse files Browse the repository at this point in the history
Refactor LoginAsCustomer Login Model
  • Loading branch information
naydav authored Apr 27, 2020
2 parents 143168f + bcf441f commit c1dee1f
Show file tree
Hide file tree
Showing 21 changed files with 617 additions and 303 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Api;

/**
* @api
*/
interface AuthenticateCustomerInterface
{
/**
* Authenticate a customer by customer ID
*
* @return bool
* @param int $customerId
* @param int $adminId
*/
public function execute(int $customerId, int $adminId):bool;
}
22 changes: 22 additions & 0 deletions app/code/Magento/LoginAsCustomer/Api/CreateSecretInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Api;

/**
* @api
*/
interface CreateSecretInterface
{
/**
* Create a new secret key
* @return string
* @param int $customerId
* @param int $adminId
*/
public function execute(int $customerId, int $adminId):string;
}
19 changes: 19 additions & 0 deletions app/code/Magento/LoginAsCustomer/Api/DeleteOldSecretsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Api;

/**
* @api
*/
interface DeleteOldSecretsInterface
{
/**
* Delete old secret key records
*/
public function execute():void;
}
19 changes: 19 additions & 0 deletions app/code/Magento/LoginAsCustomer/Api/DeleteSecretInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Api;

/**
* @api
*/
interface DeleteSecretInterface
{
/**
* Delete secret key
*/
public function execute(string $secretKey):void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Api;

/**
* @api
*/
interface GetAuthenticateDataInterface
{
/**
* Load login details based on secret key
*/
public function execute(string $secretKey):array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;
use Magento\LoginAsCustomer\Model\Login;

/**
* Login As Customer log grid action
Expand All @@ -29,23 +27,6 @@ class Index extends Action implements HttpGetActionInterface, HttpPostActionInte
*/
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';

/**
* @var Login
*/
private $loginModel;

/**
* @param Context $context
* @param Login $loginModel
*/
public function __construct(
Context $context,
Login $loginModel
) {
parent::__construct($context);
$this->loginModel = $loginModel;
}

/**
* Login As Customer log grid action
*
Expand All @@ -59,8 +40,6 @@ public function execute():ResultInterface
return $resultForward;
}

$this->loginModel->deleteNotUsed();

/** @var Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_log')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\LoginAsCustomer\Api\CreateSecretInterface;

/**
* Login as customer action
Expand All @@ -27,11 +30,6 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
*/
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button';

/**
* @var \Magento\LoginAsCustomer\Model\Login
*/
private $loginModel;

/**
* @var \Magento\Backend\Model\Auth\Session
*/
Expand All @@ -47,34 +45,47 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
*/
private $url;

/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;

/**
* @var \Magento\LoginAsCustomer\Model\Config
*/
private $config;

/**
* @var CreateSecretInterface
*/
private $createSecretProcessor;

/**
* Login constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\LoginAsCustomer\Model\Login $loginModel
* @param \Magento\Backend\Model\Auth\Session $authSession
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Url $url
* @param \Magento\LoginAsCustomer\Model\Config $config
* @param CustomerRepositoryInterface $customerRepository
* @param \Magento\LoginAsCustomer\Model\Config $config,
* @param CreateSecretInterface $createSecretProcessor
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\LoginAsCustomer\Model\Login $loginModel,
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Url $url,
\Magento\LoginAsCustomer\Model\Config $config
CustomerRepositoryInterface $customerRepository,
\Magento\LoginAsCustomer\Model\Config $config,
CreateSecretInterface $createSecretProcessor
) {
parent::__construct($context);
$this->loginModel = $loginModel;
$this->authSession = $authSession;
$this->storeManager = $storeManager;
$this->url = $url;
$this->customerRepository = $customerRepository;
$this->config = $config;
$this->createSecretProcessor = $createSecretProcessor;
}

/**
Expand All @@ -84,48 +95,45 @@ public function __construct(
*/
public function execute(): ResultInterface
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();

if (!$this->config->isEnabled()) {
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
return $resultRedirect->setPath('customer/index/index');
}

$request = $this->getRequest();

$customerId = (int) $request->getParam('customer_id');
if (!$customerId) {
$customerId = (int) $request->getParam('entity_id');
}

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();

if (!$this->config->isEnabled()) {
$this->messageManager->addErrorMessage(__('Login As Customer is disabled.'));
try {
$customer = $this->customerRepository->getById($customerId);
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
return $resultRedirect->setPath('customer/index/index');
}

$customerStoreId = $request->getParam('store_id');

if (!isset($customerStoreId) && $this->config->isManualChoiceEnabled()) {
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
return $resultRedirect->setPath('loginascustomer/login/manual', ['entity_id' => $customerId ]);
}

$login = $this->loginModel->setCustomerId($customerId);

$login->deleteNotUsed();

$customer = $login->getCustomer();

if (!$customer->getId()) {
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
return $resultRedirect->setPath('customer/index/index');
}

$user = $this->authSession->getUser();
$login->generate($user->getId());
$store = $this->storeManager->getStore();
$secret = $this->createSecretProcessor->execute($customerId, (int)$user->getId());

$store = $this->storeManager->getStore();
if (null === $store) {
$store = $this->storeManager->getDefaultStoreView();
}

$redirectUrl = $this->url->setScope($store)
->getUrl('loginascustomer/login/index', ['secret' => $login->getSecret(), '_nosid' => true]);
->getUrl('loginascustomer/login/index', ['secret' => $secret, '_nosid' => true]);

return $resultRedirect->setUrl($redirectUrl);
}
Expand Down
Loading

0 comments on commit c1dee1f

Please sign in to comment.