Get support for yiisoft/rbac-rules-container

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

yiisoft/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.1 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');

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 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

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2