yii2 0 最簡單rbac實現方法

2021-08-04 12:36:41 字數 4269 閱讀 3125

許可權設定

公司角色:銷售,專案經理,人事,老闆

公司成員:小銷是銷售,小項是專案經理,小李是人事,老雷是老闆

系統裡面有menu:客戶管理,專案管理,人事管理

需求描述:銷售客戶訪問客戶管理,專案經理可以訪問專案管理,人事可以訪問人事管理,老闆都可以訪問

實現過程:

第一步:生成rbac相關的表(mysql為例)

/**

* database schema required by \yii\rbac\dbmanager.

* * @author qiang xue * @author alexander kochetov *

@link

* @license license/

* @since 2.0

*/drop table if

exists

`auth_assignment`;

drop table if

exists

`auth_item_child`;

drop table if

exists

`auth_item`;

drop table if

exists

`auth_rule`;

create table `auth_rule`

( `name` varchar(64) not

null,

`data` text,

`created_at` integer,

`updated_at` integer,

primary

key (`name`)

) engine innodb;

create table `auth_item`

( `name` varchar(64) not

null,

`type` integer not

null,

`description` text,

`rule_name` varchar(64),

`data` text,

`created_at` integer,

`updated_at` integer,

primary

key (`name`),

foreign key (`rule_name`) references `auth_rule` (`name`) on

delete

setnull

onupdate cascade,

key`type` (`type`)

) engine innodb;

create table `auth_item_child`

( `parent` varchar(64) not

null,

`child` varchar(64) not

null,

primary

key (`parent`, `child`),

foreign key (`parent`) references `auth_item` (`name`) on

delete cascade on

update cascade,

foreign key (`child`) references `auth_item` (`name`) on

delete cascade on

update cascade

) engine innodb;

create table `auth_assignment`

( `item_name` varchar(64) not

null,

`user_id` varchar(64) not

null,

`created_at` integer,

primary

key (`item_name`, `user_id`),

foreign key (`item_name`) references `auth_item` (`name`) on

delete cascade on

update cascade

) engine innodb;

第二步:配置authmanager的元件

'authmanager' => [            

'class' => 'yii\rbac\dbmanager',

],

第三步:生成角色

$authmanager = yii:

:$ceo = $authmanager->createrole('ceo');

$authmanager->add($ceo);

$humanresource = $authmanager->createrole('humanresource');

$authmanager->add($humanresource);

$projectmanager = $authmanager->createrole('projectmanager');

$authmanager->add($projectmanager);

$sales = $authmanager->createrole('sales');

$authmanager->add($sales);

// 並且建立好等級關係,ceo是sales,projectmanager,humanresource的上司

$authmanager->addchild($ceo, $sales);

$authmanager->addchild($ceo, $projectmanager);

$authmanager->addchild($ceo, $humanresource);

第三步:建立相關許可權

$auth = yii:

:$humanresourcemenu = $auth->createpermission('humanresourcemenu');

$auth->add($humanresourcemenu);

$customermenu = $auth->createpermission('customermenu');

$auth->add($customermenu);

$projectmanagermenu = $auth->createpermission('projectmanagermenu');

$auth->add($projectmanagermenu);

第四步:將相關的許可權分配給相關的角色

$authmanager->addchild($sales, $customermenu);

$authmanager->addchild($projectmanager, $projectmanagermenu);

$authmanager->addchild($humanresource, $humanresourcemenu);

到此為止,rbac的許可權已經完全配置好了,剩下的事情就是給每個人分配相關的角色即可了

第五部:每個人分配相關的角色

將小銷分配乙個sales角色,小項分配乙個projectmanager的角色,小李分配乙個humanresource的角色,老雷分配乙個ceo的角色

$authmanager->assign($ceo,$userid); // 老雷的userid,

$authmanager->assign($humanresource,$userid); // 小李的userid,

...以此類推

rbac許可權系統已經構建完成,剩下的事情就是使用了

第六步:使用rbac

<?php

客戶管理

<?php

endif;?>

<?php

專案管理

<?php

endif;?>

<?php

人事管理

<?php

endif;?>

Yii2 0中實現規劃任務

size x large size large 1.在 console controllers 資料夾下建立乙個把握器 namespace console controllers use yii console controller test controller class testcontrol...

Yii2 0實現AJAX搜尋 分頁

1.首先判斷搜尋的資料是否為空 php view plain copy 2.定義乙個where條件 目的是讓sql語句 恆成立 php view plain copy where 1 3.判斷資料是否存在,拼接搜尋的語句。如果多條件搜尋,則直接 and 連線 即可 php view plain co...

Yii2 0建立公共方法簡單示例

因為之前寫專案都是用的thinkphp,公共方法都寫在tp自帶的common下面的function裡面,初次接觸yii框架發現它自身沒帶這個機制。在專案中,很多地方都需要公共方法的存在,以提高 復用性,減少重複開發的時間,那yii2如何定義自己的公共方法呢?以yii2 advanced版本為基礎來進...