diff --git a/CHANGELOG.md b/CHANGELOG.md index 291bbb1..d8c4122 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG v1.2 --------------------- * NEW: Bootstrap4 Themes support. +* NEW: RouteAccessControl filter autoCreatePermission option in debug mode. v1.1.3 --------------------- diff --git a/src/Module.php b/src/Module.php index 8c006d1..a513cbc 100755 --- a/src/Module.php +++ b/src/Module.php @@ -5,12 +5,10 @@ class Module extends \yii\base\Module { - public $defaultRoute = 'permissions/index'; public function init() { parent::init(); } - } diff --git a/src/filters/RouteAccessControl.php b/src/filters/RouteAccessControl.php index 1d22153..355692d 100755 --- a/src/filters/RouteAccessControl.php +++ b/src/filters/RouteAccessControl.php @@ -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. * @@ -67,6 +74,7 @@ public function beforeAction($action) ) { $allow = true; } else { + $this->autoCreatePermissions($action_rule, $controller_rule); $allow = Yii::$app->user->can($action_rule); } @@ -87,4 +95,31 @@ public function denyAccess() { throw new ForbiddenHttpException('You are not allowed to perform this action.'); } -} \ No newline at end of file + + /** + * 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); + } + } + } +}