rbac php yii,权限管理(RBAC)扩展 yii2-admin

简介

Yii2-Admin是一套基于RBAC的权限管理扩展组件,通过简单的安装配置即可完成一整套的权限管理机制,非常方便!本文酱油君将为大家介绍这套实用的扩展

下载安装

通过composer安装

注意:安装之前先确定composer是不是最新的:

如果提示长时间未更新通过下面的命令更新composercomposer self-update

composer install

下载yii2-admin:composer require mdmsoft/yii2-admin "~2.0"

提示:composer不会的童鞋可以看下composer教程 或者网上查一下资料

配置

在相应的配置文件中添加以下配置:

以高级版为例:backend/config/main.php'modules' => [

'admin' => [

'class' => 'mdm\admin\Module',

'layout' => 'left-menu',         //yii2-admin的导航菜单

],

],

'aliases' => [

'@mdm/admin' => '@vendor/mdmsoft/yii2-admin',

],

'as access' => [

'class' => 'mdm\admin\components\AccessControl',

'allowActions' => [

'admin/*',            //配置允许权限

]

],

配置RBAC

注意:如果已经安装和配置了RBAC可以跳过这步:

在应用配置文件中配置 authManager:return [

// ...

'components' => [

'authManager' => [

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

'defaultRoles' => ['guest'],

],

// ...

],

];

DbManager 使用4个数据库表存放它的数据:yii\rbac\DbManager::$itemTable: 该表存放授权条目(译者注:即角色和权限)。默认表名为 "auth_item" 。

yii\rbac\DbManager::$itemChildTable: 该表存放授权条目的层次关系。默认表名为 "auth_item_child"。

yii\rbac\DbManager::$assignmentTable: 该表存放授权条目对用户的指派情况。默认表名为 "auth_assignment"。

yii\rbac\DbManager::$ruleTable: 该表存放规则。默认表名为 "auth_rule"。

继续之前,你需要在数据库中创建这些表。你可以使用存放在 @yii/rbac/migrations 目录中的数据库迁移文件来做这件事(译者注:根据本人经验,最好是将授权数据初始化命令也写到这个 RBAC 数据库迁移文件中):

yii migrate --migrationPath=@yii/rbac/migrations

现在可以通过 \Yii::$app->authManager 访问 authManager 。

或者直接导入sqldrop table if exists `menu`;

drop table if exists `user` cascade;

create table `menu`

(

`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,

`name` varchar(128),

`parent` int(11),

`route` varchar(256),

`order` int(11),

`data`   blob,

foreign key (`parent`) references `menu`(`id`)  ON DELETE SET NULL ON UPDATE CASCADE

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table `user`

(

`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,

`username` varchar(32) NOT NULL,

`auth_key` varchar(32) NOT NULL,

`password_hash` varchar(256) NOT NULL,

`password_reset_token` varchar(256),

`email` varchar(256) NOT NULL,

`status` integer not null default 10,

`created_at` integer not null,

`updated_at` integer not null

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

rbac数据表: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 set null on update 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;

演示

访问地址:注:以下地址为url美化之后的地址,url美化教程

域名/admin/user/index   //用户管理

域名/admin/role/index   //角色管理

域名/admin/permission/index  //权限管理

域名/admin/rule/index  //规则管理

域名/admin/assignment/index  //分配权限

域名/admin/route/index  //路由管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值