2013-05-02 50 views
1

我正在嘗試使用1.11框架Link here跟隨Zend Auth和Zend Acl的教程!如何在控制器下的所有動作作爲資源在Zend Acl

我已經成功設置了身份驗證,並且能夠使用Acl.php頁面中給出的controller :: action對的身份驗證。首先我想在用戶表上測試兩個額外的參數,即用戶帳戶是否被激活以及用戶是否被管理員禁止,然後才允許訪問該網站。我如何在這段代碼中實現它。

其次我想知道如何將一個控制器下的所有操作包含到用戶授權級別。即我有一個主控制器,它在其下有各種表格的許多動作。你能告訴我如何限制訪問大師控制器的所有行動只有管理角色。無需爲Acl.php中的每個操作添加資源並允許資源。另外請告訴我,這個邏輯是否可以擴展爲允許訪問整個模塊而不是僅僅控制器(通過一個添加資源並允許資源)?如果是的話如何?

回答

0

首先,我想對用戶測試兩個附加參數 表用戶賬號是否被激活,如果用戶是 允許訪問網站之前,禁止由管理員。

教程代碼使用了一個vanilla版本的Zend_Auth_Adapter_DbTable,它使用特定的api進行身份驗證。爲了使Zend_Auth的工作方式不是非常困難,但需要一些思考,因爲您需要實施Zend_Auth_Adapter_Interface。聽起來更糟的是,你只需要實施authenticate()方法。下面是可以在適當位置使用的Zend_Auth_Adapter_DbTable一個auth適配器的例子:

<?php 
//some code truncated for length and relevance 
class My_Auth_Adapter implements Zend_Auth_Adapter_Interface 
{ 

    protected $identity = null; 

    protected $credential = null; 

    protected $usersMapper = null; 


    public function __construct($username, $password, My_Model_Mapper_Abstract $userMapper = null) 
    { 
     if (!is_null($userMapper)) { 
      $this->setMapper($userMapper); 
     } else { 
      $this->usersMapper = new Users_Model_Mapper_User(); 
     } 
     $this->setIdentity($username); 
     $this->setCredential($password); 
    } 

    /** 
    * @return \Zend_Auth_Result 
    */ 
    public function authenticate() 
    { 
     // Fetch user information according to username 
     $user = $this->getUserObject(); 

     if (is_null($user)) { 
      return new Zend_Auth_Result(
        Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, 
        $this->getIdentity(), 
        array('Invalid username') 
      ); 
     } 
     // check whether or not the hash matches 
     $check = Password::comparePassword($this->getCredential(), $user->password); 
     if (!$check) { 
      return new Zend_Auth_Result(
        Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, 
        $this->getIdentity(), 
        array('Incorrect password') 
      ); 
     } 
     // Success! 
     return new Zend_Auth_Result(
       Zend_Auth_Result::SUCCESS, 
       $this->getIdentity(), 
       array() 
     ); 
    } 


    // public function setIdentity($userName) 

    // public function setCredential($password) 

    // public function setMapper($mapper) 


    /** 
    * @return object 
    */ 
    private function getUserObject() 
    { 
     return $this->getMapper()->findOneByColumn('username', $this->getIdentity()); 
    } 

    /** 
    * @return object 
    */ 
    public function getUser() 
    { 
     $object = $this->getUserObject(); 
     $array = array(
      'id' => $object->id, 
      'username' => $object->username, 
      'role' => $object->getRoleId() 
     ); 
     return (object) $array; 
    } 
    // public function getIdentity() 

    // public function getCredential() 

    // public function getMapper() 

} 

可以通過修改身份驗證適配器做幾乎任何你需要的。

就您的訪問列表而言,需要記住的是您的資源是由字符串定義的。在本教程的情況下的資源被定義爲:

$this->add(new Zend_Acl_Resource('error::error')); 

其中對結腸的左側的字符串表示控制器和對結腸的右側的字符串代表的動作。這是此行的ACL插件,告訴在美國的資源是什麼:

if(!$acl->isAllowed($user->role, $request->getControllerName() . '::' . $request->getActionName())) 

你可以改變你的資源是對任何事情,你的作品這個定義。

因爲看起來每個項目都需要不同的東西,所以提供關於如何實施ACL的硬性規定非常困難。

環顧網絡,你會發現幾個不同的Zend Framework ACL實現,其中一些實現可能非常複雜。

下面是一個可能提供更多見解。 http://codeutopia.net/blog/2009/02/06/zend_acl-part-1-misconceptions-and-simple-acls/

祝你好運

相關問題