简单的ACL。避免在Zend Framework中手动编写ACL权限

dev-master 2015-03-10 17:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:00:50 UTC


README

此模块可以从您的路由中创建一个ACL服务。避免为您的应用程序中的每个角色或路由手动编写ACL权限规则。

安装

安装TrascastroACL使用composer。有关composer文档,请参阅getcomposer.org。

php composer.phar require itrascastro/acl:dev-master

配置

  • 将模块名称 'TrascastroACL' 添加到您的 config/application.config.php 文件中
array(
    'modules' => array(
        'Application',
        'TrascastroACL',
    ),
);
  • 从 TrascastroACL 配置目录复制 'TrascastroACL.global.dist' 并将其粘贴到 config/autoload 文件夹中,删除 '.dist' 后缀。现在添加您的应用程序角色,并添加ACL将重定向未授权访问尝试的 'controller' 和 'action'。您还需要添加一个角色提供者
return [
    'TrascastroACL' => [
        'roles' => [
            'guest',
            'user',
            'admin',
        ],
        'forbidden' => [
            'controller' => 'YOUR_FORBIDDEN_MANAGER_CONTROLLER',
            'action'     => 'YOUR_FORBIDDEN_MANAGER_ACTION',
        ],
        'role_provider' => 'YOUR_ROLE_PROVIDER',
    ],
];

角色提供者必须实现接口 'TrascastroACL\Provider\RoleProviderInterface'

namespace TrascastroACL\Provider;

interface RoleProviderInterface 
{
    /**
     * @return String
     */
    public function getUserRole();
}

这是一个角色提供者类的示例

namespace User\Provider;

use TrascastroACL\Provider\RoleProviderInterface;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\Authentication\AuthenticationService;

class RoleProvider implements RoleProviderInterface
{
    /**
     * @var AuthenticationService
     */
    private $authenticationService;

    /**
     * @param AuthenticationServiceInterface $authenticationService
     */
    public function __construct(AuthenticationServiceInterface $authenticationService)
    {
        $this->authenticationService = $authenticationService;
    }

    /**
     * @return String
     */
    public function getUserRole()
    {
        return ($identity = $this->authenticationService->getIdentity()) ? $identity->role : 'guest';
    }
}

Factory的设置如下

namespace User\Provider\Factory;

use User\Provider\RoleProvider;

class RoleProviderFactory
{
    public function __invoke($serviceLocator)
    {
        $authenticationService = $serviceLocator->get('User\Service\Authentication');

        return new RoleProvider($authenticationService);
    }
}

不要忘记将您的提供者添加到您的 module.config.php 文件中

'service_manager' => array(
    'factories' => array(
        // [ ... ]
        'User\Provider\RoleProvider' => 'User\Provider\Factory\RoleProviderFactory',
    ),
),

用法

现在您可以通过简单地添加一个类似于下面的 'roles' 键来从您的路由中管理您的应用程序访问控制

array(
    'router' => array(
        'routes' => array(
            'user\users\update' => array(
                'type' => 'Segment',
                'options' => array(
                    'route'    => '/admin/users/update/id/:id/',
                    'constraints' => array(
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'User\Controller\Users',
                        'action'     => 'update',
                        'roles'      => ['admin', 'moderator'],
                    ),
                ),
            ),
        ),
    ),
);

只有具有 'admin' 或 'moderator' 角色的用户现在可以访问该路由。如果您未在路由中创建 'roles' 键或将其留空,则该资源将是公开的。

访问Acl服务

  • 从控制器
$acl = $this->serviceLocator->get('TrascastroACL');
  • onBootstrap
<?php

namespace MyModule;

use Zend\Mvc\MvcEvent;

class Module implements AutoloaderProviderInterface
{
    public function onBootstrap(MvcEvent $e)
    {
        $sm  = $e->getApplication()->getServiceManager();
        $acl = $sm->get('TrascastroACL');
    }

    ...
}
  • 从视图

此模块提供视图助手,以便在视图中访问 TrascastroACL

<?php if ($this->tacl()->isAllowed($this->identity()->role, 'admin\users\update')): ?>

它也可以通过 layout() 视图助手使用

<?php if ($this->layout()->tacl->isAllowed($this->identity()->role, 'admin\users\update')): ?>
  • 从布局
<?php if ($this->tacl()->isAllowed($this->identity()->role, 'admin\users\update')): ?>

它也可以通过布局变量使用

<?php if ($this->tacl->isAllowed($this->identity()->role, 'admin\users\update')): ?>