2012-04-24 76 views
1

我現在有一個應用程序使用Zend_Auth進行用戶訪問。該網站有一個管理部分,我希望一個用戶在我的數據庫中擔任管理角色,以便在他使用他的憑據時可以訪問它。 Zend_Acl是唯一的方法嗎?對於我想要做的事情,似乎有點複雜,或者對我的問題會有更簡單的解決方案嗎?ZendFramework應用程序中的管理部分

我已經想過了,我現在想知道是否有可能有兩個認證控制器一個用戶和一個我的管理部分?

謝謝

回答

2

我最近做了這樣的事情。爲管理模塊創建一個用於檢查用戶憑證的前端控制器插件。就像:

class Admin_Plugin_Auth extends Zend_Controller_Plugin_Abstract 
{  
    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     if ($request->getModuleName() != 'admin'){ 
      return; 
     } 
     $auth = Zend_Auth::getInstance(); 
     if (!$auth->hasIdentity()){ 
      // send him to login 
     } 
     $user = $auth->getIdentity(); 
     if (!$user->isAdmin()){ // or however you check 
      // send him to a fail page 
     } 
    }  
} 
+0

對我來說聽起來很合理,所以如果我在「/ application」中創建了一個名爲Acl.php的文件,然後在引導程序中註冊插件,它將在啓動時初始化? – Rex89 2012-04-24 16:30:13

+0

是的,儘管您可以選擇放置位置,名稱和註冊方式。就我個人而言,我居住在管理模塊(文件:'application/modules/admin/plugins/Auth.php'),名爲'Admin_Plugin_Auth'。我確定模塊Bootstrap擴展了'Zend_Application_Module_Bootstrap'(它註冊了一個將文件夾「application/modules/admin/plugins」映射到類前綴'Admin_Plugin_'的資源加載器)。並將該插件註冊到Bootstrap模塊(文件:'application/modules/admin/Bootstrap.php')中。 – 2012-04-25 05:21:10

+0

有一天,這些信息對我有幫助,因爲它是我的應用程序不需要acl ..只是..謝謝 – Rex89 2012-04-25 20:09:11

0

我決定去在我的數據庫中有一個「is_admin」字段的方法,如果它設置爲1,用戶是管理員。然後我用這個代碼:

public function init() 
{ 
    $this->auth=Zend_Auth::getInstance(); 
    if ($this->auth->getStorage()->read()->is_admin) { 
    $route = array('controller'=>'admin', 'action'=>'index'); 
    } else { 
     $route = array('controller'=>'index', 'action'=>'index'); 
    $this->_helper->redirector->gotoRoute($route); 
    } 
} 

這從管理方面,如果他們不是管理員將用戶重定向,並允許他們進入,如果他們是一個管理員..輕鬆了許多比ACL來實現在簡單的使用我的應用程序。

+0

你打算接受這個答案,或者是什麼? http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – 2012-06-09 22:58:30