2013-02-24 84 views
2

如何使用Zend ACL允許某些用戶訪問控制器中的某些操作?現在,我只知道如何讓用戶訪問整個控制器,但我想限制控制器內的操作!Zend ACL允許某些操作

回答

1

要允許/拒絕某些操作的訪問,請在Zend_Acl的allow/deny方法中指定它們。

Zend_Acl::allow()方法中的第三個參數將只允許您將訪問控制設置爲給定控制器/資源上的某些操作。例如:

<?php 

$acl = new Zend_Acl(); 

// Roles 
$guest = new Zend_Acl_Role('guest'); 
$user = new Zend_Acl_Role('user'); 

// Register the roles with the Zend_Acl 
$acl->addRole($guest); 
$acl->addRole($user, 'guest'); 

// Resources/Controllers 
$indexController = new Zend_Acl_Resource('index'); 
$profileController = new Zend_Acl_Resource('profile'); 

// Add resources/controllers to the Zend_Acl 
$acl->add($indexController); 
$acl->add($profileController); 


// Now set limits of access to the resources. 
// Guests get access to all the actions in the index controller, 
// but to only the login and logout actions in the profile controller. 
$acl->allow('guest', 'index'); 
$acl->allow('guest', 'profile', array('login', 'logout')); 

// Users get full access to the profile controller 
$acl->allow('user', 'profile');