Skip to content

ray-di/Ray.RoleModule

Repository files navigation

Ray.RoleModule

Installation

Composer install

$ composer require ray/role-module

Module install

You need to provide RoleProvider.

class AppRoleProvider implements RoleProviderInterface
{
    public function get()
    {
        return 'admin';
    }
}

Install module with RoleProvider.

use Ray\RoleModule\ZendAclModule;
use Ray\Di\AbstractModule;
use Laminas\Permissions\Acl\Role\GenericRole;
use Laminas\Permissions\Acl\Acl;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        // @see https://framework.zend.com/manual/current/en/modules/zend.permissions.acl.intro.html
        $acl = new Acl();
        $roleGuest = new GenericRole('guest');
        $acl->addRole($roleGuest);
        $acl->addRole(new GenericRole('staff'), $roleGuest);
        $acl->addRole(new GenericRole('editor'), 'staff');
        $acl->addRole(new GenericRole('administrator'));
        $this->install(new ZendAclModule($acl, AppRoleProvider::class));
    }
}

Usage

Simple usage:

use Ray\RoleModule\Annotation\RequiresRoles;

#[RequiresRoles(['admin'])]
class Foo
{
    public function createUser($id)
    {

You can annotate individual method too, It has priority over class attribute.

use Ray\RoleModule\Annotation\RequiresRoles;

class Foo
{
   
    #[RequiresRoles(['admin', 'editor'])]
    public function createUser($id)
    {

Demo

$ php demo/run.php
// It works!