Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
RouteAccessControl filter autoCreatePermission option in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aprokopenko committed Jul 13, 2019
1 parent 1497566 commit fb44bb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
v1.2
---------------------
* NEW: Bootstrap4 Themes support.
* NEW: RouteAccessControl filter autoCreatePermission option in debug mode.

v1.1.3
---------------------
Expand Down
2 changes: 0 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

class Module extends \yii\base\Module
{

public $defaultRoute = 'permissions/index';

public function init()
{
parent::init();
}

}
37 changes: 36 additions & 1 deletion src/filters/RouteAccessControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class RouteAccessControl extends ActionFilter
*/
public $allowRegexp = '/^(site)\//i';

/**
* Creates controller/action permission automatically if they are missing in debug mode
*
* @var bool
*/
public $autoCreatePermissions = true;

/**
* RouteAccessControl constructor.
*
Expand Down Expand Up @@ -67,6 +74,7 @@ public function beforeAction($action)
) {
$allow = true;
} else {
$this->autoCreatePermissions($action_rule, $controller_rule);
$allow = Yii::$app->user->can($action_rule);
}

Expand All @@ -87,4 +95,31 @@ public function denyAccess()
{
throw new ForbiddenHttpException('You are not allowed to perform this action.');
}
}

/**
* Auto Create Permissions
* in debug mode create permissions automatically and assign them to master.
*
* @param string $action_rule
* @param string $controller_rule
*/
protected function autoCreatePermissions($action_rule, $controller_rule)
{
if (! YII_DEBUG && $this->autoCreatePermissions) {
return;
}

$auth = \Yii::$app->authManager;
if (! $auth->getPermission($action_rule)) {
$perm = $auth->createPermission($action_rule);
$perm->description = 'Route ' . $action_rule;
$auth->add($perm);

if (! $auth->getPermission($controller_rule)) {
$perm = $auth->createPermission($controller_rule);
$perm->description = 'Route ' . $controller_rule;
$auth->add($perm);
}
}
}
}

0 comments on commit fb44bb9

Please sign in to comment.