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

Commit 1de9de1

Browse files
committed
release
1 parent f783c53 commit 1de9de1

File tree

5 files changed

+223
-61
lines changed

5 files changed

+223
-61
lines changed

README.md

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
1-
Yii2 RBAC
2-
=========
3-
RBAC / Auth Manager for Yii2 Extends 'mdmsoft/yii2-admin': '~2.0'
1+
# Yii2 RBAC
42

5-
[![Latest Stable Version](https://poser.pugx.org/diecoding/yii2-rbac/v/stable?format=flat-square)](https://packagist.org/packages/diecoding/yii2-rbac)
3+
RBAC / Auth Manager for Yii2 Extends `'mdmsoft/yii2-admin': '~2.0'`
4+
5+
[![Latest Stable Version](https://poser.pugx.org/diecoding/yii2-rbac/v/stable?format=flat-square)](https://github.com/die-coding/yii2-rbac/releases)
66
[![Total Downloads](https://poser.pugx.org/diecoding/yii2-rbac/downloads?format=flat-square)](https://packagist.org/packages/diecoding/yii2-rbac)
77
[![Daily Downloads](https://poser.pugx.org/diecoding/yii2-rbac/d/daily?format=flat-square)](https://packagist.org/packages/diecoding/yii2-rbac)
88
[![License](https://poser.pugx.org/diecoding/yii2-rbac/license?format=flat-square)](LICENSE.md)
99
[![Quality Score](https://img.shields.io/scrutinizer/g/die-coding/yii2-rbac.svg?style=flat-square)](https://scrutinizer-ci.com/g/die-coding/yii2-rbac)
1010
[![Build Status](https://img.shields.io/scrutinizer/build/g/die-coding/yii2-rbac.svg?style=flat-square)](https://scrutinizer-ci.com/g/die-coding/yii2-rbac/build-status/master)
1111

12+
## Documentation
1213

13-
Documentation
14-
-------------
15-
> **Disclaimer: This package is extends from ['mdmsoft/yii2-admin': '~2.0'](https://github.com/mdmsoft/yii2-admin/), you can also use documentation from [https://github.com/mdmsoft/yii2-admin/](https://github.com/mdmsoft/yii2-admin/).**
14+
> **Disclaimer:** This package is extends from ['mdmsoft/yii2-admin': '~2.0'](https://github.com/mdmsoft/yii2-admin/), you can also use documentation from [https://github.com/mdmsoft/yii2-admin/](https://github.com/mdmsoft/yii2-admin/).
1615
16+
- [Change Log](CHANGELOG.md).
17+
- [Installation](#installation).
18+
- [Authorization Guide](https://www.yiiframework.com/doc/guide/2.0/en/security-authorization). Important, read this first before you continue.
19+
- [Configuration](documentations/configuration.md)
20+
- [Using Menu](documentations/menu.md).
21+
- [Screenshots](documentations/screenshots).
1722

18-
- [Change Log](CHANGELOG.md).
19-
- [Authorization Guide](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html). Important, read this first before you continue.
20-
- [Basic Configuration](docs/guide/configuration.md)
21-
- [Basic Usage](docs/guide/basic-usage.md).
22-
- [User Management](docs/guide/user-management.md).
23-
- [Using Menu](docs/guide/using-menu.md).
24-
- [Api](https://mdmsoft.github.io/yii2-admin/index.html).
23+
## Installation
2524

25+
### Install With Composer
2626

27-
Cara Memasang
28-
-------------
27+
The preferred way to install this extension is through [composer](https://getcomposer.org/download/).
2928

30-
Melalui console:
29+
Either run
3130

3231
```
3332
composer require diecoding/yii2-rbac "dev-master"
3433
```
3534

36-
atau tambahkan:
35+
Or, you may add
3736

3837
```
3938
"diecoding/yii2-rbac": "dev-master"
4039
```
4140

42-
pada baris `require` yang terdapat di berkas `composer.json`. Kemudian jalankan
41+
to the require section of your `composer.json` file and execute `composer update`.
4342

44-
```
45-
composer update
46-
```
43+
### Install From the Archive
4744

45+
Download the latest release from here [releases](https://github.com/die-coding/yii2-rbac/releases), then extract it to your project.
46+
In your application config, add the path alias for this extension.
4847

49-
Guide
50-
-----
51-
52-
- [Instalasi](https://github.com/die-coding/yii2-rbac/blob/master/documentation/installation.md)
53-
- [Konfigurasi](https://github.com/die-coding/yii2-rbac/blob/master/documentation/configuration.md)
54-
- [Menu](https://github.com/die-coding/yii2-rbac/blob/master/documentation/menu.md)
48+
```php
49+
return [
50+
...
51+
'aliases' => [
52+
'@diecoding/rbac' => 'path/to/your/extracted',
53+
// for example
54+
// '@diecoding/rbac' => '@app/extensions/diecoding/yii2-rbac-1.0.0',
55+
...
56+
],
57+
...
58+
];
59+
```

documentation/configuration.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Configuration
2+
3+
## Setup Config Application
4+
5+
Update to your `config`
6+
7+
```php
8+
'components' => [
9+
...
10+
'authManager' => [
11+
'class' => 'yii\rbac\DbManager',
12+
],
13+
...
14+
],
15+
...
16+
'modules' => [
17+
...
18+
'setup-rbac' => [
19+
'class' => 'diecoding\rbac\Module',
20+
],
21+
...
22+
],
23+
```
24+
25+
Then Execute yii migration here: `yii migrate --migrationPath=@yii/rbac/migrations`
26+
27+
To use menu manager (optional). Execute yii migration here: `yii migrate --migrationPath=@diecoding/rbac/migrations`
28+
29+
See [Yii RBAC](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#role-based-access-control-rbac) for more detail. You can then access Auth manager through the following URL:
30+
31+
```
32+
http://localhost/path/to/index.php?r=setup-rbac
33+
http://localhost/path/to/index.php?r=setup-rbac/user
34+
http://localhost/path/to/index.php?r=setup-rbac/assignment
35+
http://localhost/path/to/index.php?r=setup-rbac/role
36+
http://localhost/path/to/index.php?r=setup-rbac/permission
37+
http://localhost/path/to/index.php?r=setup-rbac/route
38+
http://localhost/path/to/index.php?r=setup-rbac/rule
39+
http://localhost/path/to/index.php?r=setup-rbac/menu
40+
```
41+
42+
## Access Control Filter
43+
44+
Access Control Filter (ACF) is a simple authorization method that is best used by applications that only need some simple access control.
45+
As its name indicates, ACF is an action filter that can be attached to a controller or a module as a behavior.
46+
ACF will check a set of access rules to make sure the current user can access the requested action.
47+
48+
The code below shows how to use ACF which is implemented as `diecoding\rbac\AccessControl`:
49+
50+
> **Note:** Please put it in the application config, not the common config.
51+
52+
```php
53+
...
54+
'as access' => [
55+
'class' => 'diecoding\rbac\components\AccessControl',
56+
'allowActions' => [
57+
// '*',
58+
'site/*',
59+
'setup-rbac/*',
60+
'some-controller/some-action',
61+
// The actions listed here will be allowed to everyone including guests.
62+
// So, 'setup-rbac/*' should not appear here in the production, of course.
63+
// But in the earlier stages of your development, you may probably want to
64+
// add a lot of actions here until you finally completed setting up rbac,
65+
// otherwise you may not even take a first step.
66+
],
67+
],
68+
...
69+
```
70+
71+
## Filter ActionColumn Buttons
72+
73+
When you use `GridView`, you can also filtering button visibility.
74+
75+
```php
76+
use mdm\admin\components\Helper;
77+
78+
'columns' => [
79+
...
80+
[
81+
'class' => 'yii\grid\ActionColumn',
82+
'template' => Helper::filterActionColumn('{view}{delete}{posting}'),
83+
]
84+
]
85+
```
86+
87+
It will check authorization access of button and show or hide it.
88+
89+
To check access for route, you can use
90+
91+
```php
92+
use mdm\admin\components\Helper;
93+
94+
if(Helper::checkRoute('delete')){
95+
echo Html::a(Yii::t('rbac-admin', 'Delete'), ['delete', 'id' => $model->name], [
96+
'class' => 'btn btn-danger',
97+
'data-confirm' => Yii::t('rbac-admin', 'Are you sure to delete this item?'),
98+
'data-method' => 'post',
99+
]);
100+
}
101+
102+
```

documentation/installation.md

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

documentation/menu.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Using Menu
2+
3+
Menu manager used for build hierarchical menu. This is automatically look of user
4+
role and permission then return menus that he has access.
5+
6+
```php
7+
use diecoding\rbac\helpers\MenuHelper;
8+
use yii\bootstrap\Nav;
9+
10+
if (!Yii::$app->user->isGuest) {
11+
$userId = Yii::$app->user->id;
12+
echo Nav::widget([
13+
'items' => MenuHelper::getList($userId),
14+
]);
15+
}
16+
```
17+
18+
Return of `diecoding\rbac\helpers\MenuHelper::getList($userId)` has default format like:
19+
20+
```php
21+
[
22+
[
23+
'label' => $menu['name'],
24+
'url' => [$menu['route']],
25+
'icon' => $menu['icon'],
26+
'visible' => $menu['visible'],
27+
'linkOptions' => $menu['options'],
28+
'items' => [
29+
[
30+
'label' => $menu['name'],
31+
'url' => [$menu['route']],
32+
'icon' => $menu['icon'],
33+
'visible' => $menu['visible'],
34+
'linkOptions' => $menu['options'],
35+
],
36+
....
37+
]
38+
],
39+
...
40+
]
41+
```
42+
43+
where `$menu` variable correspond with a record of table `menu`. You can customize return format of `diecoding\rbac\helpers\MenuHelper::getList($userId)` by provide a callback to this method.
44+
The callback must have format `function ($menu) {}`.
45+
46+
For example, you just get output from menu like this:
47+
48+
```php
49+
[
50+
[
51+
'label' => $menu['name'],
52+
'url' => [$menu['route']],
53+
'items' => [
54+
[
55+
'label' => $menu['name'],
56+
'url' => [$menu['route']],,
57+
],
58+
....
59+
]
60+
],
61+
...
62+
]
63+
```
64+
65+
and then in Your view:
66+
67+
```php
68+
$callback = function($menu){
69+
// $data = eval($menu['data']); ['data'] column for more information to this menu
70+
return [
71+
'label' => $menu['name'],
72+
'url' => [$menu['route']],
73+
'items' => $menu['children']
74+
];
75+
}
76+
77+
$items = MenuHelper::getList(Yii::$app->user->id, null, $callback);
78+
```
79+
80+
Default result is get from `cache`. If you want to force regenerate, provide boolean `true` as forth parameter.
81+
82+
You can modify callback function for advanced usage.
83+
84+
> **Note:** You can use the documentation from [https://github.com/mdmsoft/yii2-admin/blob/master/docs/guide/using-menu.md](https://github.com/mdmsoft/yii2-admin/blob/master/docs/guide/using-menu.md) and change the `mdm\admin\components\MenuHelper::getAssignedMenu($userId)` to `diecoding\rbac\helpers\MenuHelper::getList($userId)`

src/helpers/MenuHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
* return [
2929
* 'label' => $menu['name'],
3030
* 'url' => [$menu['route']],
31+
* 'icon' => $menu['icon'],
3132
* 'visible' => $menu['visible'],
32-
* 'options' => $menu['options'],
33+
* 'linkOptions' => $menu['options'],
3334
* 'items' => $menu['children']
3435
* ]
3536
* ]
@@ -59,8 +60,9 @@ class MenuHelper
5960
* return [
6061
* 'label' => $menu['name'],
6162
* 'url' => [$menu['route']],
63+
* 'icon' => $menu['icon'],
6264
* 'visible' => $menu['visible'],
63-
* 'options' => $menu['options'],
65+
* 'linkOptions' => $menu['options'],
6466
* 'items' => $menu['children']
6567
* ]
6668
* ]

0 commit comments

Comments
 (0)