1212use yii \helpers \Url ;
1313use yii \rbac \DbManager ;
1414
15+ /**
16+ *
17+ */
1518class AuthHelper
1619{
1720 const SESSION_PREFIX_LAST_UPDATE = '__auth_last_update ' ;
@@ -29,23 +32,15 @@ class AuthHelper
2932 */
3033 public static function layoutHandler ($ event )
3134 {
32- if ( $ event ->action ->uniqueId == 'user-management/auth/login ' )
33- {
35+ if ($ event ->action ->uniqueId == 'user-management/auth/login ' ) {
3436 $ event ->action ->controller ->layout = 'loginLayout.php ' ;
35- }
36- elseif ( $ event ->action ->controller ->id == 'auth ' )
37- {
38- if ( in_array ($ event ->action ->id , ['change-own-password ' , 'confirm-email ' ]) )
39- {
37+ } elseif ($ event ->action ->controller ->id == 'auth ' ) {
38+ if (in_array ($ event ->action ->id , ['change-own-password ' , 'confirm-email ' ])) {
4039 $ event ->action ->controller ->layout = '//back.php ' ;
41- }
42- else
43- {
40+ } else {
4441 $ event ->action ->controller ->layout = '//main.php ' ;
4542 }
46- }
47- else
48- {
43+ } else {
4944 $ event ->action ->controller ->layout = '//back.php ' ;
5045 }
5146 }
@@ -78,10 +73,8 @@ public static function updatePermissions($identity)
7873 */
7974 public static function ensurePermissionsUpToDate ()
8075 {
81- if ( !Yii::$ app ->user ->isGuest )
82- {
83- if ( Yii::$ app ->session ->get (self ::SESSION_PREFIX_LAST_UPDATE ) != filemtime (self ::getPermissionsLastModFile ()) )
84- {
76+ if (!Yii::$ app ->user ->isGuest ) {
77+ if (Yii::$ app ->session ->get (self ::SESSION_PREFIX_LAST_UPDATE ) != filemtime (self ::getPermissionsLastModFile ())) {
8578 static ::updatePermissions (Yii::$ app ->user ->identity );
8679 }
8780 }
@@ -96,8 +89,7 @@ public static function getPermissionsLastModFile()
9689 {
9790 $ file = Yii::$ app ->runtimePath . '/__permissions_last_mod.txt ' ;
9891
99- if ( !is_file ($ file ) )
100- {
92+ if (!is_file ($ file )) {
10193 file_put_contents ($ file , '' );
10294 chmod ($ file , 0777 );
10395 }
@@ -112,7 +104,7 @@ public static function invalidatePermissions()
112104 {
113105 touch (static ::getPermissionsLastModFile ());
114106 }
115-
107+
116108 /**
117109 * Return route without baseUrl and start it with slash
118110 *
@@ -123,43 +115,35 @@ public static function invalidatePermissions()
123115 public static function unifyRoute ($ route )
124116 {
125117 // If its like Html::a('Create', ['create'])
126- if ( is_array ($ route ) AND strpos ($ route [0 ], '/ ' ) === false )
127- {
118+ if (is_array ($ route ) and strpos ($ route [0 ], '/ ' ) === false ) {
128119 $ route = Url::toRoute ($ route );
129120 }
130121
131- if ( Yii::$ app ->getUrlManager ()->showScriptName === true )
132- {
122+ if (Yii::$ app ->getUrlManager ()->showScriptName === true ) {
133123 $ baseUrl = Yii::$ app ->getRequest ()->scriptUrl ;
134- }
135- else
136- {
124+ } else {
137125 $ baseUrl = Yii::$ app ->getRequest ()->baseUrl ;
138126 }
139127
140128 // Check if $route has been passed as array or as string with params (or without)
141- if ( !is_array ($ route ) )
142- {
129+ if (!is_array ($ route )) {
143130 $ route = explode ('? ' , $ route );
144131 }
145132
146133 $ routeAsString = $ route [0 ];
147134
148135 // If it's not clean url like localhost/folder/index.php/bla-bla then remove
149136 // baseUrl and leave only relative path 'bla-bla'
150- if ( $ baseUrl )
151- {
152- if ( strpos ($ routeAsString , $ baseUrl ) === 0 )
153- {
137+ if ($ baseUrl ) {
138+ if (strpos ($ routeAsString , $ baseUrl ) === 0 ) {
154139 $ routeAsString = substr_replace ($ routeAsString , '' , 0 , strlen ($ baseUrl ));
155140 }
156141 }
157142
158143 $ languagePrefix = '/ ' . Yii::$ app ->language . '/ ' ;
159144
160145 // Remove language prefix
161- if ( strpos ($ routeAsString , $ languagePrefix ) === 0 )
162- {
146+ if (strpos ($ routeAsString , $ languagePrefix ) === 0 ) {
163147 $ routeAsString = substr_replace ($ routeAsString , '' , 0 , strlen ($ languagePrefix ));
164148 }
165149
@@ -182,10 +166,8 @@ public static function getChildrenByType($itemName, $childType)
182166
183167 $ result = [];
184168
185- foreach ($ children as $ id => $ item )
186- {
187- if ( $ item ->type == $ childType )
188- {
169+ foreach ($ children as $ id => $ item ) {
170+ if ($ item ->type == $ childType ) {
189171 $ result [$ id ] = $ item ;
190172 }
191173 }
@@ -207,21 +189,15 @@ public static function separateRoutesAndPermissions($allPermissions)
207189 $ routes = [];
208190 $ permissions = [];
209191
210- foreach ($ arrayOfPermissions as $ id => $ item )
211- {
212- if ( $ item ->type == AbstractItem::TYPE_ROUTE )
213- {
192+ foreach ($ arrayOfPermissions as $ id => $ item ) {
193+ if ($ item ->type == AbstractItem::TYPE_ROUTE ) {
214194 $ routes [$ id ] = $ item ;
215-
216- }
217- else
218- {
195+ } else {
219196 $ permissions [$ id ] = $ item ;
220-
221197 }
222198 }
223199
224- return (object )compact ('routes ' , 'permissions ' );
200+ return (object ) compact ('routes ' , 'permissions ' );
225201 }
226202
227203
@@ -234,8 +210,7 @@ public static function getAllModules()
234210
235211 $ currentEnvModules = \Yii::$ app ->getModules ();
236212
237- foreach ($ currentEnvModules as $ moduleId => $ uselessStuff )
238- {
213+ foreach ($ currentEnvModules as $ moduleId => $ uselessStuff ) {
239214 $ result [$ moduleId ] = \Yii::$ app ->getModule ($ moduleId );
240215 }
241216
@@ -261,16 +236,13 @@ public static function getRoutes()
261236 */
262237 private static function getRouteRecursive ($ module , &$ result )
263238 {
264- foreach ($ module ->getModules () as $ id => $ child )
265- {
266- if ( ($ child = $ module ->getModule ($ id )) !== null )
267- {
239+ foreach ($ module ->getModules () as $ id => $ child ) {
240+ if (($ child = $ module ->getModule ($ id )) !== null ) {
268241 self ::getRouteRecursive ($ child , $ result );
269242 }
270243 }
271244 /* @var $controller \yii\base\Controller */
272- foreach ($ module ->controllerMap as $ id => $ value )
273- {
245+ foreach ($ module ->controllerMap as $ id => $ value ) {
274246 $ controller = Yii::createObject ($ value , [
275247 $ id ,
276248 $ module
@@ -282,12 +254,9 @@ private static function getRouteRecursive($module, &$result)
282254 $ namespace = trim ($ module ->controllerNamespace , '\\' ) . '\\' ;
283255 self ::getControllerRoutes ($ module , $ namespace , '' , $ result );
284256
285- if ( $ module ->uniqueId )
286- {
287- $ result [] = '/ ' . $ module ->uniqueId . '/* ' ;
288- }
289- else
290- {
257+ if ($ module ->uniqueId ) {
258+ $ result [] = '/ ' . $ module ->uniqueId . '/* ' ;
259+ } else {
291260 $ result [] = $ module ->uniqueId . '/* ' ;
292261 }
293262 }
@@ -299,16 +268,13 @@ private static function getRouteRecursive($module, &$result)
299268 private static function getActionRoutes ($ controller , &$ result )
300269 {
301270 $ prefix = '/ ' . $ controller ->uniqueId . '/ ' ;
302- foreach ($ controller ->actions () as $ id => $ value )
303- {
271+ foreach ($ controller ->actions () as $ id => $ value ) {
304272 $ result [] = $ prefix . $ id ;
305273 }
306274 $ class = new \ReflectionClass ($ controller );
307- foreach ($ class ->getMethods () as $ method )
308- {
275+ foreach ($ class ->getMethods () as $ method ) {
309276 $ name = $ method ->getName ();
310- if ( $ method ->isPublic () && !$ method ->isStatic () && strpos ($ name , 'action ' ) === 0 && $ name !== 'actions ' )
311- {
277+ if ($ method ->isPublic () && !$ method ->isStatic () && strpos ($ name , 'action ' ) === 0 && $ name !== 'actions ' ) {
312278 $ result [] = $ prefix . Inflector::camel2id (substr ($ name , 6 ));
313279 }
314280 }
@@ -322,34 +288,24 @@ private static function getActionRoutes($controller, &$result)
322288 */
323289 private static function getControllerRoutes ($ module , $ namespace , $ prefix , &$ result )
324290 {
325- try
326- {
291+ try {
327292 $ path = Yii::getAlias ('@ ' . str_replace ('\\' , '/ ' , $ namespace ));
328- }
329- catch (InvalidParamException $ e )
330- {
293+ } catch (InvalidParamException $ e ) {
331294 $ path = $ module ->getBasePath () . '/controllers ' ;
332295 }
333296
334- if ( is_dir ($ path ) )
335- {
336- foreach (scandir ($ path ) as $ file )
337- {
338- if ( strpos ($ file , '. ' ) === 0 )
339- {
297+ if (is_dir ($ path )) {
298+ foreach (scandir ($ path ) as $ file ) {
299+ if (strpos ($ file , '. ' ) === 0 ) {
340300 continue ;
341301 }
342302
343- if ( is_dir ($ path . '/ ' . $ file ) )
344- {
303+ if (is_dir ($ path . '/ ' . $ file )) {
345304 self ::getControllerRoutes ($ module , $ namespace . $ file . '\\' , $ prefix . $ file . '/ ' , $ result );
346- }
347- elseif ( strcmp (substr ($ file , -14 ), 'Controller.php ' ) === 0 )
348- {
305+ } elseif (strcmp (substr ($ file , -14 ), 'Controller.php ' ) === 0 ) {
349306 $ id = Inflector::camel2id (substr (basename ($ file ), 0 , -14 ), '- ' , true );
350307 $ className = $ namespace . Inflector::id2camel ($ id ) . 'Controller ' ;
351- if ( strpos ($ className , '- ' ) === false && class_exists ($ className ) && is_subclass_of ($ className , 'yii\base\Controller ' ) )
352- {
308+ if (strpos ($ className , '- ' ) === false && class_exists ($ className ) && is_subclass_of ($ className , 'yii\base\Controller ' )) {
353309 $ controller = new $ className ($ prefix . $ id , $ module );
354310 self ::getActionRoutes ($ controller , $ result );
355311 $ result [] = '/ ' . $ controller ->uniqueId . '/* ' ;
@@ -358,4 +314,4 @@ private static function getControllerRoutes($module, $namespace, $prefix, &$resu
358314 }
359315 }
360316 }
361- }
317+ }
0 commit comments