dmstr / yii2-rbac-migration
_____
2.0.1
2022-07-07 14:45 UTC
Requires
- php: ^7.3 || ^8.0
- yiisoft/yii2: ^2.0.35
Requires (Dev)
- phan/phan: ^2.4
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-07 19:54:01 UTC
README
此迁移允许定义rbac项目结构的期望“状态”。
使用多维数组定义迁移后期望的项目结构应如何显示。
遵循puppet配置的风格,定义项目期望或预期状态的主体属性是ensure
参数。
由于此类迁移更改与安全性相关的数据,建议定义防御性配置。
安装
composer require dmstr/yii2-rbac-migration
示例用法
<?php use dmstr\rbacMigration\Migration; use yii\rbac\Item; class m000000_000000_my_example_migration extends Migration { // define default params for al items public $defaultFlags = [ 'ensure' => self::PRESENT, 'replace' => false, ]; public $privileges = [ [ 'name' => 'Role1', 'type' => Item::TYPE_ROLE, 'description' => 'My custom description', 'ensure' => self::PRESENT, 'replace' => true, 'children' => [ [ 'name' => 'permission1', 'type' => Item::TYPE_PERMISSION, 'rule' => [ 'name' => 'Rule0', 'class' => some\namespaced\Rule::class ] ], [ 'name' => 'permission2', 'type' => Item::TYPE_PERMISSION, 'ensure' => self::MUST_EXIST ], [ 'name' => 'Role1', 'ensure' => self::PRESENT, 'children' => [ [ 'name' => 'permission3', 'type' => Item::TYPE_PERMISSION ] ] ] ] ], [ 'name' => 'permission3', 'type' => Item::TYPE_PERMISSION, 'ensure' => self::ABSENT ], ]; }
每个特权项目的配置参数
- 所有项目的默认参数通过
protected $_defaultFlagsStruct
数组定义 - 默认参数可以通过
public $defaultFlags
在每个迁移实例中定义/覆盖 - 直接在项目中设置的参数具有最高优先级
按项目合并定义的参数
ensure
参数的有效标志
self::ABSENT
的提示
- 如果将其定义为项目参数,则无论其位置如何,都将删除项目。
- 因此,如果您在子项目中定义
ensure => self::ABSENT
,则不仅会删除子关系,还会删除项目! - 如果认证项目已在数据库中定义,并且认证表具有级联外键,则数据库可能会删除此项目的子关系。
规则的提示
- 如果定义了规则,则按名称分配给项目。
- 如果不存在,则将创建具有给定类属性的规则。
- 如果已存在具有给定名称的规则且将
replace
设置为true
,则规则将被更新;否则,将使用现有规则。
弃用的项目参数
_exists
和_force
参数已弃用,但仍有效,将被新参数方案替换
批量赋值的快捷语法
为了能够快速定义批量赋值,可以使用特殊快捷语法,其中项目仅是一个字符串。该字符串将设置为主['name']属性,所有其他参数均从(定义的)默认值中获取。
示例
public $defaultFlags = [ 'type' => Item::TYPE_PERMISSION, 'ensure' => self::MUST_EXIST, 'replace' => false, ]; public $privileges = [ [ 'name' => 'PublicationEditor', 'type' => Item::TYPE_ROLE, 'ensure' => self::PRESENT, 'replace' => true, 'description' => 'Create, edit, delete publication items.', 'children' => [ 'publication_default', 'publication_crud_index', 'publication_crud_publication-item_create', 'publication_crud_publication-item_delete', 'publication_crud_publication-item_index', 'publication_crud_publication-item_update', 'publication_crud_publication-item_view', ] ] ];
safeDown()
这些认证迁移不能被回滚。
由dmstr构建