Giter VIP home page Giter VIP logo

rbac-cycle-db's Introduction

Yii

Yii RBAC Cycle Database


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

The package provides Cycle Database storage for Yii RBAC.

Detailed build statuses:

RDBMS Status
SQLite SQLite status
MySQL MYSQL status
PostgreSQL MYSQL status
Microsoft SQL Server MYSQL status

Requirements

  • PHP 8.1 or higher.
  • In the case of using with SQLite, a minimal required version is 3.8.3.
  • In the case of using with SQL Server, a minimal required version of PDO is 5.11.1.

Installation

The package could be installed with composer:

composer require yiisoft/rbac-cycle-db

General usage

Configuring database connection

Configuration depends on a selected driver. Here is an example for PostgreSQL:

use Cycle\Database\Config\DatabaseConfig;
use Cycle\Database\Config\Postgres\DsnConnectionConfig;
use Cycle\Database\Config\PostgresDriverConfig;
use Cycle\Database\DatabaseManager;

$dbConfig = new DatabaseConfig(
    [
        'default' => 'default',
        'databases' => [
            'default' => ['connection' => 'pgsql'],
        ],
        'connections' => [
            'pgsql' => new PostgresDriverConfig(new DsnConnectionConfig(
                'pgsql:host=127.0.0.1;dbname=yiitest;port=5432',
                'user',
                'password',
            )),
        ],
    ]
);
$databaseManager = new DatabaseManager($dbConfig);
$database = $databaseManager->database();

More comprehensive examples can be found at Cycle Database docs.

Working with migrations

This package uses Cycle Migrations for managing database tables required for storages. There are three tables in total (yii_rbac_ prefix is used).

Items storage:

  • yii_rbac_item.
  • yii_rbac_item_child.

Assignments storage:

  • yii_rbac_assignment.

Configuring migrator and capsule

use Cycle\Database\DatabaseManager;
use Cycle\Migrations\Capsule;
use Cycle\Migrations\Config\MigrationConfig;
use Cycle\Migrations\FileRepository;
use Cycle\Migrations\Migrator;

$migrationsSubfolders = ['items', 'assignments'];
$directories = array_map(
    static fn (): string => dirname(__DIR__. 2),  "/vendor/yiisoft/rbac-cycle-db/migrations/$subfolder",
    $migrationsSubfolders, 
);
$config = new MigrationConfig([
    'directory' => $directories[0],
    // "vendorDirectories" are specified because the "directory" option doesn't support multiple directories. In the
    // end, it makes no difference because they all will be merged into a single array.
    'vendorDirectories' => $directories[1] ?? [],
    'table' => 'cycle_migration',
    'safe' => true,
]);
/** @var DatabaseManager $databaseManager */
$migrator = new Migrator($config, $databaseManager, new FileRepository($config));
$migrator->configure();

$capsule = new Capsule($databaseManager->database());

For configuring $databaseManager, see previous section.

Because item and assignment storages are completely independent, migrations are separated as well to prevent the creation of unused tables. So, for example, if you only want to use assignment storage, adjust $migrationsSubfolders variable like this:

$migrationsSubfolders = ['assignments'];

Applying migrations

use Cycle\Migrations\Capsule;
use Cycle\Migrations\Migrator;

/**
 * @var Migrator $migrator
 * @var Capsule $capsule 
 */
while ($migrator->run($capsule) !== null) {
    echo "Migration {$migration->getState()->getName()} applied successfully.\n";
}

Reverting migrations

use Cycle\Migrations\Capsule;
use Cycle\Migrations\Migrator;

/**
 * @var Migrator $migrator
 * @var Capsule $capsule 
 */
while ($migrator->rollback($capsule) !== null) {
    echo "Migration {$migration->getState()->getName()} reverted successfully.\n";
}

Using storages

The storages are not intended to be used directly. Instead, use them with Manager from Yii RBAC package:

use Cycle\Database\DatabaseInterface;
use Yiisoft\Rbac\Cycle\AssignmentsStorage;
use Yiisoft\Rbac\Cycle\ItemsStorage;
use Yiisoft\Rbac\Cycle\TransactionalManagerDecorator;
use Yiisoft\Rbac\Manager;
use Yiisoft\Rbac\Permission;
use Yiisoft\Rbac\RuleFactoryInterface;

