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

Commit 3449c74

Browse files
authored
Merge pull request #12 from justcoded/develop
Bootstrap4 support, RouteAccessControl filter autoCreatePermission option
2 parents a973d38 + fb44bb9 commit 3449c74

File tree

15 files changed

+131
-167
lines changed

15 files changed

+131
-167
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=====================
33

4+
v1.2
5+
---------------------
6+
* NEW: Bootstrap4 Themes support.
7+
* NEW: RouteAccessControl filter autoCreatePermission option in debug mode.
8+
49
v1.1.3
510
---------------------
611
* Bugfix: Compatibility with Adminlte v2.6.0 (conflict of $.fn.tree plugin name). @ap

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ To use the RBAC extension, you need to configure the components array in your ap
9292
],
9393
```
9494

95+
##### Bootstrap4 Themes Support
96+
97+
By default all views use standard yii2-bootstrap package with Boostrap v3.
98+
If you use modern Bootstrap 4, then you can overwrite some classes to use yii2-bootstrap4
99+
package instead. Inside your configuration you need to reconfigure container dependencies like
100+
this:
101+
102+
```php
103+
'container' => [
104+
'definitions' => [
105+
// you can create your own GrivView to customize all options for main roles and permissions lists.
106+
'justcoded\yii2\rbac\widgets\RbacGridView' => [
107+
'class' => \app\modules\admin\widgets\RbacGridView::class,
108+
],
109+
// this will replace bootstrap3 ActiveForm with bootstrap4 ActiveForm.
110+
'justcoded\yii2\rbac\widgets\RbacActiveForm' => [
111+
'class' => \yii\bootstrap4\ActiveForm::class,
112+
],
113+
],
114+
],
115+
```
116+
117+
* Note: you need to add `yiisoft/yii2-bootstrap4` package dependency manually in your `composer.json`.
118+
95119
#### Basic RBAC configuration
96120

97121
Please follow [oficial documentation](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#configuring-rbac)

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/assets/RbacAssetBundle.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ class RbacAssetBundle extends \yii\web\AssetBundle
1818
];
1919
public $depends = [
2020
'yii\web\YiiAsset',
21-
'yii\bootstrap\BootstrapAsset',
2221
];
2322
}

src/assets/js/rbac.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
$('#inheritSearch').on("keyup change", function () {
3939
$('#inheritPermissions').jstree(true).search($('#inheritSearch').val());
4040
});
41+
$('#allowPermissionsCntrl').addClass('d-none');
4142
}
4243

4344
})(jQuery);

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+
}

src/views/index.php

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/views/permissions/_form.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@
66

77
use justcoded\yii2\rbac\forms\PermissionRelForm;
88
use yii\helpers\Html;
9-
use yii\bootstrap\ActiveForm;
10-
use kartik\select2\Select2;
9+
use justcoded\yii2\rbac\widgets\RbacActiveForm;
1110
use justcoded\yii2\rbac\forms\ItemForm;
1211

1312
?>
1413

1514
<div class="row">
1615
<div class="col-md-7">
17-
<?php $form = ActiveForm::begin([
16+
<?php $form = RbacActiveForm::begin([
1817
'id' => 'form-permission',
1918
]); ?>
20-
<div class="panel box">
21-
<div class="panel-header box-header with-border">
22-
<h3 class="box-title">Permission Details</h3>
19+
<div class="panel box card">
20+
<div class="panel-header box-header with-border card-header">
21+
<h3 class="box-title card-title">Permission Details</h3>
2322
</div>
24-
<div class="panel-body box-body">
23+
<div class="panel-body box-body card-body">
2524

2625
<?= $form->field($model, 'name')->textInput([
2726
'maxlength' => true,
@@ -33,7 +32,7 @@
3332
<?= $form->field($model, 'ruleClass')->textInput() ?>
3433

3534
</div>
36-
<div class="panel-footer box-footer text-right">
35+
<div class="panel-footer box-footer card-footer text-right">
3736
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?> &nbsp;
3837
<?php if (!empty($permission)) : ?>
3938
<?= Html::a(
@@ -50,7 +49,7 @@
5049
<?php endif; ?>
5150
</div>
5251
</div>
53-
<?php ActiveForm::end(); ?>
52+
<?php $form::end(); ?>
5453

5554
<?php if (!empty($permission)) : ?>
5655
<p class="text-center">

src/views/permissions/_relations-box.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
/* @var $selected array */
1111

1212
use kartik\select2\Select2;
13-
use yii\bootstrap\ActiveForm;
14-
use yii\bootstrap\Html;
13+
use justcoded\yii2\rbac\widgets\RbacActiveForm;
14+
use yii\helpers\Html;
1515

1616
?>
1717

18-
<?php $form = ActiveForm::begin([
18+
<?php $form = RbacActiveForm::begin([
1919
'action' => ['add-relation', 'name' => $model->name],
2020
]); ?>
2121

@@ -86,4 +86,4 @@
8686
</div>
8787
</div>
8888

89-
<?php ActiveForm::end(); ?>
89+
<?php $form::end(); ?>

src/views/permissions/index.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use yii\helpers\Html;
44
use justcoded\yii2\rbac\models\ItemSearch;
55
use justcoded\yii2\rbac\forms\RoleForm;
6-
use yii\grid\GridView;
6+
use justcoded\yii2\rbac\widgets\RbacGridView;
77

88
/* @var $this yii\web\View */
99
/* @var $searchModel justcoded\yii2\rbac\models\ItemSearch */
1010
/* @var $dataProviderPermissions yii\data\ActiveDataProvider */
1111
/* @var $dataProviderRoles yii\data\ActiveDataProvider */
1212

13-
$this->title = 'Permissions';
13+
$this->title = 'Permissions';
1414
$this->params['breadcrumbs'][] = $this->title;
1515
?>
1616

@@ -22,13 +22,12 @@
2222
<div class="panel-header box-header with-border">
2323
<h3 class="box-title">Roles
2424
&nbsp;
25-
<?= Html::a('Add Role', ['roles/create'], ['class' => 'btn btn-xs btn-success']); ?>
25+
<?= Html::a('Add Role', ['roles/create'], ['class' => 'btn btn-xs btn-sm btn-success']); ?>
2626
</h3>
2727
</div>
2828
<div class="panel-body box-body">
29-
<?= GridView::widget([
29+
<?= RbacGridView::widget([
3030
'dataProvider' => $dataProviderRoles,
31-
'layout' => '{items}{pager}',
3231
'filterModel' => $searchModel,
3332
'columns' => [
3433
['class' => 'yii\grid\SerialColumn'],
@@ -42,7 +41,7 @@
4241
},
4342
],
4443
[
45-
'header' => 'Permissions #',
44+
'header' => 'Permissions',
4645
'headerOptions' => ['class' => 'col-md-2'],
4746
'contentOptions' => ['class' => 'text-center'],
4847
'value' => function ($data) {
@@ -66,23 +65,18 @@
6665
<div class="panel-header box-header with-border">
6766
<h3 class="box-title">Permissions
6867
&nbsp;
69-
<?= Html::a('Add Permission', ['permissions/create'], ['class' => 'btn btn-xs btn-success']); ?>
70-
<?= Html::a('Scan Routes', ['permissions/scan'], ['class' => 'btn btn-xs btn-default']); ?>
68+
<?= Html::a('Add Permission', ['permissions/create'], ['class' => 'btn btn-xs btn-sm btn-success']); ?>
69+
<?= Html::a('Scan Routes', ['permissions/scan'], ['class' => 'btn btn-xs btn-sm btn-default']); ?>
7170
</h3>
7271
</div>
7372
<div class="panel-body box-body">
74-
<?= GridView::widget([
73+
<?= RbacGridView::widget([
7574
'dataProvider' => $dataProviderPermissions,
76-
'layout' => '{items}
77-
<div class="row">
78-
<div class="col-md-6">{summary}</div>
79-
<div class="col-md-6 text-right">{pager}</div>
80-
</div>',
8175
'filterModel' => $searchModel,
8276
'columns' => [
8377
['class' => 'yii\grid\SerialColumn'],
8478
[
85-
'header' => 'Permissions',
79+
'header' => 'Permission',
8680
'format' => 'html',
8781
'filter' => Html::activeTextInput($searchModel, 'permName', ['class' => 'form-control']),
8882
'value' => function ($data) {
@@ -93,7 +87,7 @@
9387
'attribute' => 'description',
9488
],
9589
[
96-
'header' => 'Role',
90+
'header' => 'Roles',
9791
'format' => 'html',
9892
'headerOptions' => ['class' => 'col-md-2'],
9993
'filter' => Html::activeDropDownList($searchModel, 'permRole', \justcoded\yii2\rbac\models\Role::getList(),

0 commit comments

Comments
 (0)