Git Product home page Git Product logo

rbac-rules-container's Introduction

Yii RBAC Rules Container


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

This package is a factory for creating Yii RBAC (Role-Based Access Control) rules. It provides rules container based on Yii Factory and uses Yii Definitions syntax. RBAC manager accepts rule as a name and parameters and is unaware of its creation by design, delegating creation to rules container keeping responsibilities separation.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/rbac-rules-container

General usage

Direct interaction with rules container

use Psr\Container\ContainerInterface;
use Yiisoft\Rbac\Item;
use Yiisoft\Rbac\RuleInterface;
use Yiisoft\Rbac\Rules\Container\RulesContainer;

/**
 * Checks if user ID matches `authorID` passed via parameters.
 */
final class AuthorRule implements RuleInterface
{
    public function execute(string $userId, Item $item, array $parameters = []): bool
    {
        return $parameters['authorID'] === $userId;
    }
}

$rulesContainer = new RulesContainer(new MyContainer());
$rule = $rulesContainer->create(AuthorRule::class);
  • MyContainer is a container for resolving dependencies and must be an instance of Psr\Container\ContainerInterface. Yii Dependency Injection implementation also can be used.
  • You can optionally set definitions and disable their validation if needed.

Basically, the arguments are the same as in Yii Factory. Please refer to its docs for more details.

Rules are created only once, then cached and reused for repeated calls.

$rule = $rulesContainer->create(AuthorRule::class); // Returned from cache

Using Yii config

use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Rbac\RuleFactoryInterface;
use Yiisoft\Rbac\Rules\Container\RulesContainer;

// Need to be moved to separate files accordingly
$params = [
    'yiisoft/rbac-rules-container' => [
        'rules' => ['author' => AuthorRule::class],
        'validate' => false,
    ],
];
$config = [
    RuleFactoryInterface::class => [
        'class' => RulesContainer::class,
        '__construct()' => [
            'definitions' => $params['yiisoft/rbac-rules-container']['rules'],
            'validate' => $params['yiisoft/rbac-rules-container']['validate'],
        ],
    ],
];          
$containerConfig = ContainerConfig::create()->withDefinitions($config); 
$container = new Container($containerConfig);
$rulesContainer = $container->get(RuleFactoryInterface::class);        
$rule = $rulesContainer->create('author');

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii RBAC Rules Container 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-rules-container's People

Contributors

arogachev avatar dependabot[bot] avatar samdark avatar stylecibot avatar terabytesoftw avatar vjik avatar xepozz avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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

rbac-rules-container's Issues

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.