@@ -20,6 +20,11 @@ class Route extends \yii\base\Object
20
20
{
21
21
const CACHE_TAG = 'mdm.admin.route ' ;
22
22
23
+ const PREFIX_ADVANCED = '@ ' ;
24
+ const PREFIX_BASIC = '/ ' ;
25
+
26
+ private $ _routePrefix ;
27
+
23
28
/**
24
29
* Assign or remove items
25
30
* @param array $routes
@@ -31,7 +36,7 @@ public function addNew($routes)
31
36
foreach ($ routes as $ route ) {
32
37
try {
33
38
$ r = explode ('& ' , $ route );
34
- $ item = $ manager ->createPermission (' / ' . trim ($ route, ' / ' ));
39
+ $ item = $ manager ->createPermission ($ this -> getPermissionName ($ route ));
35
40
if (count ($ r ) > 1 ) {
36
41
$ action = '/ ' . trim ($ r [0 ], '/ ' );
37
42
if (($ itemAction = $ manager ->getPermission ($ action )) === null ) {
@@ -67,7 +72,7 @@ public function remove($routes)
67
72
$ manager = Configs::authManager ();
68
73
foreach ($ routes as $ route ) {
69
74
try {
70
- $ item = $ manager ->createPermission (' / ' . trim ($ route, ' / ' ));
75
+ $ item = $ manager ->createPermission ($ this -> getPermissionName ($ route ));
71
76
$ manager ->remove ($ item );
72
77
} catch (Exception $ exc ) {
73
78
Yii::error ($ exc ->getMessage (), __METHOD__ );
@@ -76,17 +81,85 @@ public function remove($routes)
76
81
Helper::invalidate ();
77
82
}
78
83
84
+ /**
85
+ * Returns route prefix depending on the configuration.
86
+ * @return string Route prefix
87
+ */
88
+ public function getRoutePrefix ()
89
+ {
90
+ if (!$ this ->_routePrefix ) {
91
+ $ this ->_routePrefix = Configs::instance ()->advanced ? self ::PREFIX_ADVANCED : self ::PREFIX_BASIC ;
92
+ }
93
+ return $ this ->_routePrefix ;
94
+ }
95
+
96
+ /**
97
+ * Returns the correct permission name depending on the configuration.
98
+ * @param string $route Route
99
+ * @return string Permission name
100
+ */
101
+ public function getPermissionName ($ route )
102
+ {
103
+ if (self ::PREFIX_BASIC == $ this ->routePrefix ) {
104
+ return self ::PREFIX_BASIC . trim ($ route , self ::PREFIX_BASIC );
105
+ } else {
106
+ return self ::PREFIX_ADVANCED . ltrim (trim ($ route , self ::PREFIX_BASIC ), self ::PREFIX_ADVANCED );
107
+ }
108
+ }
109
+
79
110
/**
80
111
* Get available and assigned routes
81
112
* @return array
82
113
*/
83
114
public function getRoutes ()
84
115
{
85
116
$ manager = Configs::authManager ();
86
- $ routes = $ this ->getAppRoutes ();
117
+ // Get advanced configuration
118
+ $ advanced = Configs::instance ()->advanced ;
119
+ if ($ advanced ) {
120
+ // Use advanced route scheme.
121
+ // Set advanced route prefix.
122
+ $ this ->_routePrefix = self ::PREFIX_ADVANCED ;
123
+ // Create empty routes array.
124
+ $ routes = [];
125
+ // Save original app.
126
+ $ yiiApp = Yii::$ app ;
127
+ // Step through each configured application
128
+ foreach ($ advanced as $ id => $ configPaths ) {
129
+ // Force correct id string.
130
+ $ id = $ this ->routePrefix . ltrim (trim ($ id ), $ this ->routePrefix );
131
+ // Create empty config array.
132
+ $ config = [];
133
+ // Assemble configuration for current app.
134
+ foreach ($ configPaths as $ configPath ) {
135
+ // Merge every new configuration with the old config array.
136
+ $ config = yii \helpers \ArrayHelper::merge ($ config , require (Yii::getAlias ($ configPath )));
137
+ }
138
+ // Create new app using the config array.
139
+ unset($ config ['bootstrap ' ]);
140
+ $ app = new yii \web \Application ($ config );
141
+ // Get all the routes of the newly created app.
142
+ $ r = $ this ->getAppRoutes ($ app );
143
+ // Dump new app
144
+ unset($ app );
145
+ // Prepend the app id to all routes.
146
+ foreach ($ r as $ route ) {
147
+ $ routes [$ id . $ route ] = $ id . $ route ;
148
+ }
149
+ }
150
+ // Switch back to original app.
151
+ Yii::$ app = $ yiiApp ;
152
+ unset($ yiiApp );
153
+ } else {
154
+ // Use basic route scheme.
155
+ // Set basic route prefix
156
+ $ this ->_routePrefix = self ::PREFIX_BASIC ;
157
+ // Get basic app routes.
158
+ $ routes = $ this ->getAppRoutes ();
159
+ }
87
160
$ exists = [];
88
161
foreach (array_keys ($ manager ->getPermissions ()) as $ name ) {
89
- if ($ name [0 ] !== ' / ' ) {
162
+ if ($ name [0 ] !== $ this -> routePrefix ) {
90
163
continue ;
91
164
}
92
165
$ exists [] = $ name ;
@@ -109,7 +182,7 @@ public function getAppRoutes($module = null)
109
182
} elseif (is_string ($ module )) {
110
183
$ module = Yii::$ app ->getModule ($ module );
111
184
}
112
- $ key = [__METHOD__ , $ module ->getUniqueId ()];
185
+ $ key = [__METHOD__ , Yii:: $ app -> id , $ module ->getUniqueId ()];
113
186
$ cache = Configs::instance ()->cache ;
114
187
if ($ cache === null || ($ result = $ cache ->get ($ key )) === false ) {
115
188
$ result = [];
0 commit comments