mohit-singh/zf2auth-acl

此软件包最新版本(V2.0.0)没有可用的许可证信息。

这是一个与zfcuser模块插件的ACL模块,高度可配置,并在控制器、视图和模块级别提供不同的服务。

V2.0.0 2014-07-02 12:53 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:04:35 UTC


README

分支: zfcuser_acl

这是从arvind2110/ZF2-Auth-ACL分支并插入zfcuser模块的ZF2 ACL模块。它将提供基于角色的访问和角色之间的切换,提供自定义权限拒绝模板,在控制器、视图和模块级别提供插件。所有角色、资源和权限都存储在数据库中。

如何使用它

使用composer添加

  "require" : {
       ...
    "mohit-singh/zf2auth-acl": "V1.0.1"
  }

在application.config.php中启用模块

 'modules' => array(
	...
        'ZF2AuthAcl'
    ),

然后复制并重命名以下内容:

copy vendor/mohit-singh/zf2auth-acl/config/aclAuth.local.php.dist to config/autoload/aclAuth.local.php

添加依赖表

vendor/mohit-singh/zf2auth-acl/data/data.sql

例如,在表中添加用户角色

INSERT INTO `role` (`role_name`, `status`) VALUES ('Role1', 'Active');
INSERT INTO `role` (`role_name`, `status`) VALUES ('Role2', 'Active');
INSERT INTO `role` (`role_name`, `status`) VALUES ('Role3', 'Active');

添加资源,资源是您通过它调用控制器的控制器名称,例如“Application\Controller\Index”

INSERT INTO `resource` (`resource_name`) VALUES ('Application\\Controller\\Index');

添加权限,权限是操作,您必须将所有操作与其控制器资源相关联

INSERT INTO `permission` (`permission_name`, `resource_id`) VALUES ('index', 1);
INSERT INTO `permission` (`permission_name`, `resource_id`) VALUES ('show', 1);

添加角色权限,您必须决定哪些角色具有哪些权限

INSERT INTO `role_permission` (`role_id`, `permission_id`) VALUES (1, 1);
INSERT INTO `role_permission` (`role_id`, `permission_id`) VALUES (1, 2);

添加用户角色,您必须决定哪些用户具有哪些角色,这可以是手动完成或使用一些自定义脚本

INSERT INTO `user_role` (`user_id`, `role_id`) VALUES (1, 1);
INSERT INTO `user_role` (`user_id`, `role_id`) VALUES (2, 2);

注意:请检查aclAuth.local.php配置文件中的默认角色,它应该是您在数据库中插入的任何角色之一。

完成所有这些配置后,您就可以使用ACL模块了

服务

从URL中删除ACL并使其全局,允许所有访问,在此处添加链接

// in config/autoload/aclAuth.local.php
'globalList' => array(
		      'Application\Controller\Index-index'
	   ),

在登录之前从URL中删除ACL并使其全局,在此处添加链接

// in config/autoload/aclAuth.local.php
'beforeLoginList' => array(
		      'Application\Controller\Index-index'
	   ),

自定义权限拒绝模板,在此处添加新模板路径

// in config/autoload/aclAuth.local.php
'ACL_Template' =>'zf2-auth-acl/index/permission.phtml'

控制器级别的基于角色的服务

// Check user has role or not , return true, false
$this->userAuthRole()->userHasRole();

//Get  user current role
$this->userAuthRole()->getRoleName();

//get All valid role for the current user
$this->userAuthRole()->getUserValidRole();

//Switch between roles
$this->userAuthRole()->switchRole('ADMIN');

视图级别

// Check user has role or not , return true, false
$this->roleAuth()->userHasRole();

//Get  user current role
$this->roleAuth()->getRoleName();

//get All valid role for the current user
$this->roleAuth()->getUserValidRole();

//Switch between roles
$this->roleAuth()->switchRole('ADMIN');

模块级别

$roleAtuth = $serviceManager->get('roleAuthService');

// Check user has role or not , return true, false
$roleAtuth->userHasRole();

//Get  user current role
$roleAtuth->getRoleName();

//get All valid role for the current user
$roleAtuth->getUserValidRole();

//Switch between roles
$roleAtuth->switchRole('ADMIN');

它还提供缓存机制以将角色、资源和权限存储在缓存中。您可以在此处配置缓存

    /**
     * This cache is used the disk file system to store date with the following options.
     */ 
    'fileCache' => array(
        'cache_dir' => './data/cache',
        'namespace' => 'systemCache',
        'dir_level' => 2,
        'filePermission' => 0666,
        'dirPermission' => 0755
    ),

您也可以像这样在项目中访问文件系统缓存

// store item in filesystem cache
        $this->getServiceLocator()->get('Zend\Cache\Storage\Filesystem')->setItem('foo', 'taxi');


// get item from filesystem cache
        echo 'Cached Item is:- '.$this->getServiceLocator()->get('Zend\Cache\Storage\Filesystem')->getItem('foo');

您正在使用文件系统缓存,因此您必须授予缓存文件夹权限

> sudo chmod -R 0777 data/cache