antonyz89 / yii2-rbac
Yii2 框架的 RBAC
Requires
- php: >=7.2
- antonyz89/yii2-many-to-many: ^0.3
- kartik-v/yii2-grid: ^3.2
- kartik-v/yii2-widget-select2: ^2.1.0
- rawr/t-regx: ^0.9
- symfony/polyfill-php80: ^1.18
- yiisoft/yii2: ~2.0.6
- yiisoft/yii2-bootstrap4: ^2.0
This package is auto-updated.
Last update: 2023-01-14 18:09:41 UTC
README
安装
通过 composer 安装此扩展是首选方式。
运行以下命令之一
$ composer require antonyz89/yii2-rbac dev-master
或
"antonyz89/yii2-rbac": "dev-master"
将以下内容添加到你的 composer.json
文件的 require 部分中。
用法
$ php yii migrate/up --migrationPath=@antonyz89/rbac/migrations
将 bootstrap 和 module 添加到 main.php
return [ 'bootstrap' => ['rbac'], 'modules' => [ 'rbac' => ['class' => 'antonyz89\rbac\Module'], ], ]
应用规则
1 - 将 AccessControl 添加到你的控制器行为中
use antonyz89\rbac\components\AccessControl; /** * ExampleController implements the CRUD actions for Example model. */ class ExampleController extends Controller { /** * {@inheritdoc} */ public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, /* add */ /* optional fields */ 'rules' => [ [ 'actions' => [ 'create', 'update', 'view', 'index', ], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], ], ]; } }
2 - 为你的身份类创建一个 rbac_profile_id
php yii migrate/create add_rbac_profile_id_to_user_table
<?php use yii\db\Migration; /** * Class m200808_033148_add_rbac_profile_id_to_user_table */ class m200808_033148_add_rbac_profile_id_to_user_table extends Migration { /** * {@inheritdoc} */ public function safeUp() { $this->addColumn('{{%user}}', 'rbac_profile_id', $this->integer()->notNull()->after('id')); $this->createIndex('idx-user-rbac_profile_id', '{{%user}}', 'rbac_profile_id'); $this->addForeignKey('fk-user-rbac_profile_id', '{{%user}}', 'rbac_profile_id', '{{%rbac_profile}}', 'id', 'CASCADE', 'CASCADE'); } /** * {@inheritdoc} */ public function safeDown() { $this->dropForeignKey('fk-user-rbac_profile_id', '{{%user}}'); $this->dropIndex('idx-user-rbac_profile_id', '{{%user}}'); $this->dropColumn('{{%user}}', 'rbac_profile_id'); } }
3 - 使用 php yii migrate
3.1 - 现在,在 _form.php
和你的身份类的 Class
中包含它。
3.2 - 添加 getRbacProfile()
/** * @return \yii\db\ActiveQuery|\antonyz89\rbac\models\query\RbacProfileQuery */ public function getRbacProfile() { return $this->hasOne(\antonyz89\rbac\models\RbacProfile::class, ['id' => 'rbac_profile_id']); }
3.3 - 在 rules()
中包含它
//rbac_profile_id ['rbac_profile_id', 'required'], ['rbac_profile_id', 'integer'], ['rbac_profile_id', 'exist', 'skipOnError' => true, 'targetClass' => RbacProfile::class, 'targetAttribute' => ['rbac_profile_id' => 'id']],
4 - 访问 https:///rbac
或 https://?r=rbac
并创建一个包含你希望具有此配置文件的任何人可以访问的控制器和动作的配置文件
5 - 当你的身份尝试访问其配置文件中的现有控制器/动作时,不会发生任何事,但如果尝试访问其配置文件中不存在的控制器/动作,则会出现错误
错误:在访问 https:///category/update?id=16 时
--
如果你使用 AdminLTE,可以使用我修改的菜单小部件,该小部件与 RBAC 一起使用,并根据登录身份的配置文件中的控制器/动作隐藏/显示菜单,同时支持类似于 "@" 和 "?" 的角色
<?php use antonyz89\rbac\widgets\Menu; ?> <?php echo Menu::widget( [ 'options' => ['class' => 'sidebar-menu tree', 'data-widget' => 'tree'], 'items' => [ [ 'label' => 'Dashboard', 'icon' => 'dashboard', 'url' => ['site/index'], 'role' => ['@'], ], /* A button can disappear if Identity's Profile don't have this Controller and Action */ [ 'label' => 'Administrators', 'icon' => 'lock', 'url' => ['admin/index'], 'role' => ['@'] ] ] ] ); ?>
通过点击“生成迁移”按钮,你可以使用你的当前 RBAC 数据生成迁移
迁移可以在 'console/migrations' 中找到,并使用 php yii migrate
运行
条件块
你可以为配置文件中添加的每个控制器创建一个块。每个块可以包含一个特定条件,以允许用户访问该控制器及其操作。
在这种情况下,所有 user_level = 2
或 email = 'example@mail.com'
的用户都可以访问此块。