Skip to content

Commit

Permalink
Inserted fix from pull request webvimark#171.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mladen committed May 20, 2020
1 parent 3a8f14f commit 2dfb8be
Showing 1 changed file with 44 additions and 88 deletions.
132 changes: 44 additions & 88 deletions components/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use yii\helpers\Url;
use yii\rbac\DbManager;

/**
*
*/
class AuthHelper
{
const SESSION_PREFIX_LAST_UPDATE = '__auth_last_update';
Expand All @@ -29,23 +32,15 @@ class AuthHelper
*/
public static function layoutHandler($event)
{
if ( $event->action->uniqueId == 'user-management/auth/login' )
{
if ($event->action->uniqueId == 'user-management/auth/login') {
$event->action->controller->layout = 'loginLayout.php';
}
elseif ( $event->action->controller->id == 'auth' )
{
if ( in_array($event->action->id, ['change-own-password', 'confirm-email']) )
{
} elseif ($event->action->controller->id == 'auth') {
if (in_array($event->action->id, ['change-own-password', 'confirm-email'])) {
$event->action->controller->layout = '//back.php';
}
else
{
} else {
$event->action->controller->layout = '//main.php';
}
}
else
{
} else {
$event->action->controller->layout = '//back.php';
}
}
Expand Down Expand Up @@ -78,10 +73,8 @@ public static function updatePermissions($identity)
*/
public static function ensurePermissionsUpToDate()
{
if ( !Yii::$app->user->isGuest )
{
if ( Yii::$app->session->get(self::SESSION_PREFIX_LAST_UPDATE) != filemtime(self::getPermissionsLastModFile()) )
{
if (!Yii::$app->user->isGuest) {
if (Yii::$app->session->get(self::SESSION_PREFIX_LAST_UPDATE) != filemtime(self::getPermissionsLastModFile())) {
static::updatePermissions(Yii::$app->user->identity);
}
}
Expand All @@ -96,8 +89,7 @@ public static function getPermissionsLastModFile()
{
$file = Yii::$app->runtimePath . '/__permissions_last_mod.txt';

if ( !is_file($file) )
{
if (!is_file($file)) {
file_put_contents($file, '');
chmod($file, 0777);
}
Expand All @@ -112,7 +104,7 @@ public static function invalidatePermissions()
{
touch(static::getPermissionsLastModFile());
}

/**
* Return route without baseUrl and start it with slash
*
Expand All @@ -123,43 +115,35 @@ public static function invalidatePermissions()
public static function unifyRoute($route)
{
// If its like Html::a('Create', ['create'])
if ( is_array($route) AND strpos($route[0], '/') === false )
{
if (is_array($route) and strpos($route[0], '/') === false) {
$route = Url::toRoute($route);
}

if ( Yii::$app->getUrlManager()->showScriptName === true )
{
if (Yii::$app->getUrlManager()->showScriptName === true) {
$baseUrl = Yii::$app->getRequest()->scriptUrl;
}
else
{
} else {
$baseUrl = Yii::$app->getRequest()->baseUrl;
}

// Check if $route has been passed as array or as string with params (or without)
if ( !is_array($route) )
{
if (!is_array($route)) {
$route = explode('?', $route);
}

$routeAsString = $route[0];

// If it's not clean url like localhost/folder/index.php/bla-bla then remove
// baseUrl and leave only relative path 'bla-bla'
if ( $baseUrl )
{
if ( strpos($routeAsString, $baseUrl) === 0 )
{
if ($baseUrl) {
if (strpos($routeAsString, $baseUrl) === 0) {
$routeAsString = substr_replace($routeAsString, '', 0, strlen($baseUrl));
}
}

$languagePrefix = '/' . Yii::$app->language . '/';

// Remove language prefix
if ( strpos($routeAsString, $languagePrefix) === 0 )
{
if (strpos($routeAsString, $languagePrefix) === 0) {
$routeAsString = substr_replace($routeAsString, '', 0, strlen($languagePrefix));
}

Expand All @@ -182,10 +166,8 @@ public static function getChildrenByType($itemName, $childType)

$result = [];

foreach ($children as $id => $item)
{
if ( $item->type == $childType )
{
foreach ($children as $id => $item) {
if ($item->type == $childType) {
$result[$id] = $item;
}
}
Expand All @@ -207,21 +189,15 @@ public static function separateRoutesAndPermissions($allPermissions)
$routes = [];
$permissions = [];

foreach ($arrayOfPermissions as $id => $item)
{
if ( $item->type == AbstractItem::TYPE_ROUTE )
{
foreach ($arrayOfPermissions as $id => $item) {
if ($item->type == AbstractItem::TYPE_ROUTE) {
$routes[$id] = $item;

}
else
{
} else {
$permissions[$id] = $item;

}
}

return (object)compact('routes', 'permissions');
return (object) compact('routes', 'permissions');
}


Expand All @@ -234,8 +210,7 @@ public static function getAllModules()

$currentEnvModules = \Yii::$app->getModules();

foreach ($currentEnvModules as $moduleId => $uselessStuff)
{
foreach ($currentEnvModules as $moduleId => $uselessStuff) {
$result[$moduleId] = \Yii::$app->getModule($moduleId);
}

Expand All @@ -261,16 +236,13 @@ public static function getRoutes()
*/
private static function getRouteRecursive($module, &$result)
{
foreach ($module->getModules() as $id => $child)
{
if ( ($child = $module->getModule($id)) !== null )
{
foreach ($module->getModules() as $id => $child) {
if (($child = $module->getModule($id)) !== null) {
self::getRouteRecursive($child, $result);
}
}
/* @var $controller \yii\base\Controller */
foreach ($module->controllerMap as $id => $value)
{
foreach ($module->controllerMap as $id => $value) {
$controller = Yii::createObject($value, [
$id,
$module
Expand All @@ -282,12 +254,9 @@ private static function getRouteRecursive($module, &$result)
$namespace = trim($module->controllerNamespace, '\\') . '\\';
self::getControllerRoutes($module, $namespace, '', $result);

if ( $module->uniqueId )
{
$result[] = '/'. $module->uniqueId . '/*';
}
else
{
if ($module->uniqueId) {
$result[] = '/' . $module->uniqueId . '/*';
} else {
$result[] = $module->uniqueId . '/*';
}
}
Expand All @@ -299,16 +268,13 @@ private static function getRouteRecursive($module, &$result)
private static function getActionRoutes($controller, &$result)
{
$prefix = '/' . $controller->uniqueId . '/';
foreach ($controller->actions() as $id => $value)
{
foreach ($controller->actions() as $id => $value) {
$result[] = $prefix . $id;
}
$class = new \ReflectionClass($controller);
foreach ($class->getMethods() as $method)
{
foreach ($class->getMethods() as $method) {
$name = $method->getName();
if ( $method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions' )
{
if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') {
$result[] = $prefix . Inflector::camel2id(substr($name, 6));
}
}
Expand All @@ -322,34 +288,24 @@ private static function getActionRoutes($controller, &$result)
*/
private static function getControllerRoutes($module, $namespace, $prefix, &$result)
{
try
{
try {
$path = Yii::getAlias('@' . str_replace('\\', '/', $namespace));
}
catch (InvalidParamException $e)
{
} catch (InvalidParamException $e) {
$path = $module->getBasePath() . '/controllers';
}

if ( is_dir($path) )
{
foreach (scandir($path) as $file)
{
if ( strpos($file, '.') === 0 )
{
if (is_dir($path)) {
foreach (scandir($path) as $file) {
if (strpos($file, '.') === 0) {
continue;
}

if ( is_dir($path . '/' . $file) )
{
if (is_dir($path . '/' . $file)) {
self::getControllerRoutes($module, $namespace . $file . '\\', $prefix . $file . '/', $result);
}
elseif ( strcmp(substr($file, -14), 'Controller.php') === 0 )
{
} elseif (strcmp(substr($file, -14), 'Controller.php') === 0) {
$id = Inflector::camel2id(substr(basename($file), 0, -14), '-', true);
$className = $namespace . Inflector::id2camel($id) . 'Controller';
if ( strpos($className, '-') === false && class_exists($className) && is_subclass_of($className, 'yii\base\Controller') )
{
if (strpos($className, '-') === false && class_exists($className) && is_subclass_of($className, 'yii\base\Controller')) {
$controller = new $className($prefix . $id, $module);
self::getActionRoutes($controller, $result);
$result[] = '/' . $controller->uniqueId . '/*';
Expand All @@ -358,4 +314,4 @@ private static function getControllerRoutes($module, $namespace, $prefix, &$resu
}
}
}
}
}

0 comments on commit 2dfb8be

Please sign in to comment.