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

Commit 3172ec0

Browse files
committed
add denyActions & update @inheritdoc
1 parent acdeb09 commit 3172ec0

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Yii2 RBAC Change Log
22
====================
33

4+
Version 0.2.0
5+
--------------
6+
- Add denyActions
7+
48
Version 0.1.2
59
--------------
610
- Add Doc

documentation/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ The code below shows how to use ACF which is implemented as `diecoding\rbac\Acce
6464
// add a lot of actions here until you finally completed setting up rbac,
6565
// otherwise you may not even take a first step.
6666
],
67+
'denyActions' => [
68+
'some-controller/some-action',
69+
// The actions listed here will be deny to everyone including guests.
70+
],
6771
],
6872
...
6973
```

src/AppAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use yii\web\AssetBundle;
66

77
/**
8-
* @inheritDoc
8+
* @inheritdoc
99
*
1010
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
1111
* @copyright 2019 Die Coding

src/Module.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Yii;
66

77
/**
8-
* @inheritDoc
8+
* @inheritdoc
99
*
1010
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
1111
* @copyright 2019 Die Coding
@@ -16,17 +16,17 @@
1616
class Module extends \mdm\admin\Module
1717
{
1818
/**
19-
* @inheritDoc
19+
* @inheritdoc
2020
*/
2121
public $controllerNamespace = 'mdm\admin\controllers';
2222

2323
/**
24-
* @inheritDoc
24+
* @inheritdoc
2525
*/
2626
public $layout = 'left-menu';
2727

2828
/**
29-
* @inheritDoc
29+
* @inheritdoc
3030
*/
3131
public function init()
3232
{

src/components/AccessControl.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22

33
namespace diecoding\rbac\components;
44

5+
use yii\base\Module;
6+
use Yii;
7+
use yii\web\ForbiddenHttpException;
8+
59
/**
6-
* @inheritDoc
10+
* To use AccessControl, declare it in the application config as behavior.
11+
* For example.
12+
*
13+
* ```
14+
* 'as access' => [
15+
* 'class' => 'mdm\admin\components\AccessControl',
16+
* 'allowActions' => ['site/login', 'site/error'],
17+
* 'denyActions' => ['test/*'],
18+
* ]
19+
* ```
20+
*
21+
* @inheritdoc
722
*
823
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
924
* @copyright 2019 Die Coding
@@ -13,5 +28,77 @@
1328
*/
1429
class AccessControl extends \mdm\admin\components\AccessControl
1530
{
31+
/**
32+
* @var array List of action that no access.
33+
*/
34+
public $denyActions = [];
35+
36+
/**
37+
* @inheritdoc
38+
*
39+
* @since 0.2.0
40+
*/
41+
protected function isActive($action)
42+
{
43+
$uniqueId = $action->getUniqueId();
44+
if ($uniqueId === Yii::$app->getErrorHandler()->errorAction) {
45+
return false;
46+
}
47+
48+
$user = $this->getUser();
49+
if ($user->getIsGuest()) {
50+
$loginUrl = null;
51+
if (is_array($user->loginUrl) && isset($user->loginUrl[0])) {
52+
$loginUrl = $user->loginUrl[0];
53+
} else if (is_string($user->loginUrl)) {
54+
$loginUrl = $user->loginUrl;
55+
}
56+
if (!is_null($loginUrl) && trim($loginUrl, '/') === $uniqueId) {
57+
return false;
58+
}
59+
}
60+
61+
if ($this->owner instanceof Module) {
62+
// convert action uniqueId into an ID relative to the module
63+
$mid = $this->owner->getUniqueId();
64+
$id = $uniqueId;
65+
if ($mid !== '' && strpos($id, $mid . '/') === 0) {
66+
$id = substr($id, strlen($mid) + 1);
67+
}
68+
} else {
69+
$id = $action->id;
70+
}
71+
72+
foreach ($this->denyActions as $route) {
73+
if (substr($route, -1) === '*') {
74+
$route = rtrim($route, "*");
75+
if ($route === '' || strpos($id, $route) === 0) {
76+
throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
77+
}
78+
} else {
79+
if ($id === $route) {
80+
throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
81+
}
82+
}
83+
}
84+
85+
foreach ($this->allowActions as $route) {
86+
if (substr($route, -1) === '*') {
87+
$route = rtrim($route, "*");
88+
if ($route === '' || strpos($id, $route) === 0) {
89+
return false;
90+
}
91+
} else {
92+
if ($id === $route) {
93+
return false;
94+
}
95+
}
96+
}
97+
98+
if ($action->controller->hasMethod('allowAction') && in_array($action->id, $action->controller->allowAction())) {
99+
return false;
100+
}
16101

102+
return true;
103+
}
17104
}

src/components/Assign.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use yii\base\Component;
66

77
/**
8-
* @inheritDoc
8+
* @inheritdoc
99
*
1010
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
1111
* @copyright 2019 Die Coding

0 commit comments

Comments
 (0)