根據yii2-admin(
https://github.com/mdmsoft/yi...)修改,yii2-admin只是web方式,這個至關於yii2-admin的rest版本。適合用於先後端分離項目,rest方式提供接口,實現對接口的權限控制。
項目地址:
https://github.com/windhoney/...
yii2-rest-rbac
Yii2權限系統,rest版,根據
yii2-admin(https://github.com/mdmsoft/yii2-admin)修改
composer require windhoney/yii2-rest-rbac
使用
配置oauth2和rbac
'modules' => [
'rbac' => [
'class' => 'wind\rest\modules'
],
'oauth2' => [
'class' => 'filsh\yii2\oauth2server\Module',
'tokenParamName' => 'access_token',
'tokenAccessLifetime' => 3600 * 24,
'storageMap' => [
'user_credentials' => 'backend\models\User',
],
'grantTypes' => [
'user_credentials' => [
'class' => 'OAuth2\GrantType\UserCredentials',
],
'client_credentials' => [
'class' => 'OAuth2\GrantType\ClientCredentials',
],
'refresh_token' => [
'class' => 'OAuth2\GrantType\RefreshToken',
'always_issue_new_refresh_token' => true
],
'authorization_code' => [
'class' => 'OAuth2\GrantType\AuthorizationCode'
],
],
'components' => [
'request' => function () {
return \filsh\yii2\oauth2server\Request::createFromGlobals();
},
'response' => [
'class' => \filsh\yii2\oauth2server\Response::class,
],
],
]
],
'components' => [
'authManager' => [
'class' => 'wind\rest\components\DbManager', //配置文件
],
]
配置權限
'as access' => [
'class' => 'wind\rest\components\AccessControl',
'allowActions' => [
'site/*',//容許訪問的節點,可自行添加
'rbac/menu/user-menu',
'oauth2/*',
]
],
建立所須要的表
1. 用戶表user和菜單表menu
yii migrate --migrationPath=@vendor/windhoney/yii2-rest-rbac/migrations
2. rbac相關權限表
yii migrate --migrationPath=@yii/rbac/migrations/
auth_item
表添加一個字段
parent_name
varchar(30) COLLATE utf8_unicode_ci DEFAULT '' COMMENT '父級名稱',
3. oauth2相關表
yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations
4. 新增分組表
CREATE TABLE `auth_groups` (
`group_id` varchar(50) NOT NULL COMMENT '分組id',
`group_name` varchar(100) NOT NULL DEFAULT '' COMMENT '分組名稱',
`group_status` varchar(50) NOT NULL DEFAULT '' COMMENT '狀態(開啓,關閉)',
PRIMARY KEY (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='分組';
CREATE TABLE `auth_groups_child` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` varchar(50) NOT NULL COMMENT '分組id',
`user_id` varchar(64) NOT NULL COMMENT '用戶id',
PRIMARY KEY (`id`),
UNIQUE KEY `group_id_2` (`group_id`,`user_id`),
KEY `group_id` (`group_id`),
KEY `user_group_id` (`user_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=795 DEFAULT CHARSET=utf8 COMMENT='分組子集';
添加路由配置
- 將yii2-rest-rbac/example/rbac_route.php文件內容配置到項目的urlManager的rules規則下
- 或者在main.php文件中 添加
$dir = __DIR__ . "/route";
$main = wind\rest\helper\RbacHelper::addRoute($dir, $main);
return $main;
並將此文件放到config/route/rbac_route.php
接口文檔參考