yiisoft / rbac-rules-container
RBAC rules container based on "yiisoft/factory"
Fund package maintenance!
Opencollective
yiisoft
Installs: 21 549
Dependents: 4
Suggesters: 1
Security: 0
Stars: 3
Watchers: 16
Forks: 2
Open Issues: 0
Requires
- php: ^8.0
- psr/container: ^1.0|^2.0
- yiisoft/definitions: ^1.0|^2.0|^3.0
- yiisoft/factory: ^1.0
- yiisoft/rbac: ^1.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- phpunit/phpunit: ^9.5
- rector/rector: ^0.15.2
- roave/infection-static-analysis-plugin: ^1.18
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.6
- yiisoft/di: ^1.0
- yiisoft/test-support: ^3.0
This package is auto-updated.
Last update: 2023-11-18 08:40:15 UTC
README
Yii RBAC Rules Container
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 ofPsr\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.