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

Commit fb44bb9

Browse files
committed
RouteAccessControl filter autoCreatePermission option in debug mode
1 parent 1497566 commit fb44bb9

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
v1.2
55
---------------------
66
* NEW: Bootstrap4 Themes support.
7+
* NEW: RouteAccessControl filter autoCreatePermission option in debug mode.
78

89
v1.1.3
910
---------------------

src/Module.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
class Module extends \yii\base\Module
77
{
8-
98
public $defaultRoute = 'permissions/index';
109

1110
public function init()
1211
{
1312
parent::init();
1413
}
15-
1614
}

src/filters/RouteAccessControl.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class RouteAccessControl extends ActionFilter
2626
*/
2727
public $allowRegexp = '/^(site)\//i';
2828

29+
/**
30+
* Creates controller/action permission automatically if they are missing in debug mode
31+
*
32+
* @var bool
33+
*/
34+
public $autoCreatePermissions = true;
35+
2936
/**
3037
* RouteAccessControl constructor.
3138
*
@@ -67,6 +74,7 @@ public function beforeAction($action)
6774
) {
6875
$allow = true;
6976
} else {
77+
$this->autoCreatePermissions($action_rule, $controller_rule);
7078
$allow = Yii::$app->user->can($action_rule);
7179
}
7280

@@ -87,4 +95,31 @@ public function denyAccess()
8795
{
8896
throw new ForbiddenHttpException('You are not allowed to perform this action.');
8997
}
90-
}
98+
99+
/**
100+
* Auto Create Permissions
101+
* in debug mode create permissions automatically and assign them to master.
102+
*
103+
* @param string $action_rule
104+
* @param string $controller_rule
105+
*/
106+
protected function autoCreatePermissions($action_rule, $controller_rule)
107+
{
108+
if (! YII_DEBUG && $this->autoCreatePermissions) {
109+
return;
110+
}
111+
112+
$auth = \Yii::$app->authManager;
113+
if (! $auth->getPermission($action_rule)) {
114+
$perm = $auth->createPermission($action_rule);
115+
$perm->description = 'Route ' . $action_rule;
116+
$auth->add($perm);
117+
118+
if (! $auth->getPermission($controller_rule)) {
119+
$perm = $auth->createPermission($controller_rule);
120+
$perm->description = 'Route ' . $controller_rule;
121+
$auth->add($perm);
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)