# Access Plugin and Access View Helper
## AccessPlugin
Create file `module/User/src/Controller/Plugin/AccessPlugin.php`
```php
namespace User\Controller\Plugin;
use User\Service\RbacManager;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
/**
* This controller plugin is used for role-based access control (RBAC)
* @package User\Controller\Plugin
*/
class AccessPlugin extends AbstractPlugin {
/**
* @var RbacManager
*/
private $rbacManager;
public function __construct($rbacManager)
{
$this->rbacManager = $rbacManager;
}
/**
* Checks whether the currently logged in user has the given permission.
* @param $permission
* @param array $params
* @return mixed
* @throws \Exception
*/
public function __invoke($permission, $params = [])
{
return $this->rbacManager->isGranted(null, $permission, $params);
}
}
```
## AccessPluginFactory
```php
namespace User\Controller\Plugin\Factory;
use Interop\Container\ContainerInterface;
use User\Controller\Plugin\AccessPlugin;
use User\Service\RbacManager;
use Zend\ServiceManager\Factory\FactoryInterface;
/**
* This is the factory for AccessPlugin. Its purpose is to instantiate the plugin
* and inject dependencies into its constructor.
* @package User\Controller\Plugin\Factory
*/
class AccessPluginFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$rbacManager = $container->get(RbacManager::class);
return new AccessPlugin($rbacManager);
}
}
```
## Register AccessPlugin
```php
use User\Controller\Plugin\AccessPlugin;
use User\Controller\Plugin\Factory\AccessPluginFactory;
return [
'controller_plugins' => [
'factories' => [
AccessPlugin::class => AccessPluginFactory::class
],
'aliases' => [
'access' => AccessPlugin::class
]
]
];
```
## Access ViewHelper
```php
namespace User\View\Helper;
use Zend\View\Helper\AbstractHelper;
/**
* This view helper is used to check user permissions.
*/
class Access extends AbstractHelper
{
private $rbacManager = null;
public function __construct($rbacManager)
{
$this->rbacManager = $rbacManager;
}
public function __invoke($permission, $params = [])
{
return $this->rbacManager->isGranted(null, $permission, $params);
}
}
```
## AccessFactory
```php
namespace User\View\Helper\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use User\Service\RbacManager;
use User\View\Helper\Access;
/**
* This is the factory for Access view helper. Its purpose is to instantiate the helper
* and inject dependencies into its constructor.
*/
class AccessFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$rbacManager = $container->get(RbacManager::class);
return new Access($rbacManager);
}
}
```
## Register Access ViewHelper
```php
use User\View\Helper\Access;
use User\View\Helper\Factory\AccessFactory::class
return [
'view_helpers' => [
'factories' => [
Access::class => AccessFactory::class
],
'aliases' => [
'access' => Access::class
]
]
]
```