ray/role-module

Role Module for Ray.Di

1.0.0 2022-04-05 03:08 UTC

This package is auto-updated.

Last update: 2024-12-01 00:13:09 UTC


README

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 http://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!