Skip to content

Commit 8bbebf3

Browse files
committed
Avoid executing original execute method when no permission to do so
1 parent 54ae63a commit 8bbebf3

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

app/code/Magento/Customer/Controller/Plugin/Account.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
namespace Magento\Customer\Controller\Plugin;
99

10+
use Closure;
1011
use Magento\Customer\Controller\AccountInterface;
1112
use Magento\Customer\Model\Session;
13+
use Magento\Framework\App\ActionFlag;
1214
use Magento\Framework\App\ActionInterface;
1315
use Magento\Framework\App\RequestInterface;
1416
use Magento\Framework\App\ResponseInterface;
@@ -33,53 +35,61 @@ class Account
3335
* @var array
3436
*/
3537
private $allowedActions = [];
38+
/**
39+
* @var ActionFlag
40+
*/
41+
private $actionFlag;
3642

3743
/**
3844
* @param RequestInterface $request
3945
* @param Session $customerSession
46+
* @param ActionFlag $actionFlag
4047
* @param array $allowedActions List of actions that are allowed for not authorized users
4148
*/
4249
public function __construct(
4350
RequestInterface $request,
4451
Session $customerSession,
52+
ActionFlag $actionFlag,
4553
array $allowedActions = []
4654
) {
4755
$this->session = $customerSession;
4856
$this->allowedActions = $allowedActions;
4957
$this->request = $request;
58+
$this->actionFlag = $actionFlag;
5059
}
5160

5261
/**
53-
* Dispatch actions allowed for not authorized users
62+
* Executes original method if allowed, otherwise - redirects to log in
5463
*
55-
* @param AccountInterface $subject
56-
* @return void
64+
* @param AccountInterface $controllerAction
65+
* @param Closure $proceed
66+
* @return ResultInterface|ResponseInterface|void
5767
*/
58-
public function beforeExecute(AccountInterface $subject)
68+
public function aroundExecute(AccountInterface $controllerAction, Closure $proceed)
5969
{
60-
$action = strtolower($this->request->getActionName());
61-
$pattern = '/^(' . implode('|', $this->allowedActions) . ')$/i';
62-
63-
if (!preg_match($pattern, $action)) {
64-
if (!$this->session->authenticate()) {
65-
$subject->getActionFlag()->set('', ActionInterface::FLAG_NO_DISPATCH, true);
66-
}
67-
} else {
70+
if ($this->isActionAllowed()) {
6871
$this->session->setNoReferer(true);
72+
$response = $proceed();
73+
$this->session->unsNoReferer(false);
74+
75+
return $response;
76+
}
77+
78+
if (!$this->session->authenticate()) {
79+
$this->actionFlag->set('', ActionInterface::FLAG_NO_DISPATCH, true);
6980
}
7081
}
7182

7283
/**
73-
* Remove No-referer flag from customer session
84+
* Validates whether currently requested action is one of the allowed
7485
*
75-
* @param AccountInterface $subject
76-
* @param ResponseInterface|ResultInterface $result
77-
* @return ResponseInterface|ResultInterface
78-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
86+
* @return bool
7987
*/
80-
public function afterExecute(AccountInterface $subject, $result)
88+
private function isActionAllowed(): bool
8189
{
82-
$this->session->unsNoReferer(false);
83-
return $result;
90+
$action = strtolower($this->request->getActionName());
91+
$pattern = '/^(' . implode('|', $this->allowedActions) . ')$/i';
92+
93+
return (bool)preg_match($pattern, $action);
8494
}
8595
}

0 commit comments

Comments
 (0)