markdown Zend Framework 3:RBAC的访问过滤器和访问视图助手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Zend Framework 3:RBAC的访问过滤器和访问视图助手相关的知识,希望对你有一定的参考价值。

# 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
        ]
    ]
]
```

以上是关于markdown Zend Framework 3:RBAC的访问过滤器和访问视图助手的主要内容,如果未能解决你的问题,请参考以下文章

markdown Zend Framework 3:授权和RBAC

markdown Zend Framework 3:Controller插件管理器

markdown Zend Framework 3:RBAC的访问过滤器和访问视图助手

markdown Zend Framework 3:用于身份验证服务的CurrentUser Filter和CurrentUser View Helper

如何模拟 Zend\Form 提交而不在 Zend Framework 2/3 中显示表单?

如何模拟Zend Form提交而不在Zend Framework 2/3中显示表单?