/** @var DatabaseInterface $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'));

Note wrapping manager with decorator—it additionally provides database transactions to guarantee data integrity.

Note that it's not necessary to use both DB storages. Combining different implementations is possible. A quite popular case is to manage items via PHP file while store assignments in a database.

More examples can be found in Yii RBAC documentation.

Syncing storages manually

The storages stay synced thanks to manager, but there can be situations where you need to sync them manually. One of them is using combination with PHP file based storage and editing it manually.

Let's say PHP file is used for items, while database - for assignments, and some items were deleted:

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,
    ],
];

Then related entries in other storage needs to be deleted as well. This can be done within a migration:

use Cycle\Migrations\Migration;

final class DeletePostUpdateItems extends Migration
{
    private const TABLE_PREFIX = 'yii_rbac_';
    private const ASSIGNMENTS_TABLE = self::TABLE_PREFIX . 'assignment';

    public function up()
    {
        $this
            ->database()
            ->delete()
            ->from(self::ASSIGNMENTS_TABLE)
            ->where('item_name', 'IN', ['posts.redactor', 'posts.update']);
    }

    public function down()
    {
    }
}

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii RBAC Cycle Database is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

rbac-cycle-db's People

Contributors

arogachev avatar dependabot[bot] avatar devanych avatar luizcmarin avatar razonyang avatar rustamwin avatar samdark avatar terabytesoftw avatar thenotsoft avatar viktorprogger avatar vjik avatar xepozz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rbac-cycle-db's Issues

userHasPermission return false for readPost

What steps will reproduce the problem?

    $manager->addPermission(new Permission('createPost'));
    $manager->addPermission(new Permission('readPost'));
    $manager->addPermission(new Permission('deletePost'));

    $manager->addRole(new Role('author'));
    $manager->addRole(new Role('reader'));

    $manager->addChild('reader', 'readPost');
    $manager->addChild('author', 'createPost');
    $manager->addChild('author', 'deletePost');
    $manager->addChild('author', 'reader');

    $manager->assign('author', 3);

    echo $manager->userHasPermission(3, 'createPost') ? '1' : '0';
    echo $manager->userHasPermission(3, 'deletePost') ? '1' : '0';
    echo $manager->userHasPermission(3, 'readPost') ? '1' : '0';

What is the expected result?

111

What do you get instead?

110

Additional info

Q A
Version 1.0.?
PHP version 8.2
Operating system Ubuntu

PHP Fatal error: Uncaught PDOException: SQLSTATE[22008]: Datetime field overflow: 7 ERROR: date/time field value out of range: "1678513615"

PHP 8.2
PostgreSQL

$itemsStorage= new ItemsStorage(ITEMS_TABLE,$database);
$assignmentsStorage=new AssignmentsStorage(ASSIGNMENTS_TABLE,$database);
$manager = new Manager($itemsStorage, $assignmentsStorage, new RulesFactory());
$manager->addPermission(new Permission('createPost'));
PHP Fatal error:  Uncaught PDOException: SQLSTATE[22008]: Datetime field overflow: 7 ERROR:  date/time field value out of range: "1678513615"
HINT:  Perhaps you need a different "datestyle" setting.
CONTEXT:  unnamed portal parameter $5 = '...' in /home/idk/Code/rbac/vendor/cycle/database/src/Driver/Driver.php:444
Stack trace:
#0 /home/idk/Code/rbac/vendor/cycle/database/src/Driver/Driver.php(444): PDOStatement->execute()
#1 /home/idk/Code/rbac/vendor/cycle/database/src/Driver/Driver.php(253): Cycle\Database\Driver\Driver->statement()
#2 /home/idk/Code/rbac/vendor/cycle/database/src/Driver/Postgres/Query/PostgresInsertQuery.php(69): Cycle\Database\Driver\Driver->query()
#3 /home/idk/Code/rbac/vendor/yiisoft/rbac-cycle-db/src/ItemsStorage.php(119): Cycle\Database\Driver\Postgres\Query\PostgresInsertQuery->run()
#4 /home/idk/Code/rbac/vendor/yiisoft/rbac/src/Manager.php(595): Yiisoft\Rbac\Cycle\ItemsStorage->add()
#5 /home/idk/Code/rbac/vendor/yiisoft/rbac/src/Manager.php(451): Yiisoft\Rbac\Manager->addItem()
#6 /home/idk/Code/rbac/index.php(58): Yiisoft\Rbac\Manager->addPermission()
#7 {main}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.