Skip to content

Commit

Permalink
bitExpert#35 Redirecting after logging in now respects logion option …
Browse files Browse the repository at this point in the history
…configuration and last visited page
  • Loading branch information
Florian Horn committed Mar 15, 2017
1 parent 162994c commit 5b9f82e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
22 changes: 22 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.0.0

### Added

- Changed type namings in DI to match best practice.
- Respect configured login option behavior.
- Definition of default target URI in DI.
- Added own session handler.

### Deprecated

- Nothing.

### Removed

- Removed full qualification of namespace represtantion type name to match best practice.

### Fixed

- Refactored code structure.
- #35 Redirection after logging in

## 1.3.1

### Added
Expand Down
15 changes: 11 additions & 4 deletions Controller/LoginCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use \bitExpert\ForceCustomerLogin\Model\ResourceModel\WhitelistEntry\Collection;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\App\Action\Context;
use \bitExpert\ForceCustomerLogin\Model\Session;
use \Magento\Framework\UrlInterface;
use \Magento\Framework\App\DeploymentConfig;
use \Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList;
Expand All @@ -29,6 +30,10 @@ class LoginCheck extends Action implements LoginCheckInterface
* @var UrlInterface
*/
protected $url;
/**
* @var Session
*/
protected $session;
/**
* @var DeploymentConfig
*/
Expand All @@ -46,16 +51,19 @@ class LoginCheck extends Action implements LoginCheckInterface
* Creates a new {@link \bitExpert\ForceCustomerLogin\Controller\LoginCheck}.
*
* @param Context $context
* @param Session $session
* @param DeploymentConfig $deploymentConfig
* @param WhitelistRepositoryInterface $whitelistRepository
* @param string $targetUrl
*/
public function __construct(
Context $context,
Session $session,
DeploymentConfig $deploymentConfig,
WhitelistRepositoryInterface $whitelistRepository,
$targetUrl
) {
$this->session = $session;
$this->deploymentConfig = $deploymentConfig;
$this->whitelistRepository = $whitelistRepository;
$this->targetUrl = $targetUrl;
Expand Down Expand Up @@ -85,10 +93,9 @@ public function execute()
}
}

$this->_redirect($this->targetUrl, [
'referer',
\base64_encode($url)
])->sendResponse();
$this->session->setAfterLoginReferer($path);

$this->_redirect($this->targetUrl)->sendResponse();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions Model/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace bitExpert\ForceCustomerLogin\Model;

/**
* Class Session
* @package bitExpert\ForceCustomerLogin\Model
*/
class Session extends \Magento\Framework\Session\SessionManager
{

}
41 changes: 24 additions & 17 deletions Plugin/AfterLoginPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

use \Magento\Customer\Controller\Account\LoginPost;
use \Magento\Framework\Controller\Result\Redirect;
use \Magento\Framework\App\Action\Context;
use \Magento\Framework\UrlInterface;
use \Magento\Customer\Model\Account\Redirect as AccountRedirect;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \bitExpert\ForceCustomerLogin\Model\Session;

/**
* Class AfterLoginPlugin
Expand All @@ -24,29 +22,34 @@
class AfterLoginPlugin
{
/**
* @var AccountRedirect
* Redirect behaviour
*/
protected $accountRedirect;
const REDIRECT_DASHBOARD_ENABLED = '1';

/**
* @var Session
*/
protected $session;
/**
* @var UrlInterface
* @var string
*/
protected $url;
protected $defaultTargetUrl;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* AfterLoginPlugin constructor.
* @param Context $context
* @param AccountRedirect $accountRedirect
* @param Session $session
* @param string $defaultTargetUrl
*/
public function __construct(
Context $context,
AccountRedirect $accountRedirect
Session $session,
$defaultTargetUrl
) {
$this->accountRedirect = $accountRedirect;
$this->url = $context->getUrl();
$this->session = $session;
$this->defaultTargetUrl = $defaultTargetUrl;
}

/**
Expand All @@ -57,14 +60,18 @@ public function __construct(
*/
public function afterExecute(LoginPost $customerAccountLoginController, $resultRedirect)
{
$currentUrl = $this->url->getCurrentUrl();

if ($this->getScopeConfig()->getValue('customer/startup/redirect_dashboard')) {
if (self::REDIRECT_DASHBOARD_ENABLED ===
$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard')) {
return $resultRedirect;
}

$targetUrl = $this->session->getAfterLoginReferer();
if (empty($targetUrl)) {
$targetUrl = $this->defaultTargetUrl;
}

/** @var $resultRedirect Redirect */
$resultRedirect->setUrl($currentUrl);
$resultRedirect->setUrl($targetUrl);

return $resultRedirect;
}
Expand Down
11 changes: 11 additions & 0 deletions Test/Unit/Controller/LoginCheckUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testConstructor()
{
$loginCheck = new \bitExpert\ForceCustomerLogin\Controller\LoginCheck(
$this->getContext(),
$this->getCustomerSession(),
$this->getDeploymentConfig(),
$this->getWhitelistRepository(),
''
Expand Down Expand Up @@ -174,6 +175,7 @@ public function testNoUrlMappingOnMatchingPathWithTargetUrl()

$loginCheck = new \bitExpert\ForceCustomerLogin\Controller\LoginCheck(
$context,
$this->getCustomerSession(),
$this->getDeploymentConfig(),
$this->getWhitelistRepository(),
$targetUrl
Expand Down Expand Up @@ -255,6 +257,7 @@ protected function runCase($urlString, $urlRule, $runMapping = false)

$loginCheck = new \bitExpert\ForceCustomerLogin\Controller\LoginCheck(
$context,
$this->getCustomerSession(),
$deploymentConfig,
$whitelistRepository,
$targetUrl
Expand All @@ -271,6 +274,14 @@ protected function getContext()
return $this->getMockBuilder('\Magento\Framework\App\Action\Context')->disableOriginalConstructor()->getMock();
}

/**
* @return \bitExpert\ForceCustomerLogin\Model\Session
*/
protected function getCustomerSession()
{
return $this->getMockBuilder('\bitExpert\ForceCustomerLogin\Model\Session')->disableOriginalConstructor()->getMock();
}

/**
* @return \Magento\Framework\UrlInterface
*/
Expand Down
5 changes: 5 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<type name="Magento\Customer\Controller\Account\LoginPost">
<plugin name="after_execute_login" type="bitExpert\ForceCustomerLogin\Plugin\AfterLoginPlugin" sortOrder="10" disabled="false"/>
</type>
<type name="bitExpert\ForceCustomerLogin\Plugin\AfterLoginPlugin">
<arguments>
<argument name="defaultTargetUrl" xsi:type="string">/</argument>
</arguments>
</type>

<!-- Entities -->
<preference for="bitExpert\ForceCustomerLogin\Api\Data\WhitelistEntryInterface"
Expand Down

0 comments on commit 5b9f82e

Please sign in to comment.