yiisoft/rbac-db

Yii RBAC 数据库存储

2.0.0 2024-03-07 10:54 UTC

This package is auto-updated.

Last update: 2024-09-22 10:20:04 UTC


README

Yii

Yii RBAC 数据库


Latest Stable Version Total Downloads Build status codecov Mutation testing badge static analysis type-coverage

该包为 Yii 数据库 提供 Yii RBAC 的存储。

详细的构建状态

要求

  • PHP 8.1 或更高版本。
  • PDO PHP 扩展。
  • 以下驱动器之一
  • 所选驱动器的 PDO PHP 扩展。
  • 在与其他 SQL Server 一起使用的情况下,所需的最小 PDO 版本为 5.11.1。

安装

可以使用 Composer 安装此包

composer require yiisoft/rbac-db

通用用法

配置数据库连接

配置取决于所选驱动器。以下是 PostgreSQL 的示例

use Yiisoft\Cache\ArrayCache; // Requires https://github.com/yiisoft/cache
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Pgsql\Connection;
use Yiisoft\Db\Pgsql\Driver;

$pdoDriver = new Driver('pgsql:host=127.0.0.1;dbname=yiitest;port=5432', 'user', 'password');
$pdoDriver->charset('UTF8');
$connection = Connection(
    $pdoDriver, 
    new SchemaCache(
        new ArrayCache(), // Any other PSR-16 compatible cache can be used.
    )
);

更全面的示例可以在 Yii 数据库文档 中找到。

处理迁移

此包使用 Yii DB 迁移 来管理存储所需的数据表。总共有三个表(使用 yii_rbac_ 前缀)。

项目存储

  • yii_rbac_item.
  • yii_rbac_item_child.

分配存储

  • yii_rbac_assignment.

配置迁移

请确保将这些目录作为源路径包含在内

当使用 Yii Console 时,将此添加到 config/params.php

'yiisoft/db-migration' => [
    // ...
    'sourcePaths' => [
        dirname(__DIR__) . '/vendor/yiisoft/rbac-db/migrations/items',
        dirname(__DIR__) . '/vendor/yiisoft/rbac-db/migrations/assignments',
    ],
],

并将数据库连接配置从 上一节 添加到 DI 容器 config/common/db.php

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Pgsql\Connection as PgsqlConnection;

return [
    ConnectionInterface::class => [
        'class' => PgsqlConnection::class,
        '__construct()' => [
            // ...
        ],
    ]
];

由于项目和分配存储是完全独立的,因此迁移也被分开,以防止创建未使用的表。例如,如果您只想使用分配存储,请仅将 migrations/assignments 添加到源路径。

有关迁移的其他使用方式,请参阅 此处

应用迁移

使用 Yii Console

./yii migrate:up

有关迁移的其他使用方式,请参阅 此处

回滚迁移

使用 Yii Console

./yii migrate:down --limit=2

有关迁移的其他使用方式,请参阅 此处

使用存储

存储不打算直接使用。相反,请使用来自 Yii RBAC 包的 Manager

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Rbac\Db\AssignmentsStorage;
use Yiisoft\Rbac\Db\ItemsStorage;
use Yiisoft\Rbac\Db\TransactionalManagerDecorator;
use Yiisoft\Rbac\Manager;
use Yiisoft\Rbac\Permission;
use Yiisoft\Rbac\RuleFactoryInterface;

/** @var ConnectionInterface $database */
$itemsStorage = new ItemsStorage($database);
$assignmentsStorage = new AssignmentsStorage($database);
/** @var RuleFactoryInterface $rulesContainer */
$manager = new TransactionalManagerDecorator(
    new Manager(
        itemsStorage: $itemsStorage, 
        assignmentsStorage: $assignmentsStorage,
        // Requires https://github.com/yiisoft/rbac-rules-container or another compatible factory.
        ruleFactory: $rulesContainer,
    ),
);
$manager->addPermission(new Permission('posts.create'));

注意使用装饰器包装管理器——它还提供了数据库事务,以确保数据完整性。

请注意,没有必要同时使用 DB 存储器。结合不同的实现是可能的。一个相当常见的案例是,通过 PHP 文件 管理项目,同时在数据库中存储分配。

更多示例可以在 Yii RBAC 文档中找到。

手动同步存储

由于管理器的存在,存储保持同步,但在某些情况下可能需要手动同步。其中一种情况是使用基于PHP文件存储的组合,并手动编辑它

假设PHP文件用于项目,而数据库用于任务分配,并且一些项目已被删除

return [
    [
        'name' => 'posts.admin',        
        'type' => 'role',        
        'created_at' => 1683707079,
        'updated_at' => 1683707079,
        'children' => [
            'posts.redactor',
            'posts.delete',
            'posts.update.all',
        ],
    ],
-   [
-       'name' => 'posts.redactor',
-       'type' => 'role',        
-       'created_at' => 1683707079,
-       'updated_at' => 1683707079,
-       'children' => [
-           'posts.viewer',
-           'posts.create',
-           'posts.update',
-       ],
-   ],
    [
        'name' => 'posts.viewer',
        'type' => 'role',        
        'created_at' => 1683707079,
        'updated_at' => 1683707079,
        'children' => [
            'posts.view',
        ],
    ],
    [
        'name' => 'posts.view',
        'type' => 'permission',        
        'created_at' => 1683707079,
        'updated_at' => 1683707079,
    ],
    [
        'name' => 'posts.create',
        'type' => 'permission',        
        'created_at' => 1683707079,
        'updated_at' => 1683707079,
    ],
-   [
-       'name' => 'posts.update',
-       'rule_name' => 'is_author',
-       'type' => 'permission',
-       'created_at' => 1683707079,
-       'updated_at' => 1683707079,
-   ],
    [
        'name' => 'posts.delete',        
        'type' => 'permission',        
        'created_at' => 1683707079,
        'updated_at' => 1683707079,
    ],
    [
        'name' => 'posts.update.all',
        'type' => 'permission',        
        'created_at' => 1683707079,
        'updated_at' => 1683707079,
    ],
];

那么在其他存储中的相关条目也需要被删除。这可以在迁移中完成

use Yiisoft\Db\Migration\MigrationBuilder;
use Yiisoft\Db\Migration\RevertibleMigrationInterface;
use Yiisoft\Db\Migration\TransactionalMigrationInterface;

final class M240229184400DeletePostUpdateItems implements RevertibleMigrationInterface, TransactionalMigrationInterface
{
    private const TABLE_PREFIX = 'yii_rbac_';
    private const ASSIGNMENTS_TABLE = self::TABLE_PREFIX . 'assignment';
    
    public function up(MigrationBuilder $b): void
    {
        $b
            ->getDb()
            ->createCommand()
            ->delete(self::ASSIGNMENTS_TABLE, ['item_name' => ['posts.redactor', 'posts.update']])
            ->execute();
    }
    
    public function down(MigrationBuilder $b): void; 
    {        
    }   
}

文档

如果您需要帮助或有疑问,Yii 论坛 是一个不错的选择。您还可以查看其他 Yii 社区资源

许可

Yii RBAC 数据库是自由软件。它根据BSD许可条款发布。有关更多信息,请参阅 LICENSE

Yii 软件 维护。

支持项目

Open Collective

关注更新

Official website Twitter Telegram Facebook Slack