Skip to content

Commit

Permalink
Merge pull request #5961 from magento-engcom/magento2-login-as-custom…
Browse files Browse the repository at this point in the history
…er#144

"Login as Customer" functionality should be enabled by default
  • Loading branch information
naydav authored Aug 5, 2020
2 parents d76acc2 + e099619 commit e12a2fd
Show file tree
Hide file tree
Showing 35 changed files with 898 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
namespace Magento\LoginAsCustomer\Model;

use Magento\Customer\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface;
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface;

/**
* @inheritdoc
Expand All @@ -29,16 +31,25 @@ class AuthenticateCustomerBySecret implements AuthenticateCustomerBySecretInterf
*/
private $customerSession;

/**
* @var SetLoggedAsCustomerAdminIdInterface
*/
private $setLoggedAsCustomerAdminId;

/**
* @param GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret
* @param Session $customerSession
* @param SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId
*/
public function __construct(
GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret,
Session $customerSession
Session $customerSession,
?SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId = null
) {
$this->getAuthenticationDataBySecret = $getAuthenticationDataBySecret;
$this->customerSession = $customerSession;
$this->setLoggedAsCustomerAdminId = $setLoggedAsCustomerAdminId
?? ObjectManager::getInstance()->get(SetLoggedAsCustomerAdminIdInterface::class);
}

/**
Expand All @@ -58,6 +69,6 @@ public function execute(string $secret): void
}

$this->customerSession->regenerateId();
$this->customerSession->setLoggedAsCustomerAdmindId($authenticationData->getAdminId());
$this->setLoggedAsCustomerAdminId->execute($authenticationData->getAdminId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Model;

use Magento\Customer\Model\Session;
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface;

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class GetLoggedAsCustomerAdminId implements GetLoggedAsCustomerAdminIdInterface
{
/**
* @var Session
*/
private $session;

/**
* @param Session $session
*/
public function __construct(Session $session)
{
$this->session = $session;
}

/**
* @inheritdoc
*/
public function execute(): int
{
return (int)$this->session->getLoggedAsCustomerAdmindId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Model;

use Magento\Backend\Model\Auth\Session;
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerCustomerIdInterface;

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class GetLoggedAsCustomerCustomerId implements GetLoggedAsCustomerCustomerIdInterface
{
/**
* @var Session
*/
private $session;

/**
* @param Session $session
*/
public function __construct(Session $session)
{
$this->session = $session;
}

/**
* @inheritdoc
*/
public function execute(): int
{
return (int)$this->session->getLoggedAsCustomerCustomerId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Model;

use Magento\LoginAsCustomerApi\Api\ConfigInterface;
use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;
use Magento\LoginAsCustomerApi\Model\IsLoginAsCustomerEnabledForCustomerResolverInterface;

/**
* @inheritdoc
*/
class IsLoginAsCustomerEnabledForCustomerChain implements IsLoginAsCustomerEnabledForCustomerInterface
{
/**
* @var ConfigInterface
*/
private $config;

/**
* @var IsLoginAsCustomerEnabledForCustomerResultFactory
*/
private $resultFactory;

/**
* @var IsLoginAsCustomerEnabledForCustomerResultInterface[]
*/
private $resolvers;

/**
* @param ConfigInterface $config
* @param IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory
* @param array $resolvers
*/
public function __construct(
ConfigInterface $config,
IsLoginAsCustomerEnabledForCustomerResultFactory $resultFactory,
array $resolvers = []
) {
$this->config = $config;
$this->resultFactory = $resultFactory;
$this->resolvers = $resolvers;
}

/**
* @inheritdoc
*/
public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface
{
$messages = [[]];
/** @var IsLoginAsCustomerEnabledForCustomerResultInterface $resolver */
foreach ($this->resolvers as $resolver) {
$resolverResult = $resolver->execute($customerId);
if (!$resolverResult->isEnabled()) {
$messages[] = $resolverResult->getMessages();
}
}

return $this->resultFactory->create(['messages' => array_merge(...$messages)]);
}
}
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\LoginAsCustomer\Model;

use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;

/**
* @inheritdoc
*/
class IsLoginAsCustomerEnabledForCustomerResult implements IsLoginAsCustomerEnabledForCustomerResultInterface
{
/**
* @var string[]
*/
private $messages;

/**
* @param array $messages
*/
public function __construct(array $messages = [])
{
$this->messages = $messages;
}

/**
* @inheritdoc
*/
public function isEnabled(): bool
{
return empty($this->messages);
}

/**
* @inheritdoc
*/
public function getMessages(): array
{
return $this->messages;
}

/**
* @inheritdoc
*/
public function setMessages(array $messages): void
{
$this->messages = $messages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Model\Resolver;

use Magento\LoginAsCustomer\Model\IsLoginAsCustomerEnabledForCustomerResultFactory;
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
use Magento\LoginAsCustomerApi\Api\Data\IsLoginAsCustomerEnabledForCustomerResultInterface;
use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface;

/**
* @inheritdoc
*/
class IsLoginAsCustomerEnabledResolver implements IsLoginAsCustomerEnabledForCustomerInterface
{
/**
* @var ConfigInterface
*/
private $config;

/**
* @var IsLoginAsCustomerEnabledForCustomerResultFactory
*/
private $resultFactory;

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

/**
* @inheritdoc
*/
public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface
{
$messages = [];
if (!$this->config->isEnabled()) {
$messages[] = __('Login as Customer is disabled.');
}

return $this->resultFactory->create(['messages' => $messages]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Model;

use Magento\Customer\Model\Session;
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface;

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class SetLoggedAsCustomerAdminId implements SetLoggedAsCustomerAdminIdInterface
{
/**
* @var Session
*/
private $session;

/**
* @param Session $session
*/
public function __construct(Session $session)
{
$this->session = $session;
}

/**
* @inheritdoc
*/
public function execute(int $adminId): void
{
$this->session->setLoggedAsCustomerAdmindId($adminId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Model;

use Magento\Backend\Model\Auth\Session;
use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface;

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class SetLoggedAsCustomerCustomerId implements SetLoggedAsCustomerCustomerIdInterface
{
/**
* @var Session
*/
private $session;

/**
* @param Session $session
*/
public function __construct(Session $session)
{
$this->session = $session;
}

/**
* @inheritdoc
*/
public function execute(int $customerId): void
{
$this->session->setLoggedAsCustomerCustomerId($customerId);
}
}
Loading

0 comments on commit e12a2fd

Please sign in to comment.