7
7
8
8
namespace Magento \Customer \Controller \Plugin ;
9
9
10
+ use Closure ;
10
11
use Magento \Customer \Controller \AccountInterface ;
11
12
use Magento \Customer \Model \Session ;
13
+ use Magento \Framework \App \ActionFlag ;
12
14
use Magento \Framework \App \ActionInterface ;
13
15
use Magento \Framework \App \RequestInterface ;
14
16
use Magento \Framework \App \ResponseInterface ;
@@ -33,53 +35,61 @@ class Account
33
35
* @var array
34
36
*/
35
37
private $ allowedActions = [];
38
+ /**
39
+ * @var ActionFlag
40
+ */
41
+ private $ actionFlag ;
36
42
37
43
/**
38
44
* @param RequestInterface $request
39
45
* @param Session $customerSession
46
+ * @param ActionFlag $actionFlag
40
47
* @param array $allowedActions List of actions that are allowed for not authorized users
41
48
*/
42
49
public function __construct (
43
50
RequestInterface $ request ,
44
51
Session $ customerSession ,
52
+ ActionFlag $ actionFlag ,
45
53
array $ allowedActions = []
46
54
) {
47
55
$ this ->session = $ customerSession ;
48
56
$ this ->allowedActions = $ allowedActions ;
49
57
$ this ->request = $ request ;
58
+ $ this ->actionFlag = $ actionFlag ;
50
59
}
51
60
52
61
/**
53
- * Dispatch actions allowed for not authorized users
62
+ * Executes original method if allowed, otherwise - redirects to log in
54
63
*
55
- * @param AccountInterface $subject
56
- * @return void
64
+ * @param AccountInterface $controllerAction
65
+ * @param Closure $proceed
66
+ * @return ResultInterface|ResponseInterface|void
57
67
*/
58
- public function beforeExecute (AccountInterface $ subject )
68
+ public function aroundExecute (AccountInterface $ controllerAction , Closure $ proceed )
59
69
{
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 ()) {
68
71
$ 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 );
69
80
}
70
81
}
71
82
72
83
/**
73
- * Remove No-referer flag from customer session
84
+ * Validates whether currently requested action is one of the allowed
74
85
*
75
- * @param AccountInterface $subject
76
- * @param ResponseInterface|ResultInterface $result
77
- * @return ResponseInterface|ResultInterface
78
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
86
+ * @return bool
79
87
*/
80
- public function afterExecute ( AccountInterface $ subject , $ result )
88
+ private function isActionAllowed (): bool
81
89
{
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 );
84
94
}
85
95
}
0 commit comments