zfstarter / zfs-rbac
1.1
2015-04-01 13:23 UTC
Requires
- php: >=5.3.23
- zendframework/zend-config: 2.3.*
- zendframework/zend-modulemanager: 2.3.*
- zendframework/zend-mvc: 2.3.*
- zendframework/zend-permissions-rbac: 2.3.*
- zendframework/zend-view: 2.3.*
This package is not auto-updated.
Last update: 2024-09-24 07:42:31 UTC
README
为Zf2\Rbac提供的包装,方便在ZF2/ZFStarter项目中使用
连接
服务以模块的形式提供,因此您只需将其名称添加到application.config.php
中要连接的模块列表即可
'modules' => array( 'ZFS\Rbac', // <-- 'Application' ),
通过事件进行配置
模块包含服务ZFS\Rbac\Rbac
,视图助手isGranted
和控制器插件isGranted
。
在服务运行过程中,它抛出2个事件
- EVENT_GET_CONFIG (ZFS\Rbac\Service\Event\GetConfig)
- EVENT_GET_USER_ROLES (ZFS\Rbac\Service\Event\GetUserRoles)
EVENT_GET_CONFIG等待从程序环境获取角色配置及其权限。可以通过订阅事件来提供
$this->getEventManager()->getSharedManager()->attach( ZFS\Rbac\Rbac::EVENT_MANAGER_IDENTIFIER, ZFS\Rbac\Rbac::EVENT_GET_CONFIG, function () { return array( 'user' => array( 'permissions' => array( 'login' ) ), 'users_manager' => array( 'permissions' => array( 'modify_users' ) ), 'admin' => array( 'children' => array( 'users_manager' ) ) ); } );
处理程序应返回一个包含角色(键)及其配置(值)的数组。配置中可能包含权限数组(键permissions)和子角色数组(键children)。
EVENT_GET_USER_ROLES等待当前用户的角色列表。可以通过订阅事件来提供
$this->getEventManager()->getSharedManager()->attach( ZFS\Rbac\Rbac::EVENT_MANAGER_IDENTIFIER, ZFS\Rbac\Rbac::EVENT_GET_USER_ROLES, function () { return array('admin'); } );
两个事件都可以由多个处理程序处理,以补充先前数组的数组。这样,每个模块都可以以对其方便的方式修改配置:从数据库中选择数据,从项目配置或单独的配置文件中选择。
使用示例
- 在控制器中
class IndexController extends AbstractActionController { public function indexAction() { if (!$this->isGranted('index_action')) { return $this->notFoundAction(); } /* ... */ } }
- 在视图模板中
<?php if ($this->isGranted('buy')): ?> <a href="/buy">Buy</a> <?php else: ?> <a href="/login">Login to buy</a> <?php endif; ?>
- 任何有
ServiceLocator
访问的地方
$this->getServiceLocator()->get('ZFS\Rbac\Rbac')->isGranted('some_permission');
在所有三个示例中,isGranted方法接受第一个参数为权限名称的字符串或权限名称数组,第二个参数为布尔值,指示是否需要重新调用EVENT_GET_USER_ROLES事件。默认值为false。
许可证
MIT