Skip to content

Commit

Permalink
Allow Magento admin to log in as customer
Browse files Browse the repository at this point in the history
  • Loading branch information
shochdoerfer committed May 12, 2022
1 parent 34dd010 commit 8b71fb0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 32 deletions.
7 changes: 7 additions & 0 deletions Controller/LoginCheck.php
Expand Up @@ -134,6 +134,13 @@ public function execute()
return false;
}

// allow "Login as customer" via Magento Admin
$sessionData = $this->session->getData();
$afterLoginReferer = $sessionData['after_login_referer'] ?? '';
if (strpos($afterLoginReferer, 'loginascustomer') !== false) {
return false;
}

$url = $this->url->getCurrentUrl();
$urlParts = \parse_url($url);
$path = is_array($urlParts) && isset($urlParts['path']) ? $urlParts['path'] : '';
Expand Down
101 changes: 69 additions & 32 deletions Test/Unit/Controller/LoginCheckUnitTest.php
Expand Up @@ -104,7 +104,8 @@ private function getSession()
return $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->setMethods([
'setAfterLoginReferer'
'setAfterLoginReferer',
'getData'
])
->getMock();
}
Expand Down Expand Up @@ -197,37 +198,6 @@ private function getRequestObject()
return $this->createMock(RequestHttp::class);
}

/**
* Run test with url equals target, so no redirecting is happening.
*
* @test
* @depends testConstructor
*/
public function skipMatchingWhenModuleIsDisabled()
{
$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(false);

$context = $this->getContext();

$loginCheck = new LoginCheck(
$context,
$this->getCustomerSession(),
$this->getSession(),
$this->getStoreManager(),
$this->getScopeConfig(),
$this->getWhitelistRepository(),
$this->getStrategyManager(),
$moduleCheck,
$this->getResponseHttp(),
$this->getPasswordResetHelper()
);

$loginCheck->execute();
}

/**
* @return MockObject|UrlInterface
*/
Expand Down Expand Up @@ -262,6 +232,37 @@ private function getPasswordResetHelper()
->getMock();
}

/**
* Run test with url equals target, so no redirecting is happening.
*
* @test
* @depends testConstructor
*/
public function skipMatchingWhenModuleIsDisabled()
{
$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(false);

$context = $this->getContext();

$loginCheck = new LoginCheck(
$context,
$this->getCustomerSession(),
$this->getSession(),
$this->getStoreManager(),
$this->getScopeConfig(),
$this->getWhitelistRepository(),
$this->getStrategyManager(),
$moduleCheck,
$this->getResponseHttp(),
$this->getPasswordResetHelper()
);

$loginCheck->execute();
}

/**
* Run test with existing customer session, so no redirecting is happening.
*
Expand Down Expand Up @@ -298,6 +299,42 @@ public function skipMatchingWhenCustomerSessionIsActive()
$loginCheck->execute();
}

/**
* Run test with a Magento Admin logging in as customer, so no redirecting is happening.
*
* @test
* @depends testConstructor
*/
public function skipMatchingWhenCustomerLoginViaMagentoAdminHappens()
{
$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(true);

$session = $this->getSession();
$session->expects($this->once())
->method('getData')
->willReturn(['after_login_referer' => 'loginascustomer']);

$context = $this->getContext();

$loginCheck = new LoginCheck(
$context,
$this->getCustomerSession(),
$session,
$this->getStoreManager(),
$this->getScopeConfig(),
$this->getWhitelistRepository(),
$this->getStrategyManager(),
$moduleCheck,
$this->getResponseHttp(),
$this->getPasswordResetHelper()
);

$loginCheck->execute();
}

/**
* Run test with url equals target, so no redirecting is happening.
*
Expand Down

0 comments on commit 8b71fb0

Please sign in to comment.