$ composer require ray/role-module
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));
}
}
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)
{
$ php demo/run.php
// It works!