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

Commit

Permalink
Merge pull request #12 from justcoded/develop
Browse files Browse the repository at this point in the history
Bootstrap4 support,  RouteAccessControl filter autoCreatePermission option
  • Loading branch information
aprokopenko authored Jul 13, 2019
2 parents a973d38 + fb44bb9 commit 3449c74
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 167 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=====================

v1.2
---------------------
* NEW: Bootstrap4 Themes support.
* NEW: RouteAccessControl filter autoCreatePermission option in debug mode.

v1.1.3
---------------------
* Bugfix: Compatibility with Adminlte v2.6.0 (conflict of $.fn.tree plugin name). @ap
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ To use the RBAC extension, you need to configure the components array in your ap
],
```

##### Bootstrap4 Themes Support

By default all views use standard yii2-bootstrap package with Boostrap v3.
If you use modern Bootstrap 4, then you can overwrite some classes to use yii2-bootstrap4
package instead. Inside your configuration you need to reconfigure container dependencies like
this:

```php
'container' => [
'definitions' => [
// you can create your own GrivView to customize all options for main roles and permissions lists.
'justcoded\yii2\rbac\widgets\RbacGridView' => [
'class' => \app\modules\admin\widgets\RbacGridView::class,
],
// this will replace bootstrap3 ActiveForm with bootstrap4 ActiveForm.
'justcoded\yii2\rbac\widgets\RbacActiveForm' => [
'class' => \yii\bootstrap4\ActiveForm::class,
],
],
],
```

* Note: you need to add `yiisoft/yii2-bootstrap4` package dependency manually in your `composer.json`.

#### Basic RBAC configuration

Please follow [oficial documentation](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#configuring-rbac)
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();
}

}
1 change: 0 additions & 1 deletion src/assets/RbacAssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ class RbacAssetBundle extends \yii\web\AssetBundle
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
1 change: 1 addition & 0 deletions src/assets/js/rbac.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
$('#inheritSearch').on("keyup change", function () {
$('#inheritPermissions').jstree(true).search($('#inheritSearch').val());
});
$('#allowPermissionsCntrl').addClass('d-none');
}

})(jQuery);
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);
}
}
}
}
119 changes: 0 additions & 119 deletions src/views/index.php

This file was deleted.

17 changes: 8 additions & 9 deletions src/views/permissions/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@

use justcoded\yii2\rbac\forms\PermissionRelForm;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\select2\Select2;
use justcoded\yii2\rbac\widgets\RbacActiveForm;
use justcoded\yii2\rbac\forms\ItemForm;

?>

<div class="row">
<div class="col-md-7">
<?php $form = ActiveForm::begin([
<?php $form = RbacActiveForm::begin([
'id' => 'form-permission',
]); ?>
<div class="panel box">
<div class="panel-header box-header with-border">
<h3 class="box-title">Permission Details</h3>
<div class="panel box card">
<div class="panel-header box-header with-border card-header">
<h3 class="box-title card-title">Permission Details</h3>
</div>
<div class="panel-body box-body">
<div class="panel-body box-body card-body">

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

</div>
<div class="panel-footer box-footer text-right">
<div class="panel-footer box-footer card-footer text-right">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?> &nbsp;
<?php if (!empty($permission)) : ?>
<?= Html::a(
Expand All @@ -50,7 +49,7 @@
<?php endif; ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<?php $form::end(); ?>

<?php if (!empty($permission)) : ?>
<p class="text-center">
Expand Down
8 changes: 4 additions & 4 deletions src/views/permissions/_relations-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
/* @var $selected array */

use kartik\select2\Select2;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Html;
use justcoded\yii2\rbac\widgets\RbacActiveForm;
use yii\helpers\Html;

?>

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

Expand Down Expand Up @@ -86,4 +86,4 @@
</div>
</div>

<?php ActiveForm::end(); ?>
<?php $form::end(); ?>
26 changes: 10 additions & 16 deletions src/views/permissions/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use yii\helpers\Html;
use justcoded\yii2\rbac\models\ItemSearch;
use justcoded\yii2\rbac\forms\RoleForm;
use yii\grid\GridView;
use justcoded\yii2\rbac\widgets\RbacGridView;

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

$this->title = 'Permissions';
$this->title = 'Permissions';
$this->params['breadcrumbs'][] = $this->title;
?>

Expand All @@ -22,13 +22,12 @@
<div class="panel-header box-header with-border">
<h3 class="box-title">Roles
&nbsp;
<?= Html::a('Add Role', ['roles/create'], ['class' => 'btn btn-xs btn-success']); ?>
<?= Html::a('Add Role', ['roles/create'], ['class' => 'btn btn-xs btn-sm btn-success']); ?>
</h3>
</div>
<div class="panel-body box-body">
<?= GridView::widget([
<?= RbacGridView::widget([
'dataProvider' => $dataProviderRoles,
'layout' => '{items}{pager}',
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
Expand All @@ -42,7 +41,7 @@
},
],
[
'header' => 'Permissions #',
'header' => 'Permissions',
'headerOptions' => ['class' => 'col-md-2'],
'contentOptions' => ['class' => 'text-center'],
'value' => function ($data) {
Expand All @@ -66,23 +65,18 @@
<div class="panel-header box-header with-border">
<h3 class="box-title">Permissions
&nbsp;
<?= Html::a('Add Permission', ['permissions/create'], ['class' => 'btn btn-xs btn-success']); ?>
<?= Html::a('Scan Routes', ['permissions/scan'], ['class' => 'btn btn-xs btn-default']); ?>
<?= Html::a('Add Permission', ['permissions/create'], ['class' => 'btn btn-xs btn-sm btn-success']); ?>
<?= Html::a('Scan Routes', ['permissions/scan'], ['class' => 'btn btn-xs btn-sm btn-default']); ?>
</h3>
</div>
<div class="panel-body box-body">
<?= GridView::widget([
<?= RbacGridView::widget([
'dataProvider' => $dataProviderPermissions,
'layout' => '{items}
<div class="row">
<div class="col-md-6">{summary}</div>
<div class="col-md-6 text-right">{pager}</div>
</div>',
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'header' => 'Permissions',
'header' => 'Permission',
'format' => 'html',
'filter' => Html::activeTextInput($searchModel, 'permName', ['class' => 'form-control']),
'value' => function ($data) {
Expand All @@ -93,7 +87,7 @@
'attribute' => 'description',
],
[
'header' => 'Role',
'header' => 'Roles',
'format' => 'html',
'headerOptions' => ['class' => 'col-md-2'],
'filter' => Html::activeDropDownList($searchModel, 'permRole', \justcoded\yii2\rbac\models\Role::getList(),
Expand Down
Loading

0 comments on commit 3449c74

Please sign in to comment.