funson86 / yii2-auth
带有操作类别的Yii2 Auth
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-15 12:58:02 UTC
README
Yii2 Auth 可以管理类别的操作,你可以将其集成到 Yii2 Adminlte
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require funson86/yii2-auth "dev-master"
或者
"funson86/yii2-auth": "*"
将以下内容添加到你的 composer.json
文件的 require 部分。
用法
扩展安装完成后,只需在你的代码中通过以下方式使用它
迁移
运行迁移
yii migrate --migrationPath=@funson86/auth/migrations
或者手动插入到 setting
表。
CREATE TABLE `auth_operation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` int(11) NOT NULL DEFAULT 0, `name` varchar(32) NOT NULL, PRIMARY KEY (`id`), KEY `parent_id` (`parent_id`) ) ENGINE=InnoDB AUTO_INCREMENT=100000 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Auth Operation'; CREATE TABLE `auth_role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, `description` varchar(255) DEFAULT NULL, `operation_list` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Auth Role'; INSERT INTO `auth_role` VALUES ('1', 'Super Admin', '', 'all'); INSERT INTO `auth_role` VALUES ('3', 'Normal Admin', '', 'backendLogin;viewUser;viewRole'); INSERT INTO `auth_operation` VALUES ('111', '0', 'basic'); INSERT INTO `auth_operation` VALUES ('113', '0', 'user'); INSERT INTO `auth_operation` VALUES ('114', '0', 'role'); INSERT INTO `auth_operation` VALUES ('11101', '111', 'backendLogin'); INSERT INTO `auth_operation` VALUES ('11302', '113', 'viewUser'); INSERT INTO `auth_operation` VALUES ('11303', '113', 'createUser'); INSERT INTO `auth_operation` VALUES ('11304', '113', 'updateUser'); INSERT INTO `auth_operation` VALUES ('11305', '113', 'deleteUser'); INSERT INTO `auth_operation` VALUES ('11402', '114', 'viewRole'); INSERT INTO `auth_operation` VALUES ('11403', '114', 'createRole'); INSERT INTO `auth_operation` VALUES ('11404', '114', 'updateRole'); INSERT INTO `auth_operation` VALUES ('11405', '114', 'deleteRole'); ALTER TABLE `user` ADD COLUMN `auth_role` int(11) AFTER `status`; UPDATE `user` set `auth_role`=3; UPDATE `user` set `auth_role`=1 where `username` = 'admin';
配置 /common/config/main.php 以检查权限
添加以下代码后,控制器中的每个操作中的代码 if(!Yii::$app->user->can('createRole')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); 将检查当前用户是否有执行此操作的权限。
'components' => [ 'user' => [ 'class' => 'funson86\auth\User', 'enableAutoLogin' => true, ], ],
在后台进行CRUD认证角色与操作
在 backend/config/main.php 中配置后台模块以管理设置
'modules' => [ 'auth' => [ 'class' => 'funson86\auth\Module', ], ],
访问后台URL: http://you-domain/backend/web/auth
添加您的认证操作
您可以通过迁移或手动将数据插入到 auth_operation
表中来根据业务添加新的操作。
INSERT INTO `auth_operation` VALUES ('115', '0', 'Service'); INSERT INTO `auth_operation` VALUES ('11501', '115', 'viewService');
使用您的操作检查
一旦您在 auth_operation
中添加了新的操作,就像以下这样,在控制器动作的开头添加 if (!Yii::$app->user->can('viewService')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'))
class ServiceController extends Controller { public function actionView($id) { if (!Yii::$app->user->can('viewService')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); // you business code } }
翻译
如果没有翻译,它将在后台管理中显示 'viewService'。您可以在后台的 messages/en/auth.php 或 messages/zh-CN/auth.php 中添加翻译。如下所示
return [ 'viewService' => 'View Service', ];