3
3
namespace yii2mod \rbac \filters ;
4
4
5
5
use Yii ;
6
- use yii \base \InlineAction ;
6
+ use yii \base \Action ;
7
+ use yii \base \Module ;
8
+ use yii \helpers \ArrayHelper ;
9
+ use yii \helpers \Url ;
7
10
8
11
/**
9
12
* Class AccessControl
@@ -17,25 +20,24 @@ class AccessControl extends \yii\filters\AccessControl
17
20
public $ params = [];
18
21
19
22
/**
20
- * This method is invoked right before an action is to be executed (after all possible filters.)
21
- *
22
- * @param InlineAction $action the action to be executed.
23
- * @return boolean whether the action should continue to be executed.
23
+ * @var array list of action that not need to check access.
24
+ */
25
+ public $ allowActions = [];
26
+
27
+ /**
28
+ * @inheritdoc
24
29
*/
25
30
public function beforeAction ($ action )
26
31
{
27
- $ actionId = $ action ->getUniqueId ();
28
- $ user = Yii::$ app ->getUser ();
29
- $ params = isset ($ this ->params [$ action ->id ]) ? $ this ->params [$ action ->id ] : [];
32
+ $ controller = $ action ->controller ;
33
+ $ params = ArrayHelper::getValue ($ this ->params , $ action ->id , []);
30
34
31
- if ($ user ->can ('/ ' . $ actionId , $ params )) {
35
+ if (Yii:: $ app -> user ->can ('/ ' . $ action -> getUniqueId () , $ params )) {
32
36
return true ;
33
37
}
34
38
35
- $ controller = $ action ->controller ;
36
-
37
39
do {
38
- if ($ user ->can ('/ ' . ltrim ($ controller ->getUniqueId () . '/* ' , '/ ' ))) {
40
+ if (Yii:: $ app -> user ->can ('/ ' . ltrim ($ controller ->getUniqueId () . '/* ' , '/ ' ))) {
39
41
return true ;
40
42
}
41
43
$ controller = $ controller ->module ;
@@ -44,23 +46,81 @@ public function beforeAction($action)
44
46
return parent ::beforeAction ($ action );
45
47
}
46
48
47
-
48
49
/**
49
- * Returns a value indicating whether the filer is active for the given action.
50
- *
51
- * @param InlineAction $action the action being filtered
52
- * @return boolean whether the filer is active for the given action.
50
+ * @inheritdoc
53
51
*/
54
52
protected function isActive ($ action )
55
53
{
56
- $ uniqueId = $ action ->getUniqueId ();
57
-
58
- if ($ uniqueId === Yii::$ app ->getErrorHandler ()->errorAction ) {
59
- return false ;
60
- } else if (Yii::$ app ->user ->isGuest && Yii::$ app ->user ->loginUrl == $ uniqueId ) {
54
+ if ($ this ->isErrorPage ($ action ) || $ this ->isLoginPage ($ action ) || $ this ->isAllowedAction ($ action )) {
61
55
return false ;
62
56
}
63
57
64
58
return parent ::isActive ($ action );
65
59
}
60
+
61
+ /**
62
+ * Returns a value indicating whether a current url equals `errorAction` property of the ErrorHandler component
63
+ *
64
+ * @param Action $action
65
+ * @return bool
66
+ */
67
+ private function isErrorPage ($ action )
68
+ {
69
+ if ($ action ->getUniqueId () === Yii::$ app ->getErrorHandler ()->errorAction ) {
70
+ return true ;
71
+ }
72
+
73
+ return false ;
74
+ }
75
+
76
+ /**
77
+ * Returns a value indicating whether a current url equals `loginUrl` property of the User component
78
+ *
79
+ * @param Action $action
80
+ * @return bool
81
+ */
82
+ private function isLoginPage ($ action )
83
+ {
84
+ $ loginUrl = trim (Url::to (Yii::$ app ->user ->loginUrl ), '/ ' );
85
+
86
+ if (Yii::$ app ->user ->isGuest && $ action ->getUniqueId () === $ loginUrl ) {
87
+ return true ;
88
+ }
89
+
90
+ return false ;
91
+ }
92
+
93
+ /**
94
+ * Returns a value indicating whether a current url exists in the `allowActions` list.
95
+ *
96
+ * @param Action $action
97
+ * @return bool
98
+ */
99
+ private function isAllowedAction ($ action )
100
+ {
101
+ if ($ this ->owner instanceof Module) {
102
+ $ ownerId = $ this ->owner ->getUniqueId ();
103
+ $ id = $ action ->getUniqueId ();
104
+ if (!empty ($ ownerId ) && strpos ($ id , $ ownerId . '/ ' ) === 0 ) {
105
+ $ id = substr ($ id , strlen ($ ownerId ) + 1 );
106
+ }
107
+ } else {
108
+ $ id = $ action ->id ;
109
+ }
110
+
111
+ foreach ($ this ->allowActions as $ route ) {
112
+ if (substr ($ route , -1 ) === '* ' ) {
113
+ $ route = rtrim ($ route , "* " );
114
+ if ($ route === '' || strpos ($ id , $ route ) === 0 ) {
115
+ return true ;
116
+ }
117
+ } else {
118
+ if ($ id === $ route ) {
119
+ return true ;
120
+ }
121
+ }
122
+ }
123
+
124
+ return false ;
125
+ }
66
126
}
0 commit comments