Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…er/issues/29

magento2-login-as-customer/issues/29: Destroy impersonated customer sessions on admin logout.
  • Loading branch information
naydav authored Apr 30, 2020
2 parents 740cfd5 + 6d955c8 commit adb8f4d
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 100 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,15 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(): void
public function execute(int $userId): void
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('login_as_customer');

$timePoint = date(
'Y-m-d H:i:s',
$this->dateTime->gmtTimestamp() - $this->config->getAuthenticationDataExpirationTime()
);

$connection->delete(
$tableName,
[
'created_at < ?' => $timePoint
'admin_id = ?' => $userId
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public function execute(string $secretKey): AuthenticationDataInterface
/** @var AuthenticationDataInterface $authenticationData */
$authenticationData = $this->authenticationDataFactory->create(
[
'customerId' => (int)$data['admin_id'],
'adminId' => (int)$data['customer_id'],
'customerId' => (int)$data['customer_id'],
'adminId' => (int)$data['admin_id'],
'extensionAttributes' => null,
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace Magento\LoginAsCustomer\Model\ResourceModel;

use Magento\Framework\App\ResourceConnection;
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataBySecretInterface;
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface;

/**
* @inheritdoc
*/
class DeleteAuthenticationDataBySecret implements DeleteAuthenticationDataBySecretInterface
class IsLoginAsCustomerSessionActive implements IsLoginAsCustomerSessionActiveInterface
{
/**
* @var ResourceConnection
Expand All @@ -32,16 +32,18 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(string $secret): void
public function execute(int $customerId, int $userId): bool
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('login_as_customer');
$connection = $this->resourceConnection->getConnection();

$query = $connection->select()
->from($tableName)
->where('customer_id = ?', $customerId)
->where('admin_id = ?', $userId);

$result = $connection->fetchRow($query);

$connection->delete(
$tableName,
[
'secret = ?' => $secret
]
);
return false !== $result;
}
}
16 changes: 0 additions & 16 deletions app/code/Magento/LoginAsCustomer/etc/crontab.xml

This file was deleted.

1 change: 1 addition & 0 deletions app/code/Magento/LoginAsCustomer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<preference for="Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataBySecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteAuthenticationDataBySecret"/>
<preference for="Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteExpiredAuthenticationData"/>
<preference for="Magento\LoginAsCustomerApi\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config"/>
<preference for="Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\IsLoginAsCustomerSessionActive"/>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
interface DeleteExpiredAuthenticationDataInterface
{
/**
* Delete expired authentication data
* Delete expired authentication data by user id.
*
* @param int $userId
* @return void
*/
public function execute(): void;
public function execute(int $userId): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomerApi\Api;

/**
* Check if Login as Customer session is still active.
*
* @api
*/
interface IsLoginAsCustomerSessionActiveInterface
{
/**
* Check if Login as Customer session is still active.
*
* @param int $customerId
* @param int $userId
* @return bool
*/
public function execute(int $customerId, int $userId): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Magento\User\Model\UserFactory;

/**
* Add comment after order placed by admin using login-as-customer.
* Add comment after order placed by admin using Login as Customer.
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
Expand Down Expand Up @@ -41,7 +41,7 @@ public function __construct(
}

/**
* Add comment after order placed by admin using login-as-customer.
* Add comment after order placed by admin using Login as Customer.
*
* @param Order $subject
* @param Order $result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface;
use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory;
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;
use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface;
use Magento\Store\Model\StoreManagerInterface;

Expand All @@ -30,6 +31,8 @@
* Generate secret key and forward to the storefront action
*
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
{
Expand Down Expand Up @@ -70,6 +73,11 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
*/
private $saveAuthenticationData;

/**
* @var DeleteExpiredAuthenticationDataInterface
*/
private $deleteExpiredAuthenticationData;

/**
* @var Url
*/
Expand All @@ -83,6 +91,7 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
* @param ConfigInterface $config
* @param AuthenticationDataInterfaceFactory $authenticationDataFactory
* @param SaveAuthenticationDataInterface $saveAuthenticationData ,
* @param DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
* @param Url $url
*/
public function __construct(
Expand All @@ -93,6 +102,7 @@ public function __construct(
ConfigInterface $config,
AuthenticationDataInterfaceFactory $authenticationDataFactory,
SaveAuthenticationDataInterface $saveAuthenticationData,
DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData,
Url $url
) {
parent::__construct($context);
Expand All @@ -103,6 +113,7 @@ public function __construct(
$this->config = $config;
$this->authenticationDataFactory = $authenticationDataFactory;
$this->saveAuthenticationData = $saveAuthenticationData;
$this->deleteExpiredAuthenticationData = $deleteExpiredAuthenticationData;
$this->url = $url;
}

Expand Down Expand Up @@ -142,15 +153,18 @@ public function execute(): ResultInterface
}

$adminUser = $this->authSession->getUser();
$userId = (int)$adminUser->getId();

/** @var AuthenticationDataInterface $authenticationData */
$authenticationData = $this->authenticationDataFactory->create(
[
'customerId' => $customerId,
'adminId' => (int)$adminUser->getId(),
'adminId' => $userId,
'extensionAttributes' => null,
]
);

$this->deleteExpiredAuthenticationData->execute($userId);
$secret = $this->saveAuthenticationData->execute($authenticationData);

$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);
Expand Down
11 changes: 0 additions & 11 deletions app/code/Magento/LoginAsCustomerUi/Controller/Login/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Magento\Framework\Message\ManagerInterface;
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerInterface;
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataBySecretInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -51,11 +50,6 @@ class Index implements HttpGetActionInterface
*/
private $authenticateCustomer;

/**
* @var DeleteAuthenticationDataBySecretInterface
*/
private $deleteAuthenticationDataBySecret;

/**
* @var ManagerInterface
*/
Expand All @@ -72,7 +66,6 @@ class Index implements HttpGetActionInterface
* @param CustomerRepositoryInterface $customerRepository
* @param GetAuthenticationDataBySecretInterface $getAuthenticateDataProcessor
* @param AuthenticateCustomerInterface $authenticateCustomerProcessor
* @param DeleteAuthenticationDataBySecretInterface $deleteSecretProcessor
* @param ManagerInterface $messageManager
* @param LoggerInterface $logger
*/
Expand All @@ -82,7 +75,6 @@ public function __construct(
CustomerRepositoryInterface $customerRepository,
GetAuthenticationDataBySecretInterface $getAuthenticateDataProcessor,
AuthenticateCustomerInterface $authenticateCustomerProcessor,
DeleteAuthenticationDataBySecretInterface $deleteSecretProcessor,
ManagerInterface $messageManager,
LoggerInterface $logger
) {
Expand All @@ -91,7 +83,6 @@ public function __construct(
$this->customerRepository = $customerRepository;
$this->getAuthenticationDataBySecret = $getAuthenticateDataProcessor;
$this->authenticateCustomer = $authenticateCustomerProcessor;
$this->deleteAuthenticationDataBySecret = $deleteSecretProcessor;
$this->messageManager = $messageManager;
$this->logger = $logger;
}
Expand All @@ -114,8 +105,6 @@ public function execute(): ResultInterface

$authenticateData = $this->getAuthenticationDataBySecret->execute($secret);

$this->deleteAuthenticationDataBySecret->execute($secret);

try {
$customer = $this->customerRepository->getById($authenticateData->getCustomerId());
} catch (NoSuchEntityException $e) {
Expand Down
53 changes: 53 additions & 0 deletions app/code/Magento/LoginAsCustomerUi/Plugin/AdminLogoutPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomerUi\Plugin;

use Magento\Backend\Model\Auth;
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
use Magento\LoginAsCustomerApi\Api\DeleteExpiredAuthenticationDataInterface;

/**
* Delete all Login as Customer sessions for logging out admin.
*/
class AdminLogoutPlugin
{
/**
* @var ConfigInterface
*/
private $config;

/**
* @var DeleteExpiredAuthenticationDataInterface
*/
private $deleteExpiredAuthenticationData;

/**
* @param ConfigInterface $config
* @param DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
*/
public function __construct(
ConfigInterface $config,
DeleteExpiredAuthenticationDataInterface $deleteExpiredAuthenticationData
) {
$this->config = $config;
$this->deleteExpiredAuthenticationData = $deleteExpiredAuthenticationData;
}

/**
* Delete all Login as Customer sessions for logging out admin.
*
* @param Auth $subject
*/
public function beforeLogout(Auth $subject): void
{
if ($this->config->isEnabled()) {
$userId = (int)$subject->getUser()->getId();
$this->deleteExpiredAuthenticationData->execute($userId);
}
}
}
Loading

0 comments on commit adb8f4d

Please sign in to comment.