作者 主題: Zend_Acl+Zend_Auth一起使用  (閱讀 15389 次)

0 會員 與 1 訪客 正在閱讀本文。

uberr

  • 懷疑的國中生
  • **
  • 文章數: 62
    • 檢視個人資料
    • 天創科技網頁設計公司
Zend_Acl+Zend_Auth一起使用
« 於: 2008-09-06 00:02 »
Zend_Acl

今天介紹Zend Acl的用途,意思就是權限控制。它配合Zend_Auth,Plugin來用的話,會幫你省卻很時間。

先教大家如何使用Zend_Acl
1.建立角色
2.建立資源
3.對每個來源加入角色權限控制


//建立一個zend_acl object
$acl = new Zend_Acl();

1.建立角色,我們建立最簡單的二個角色好了(guest,admin)

$guest = new Zend_Acl_Role('guest');
$admin = new Zend_Acl_Role('admin');

//把群組加到acl object裡
$acl->addRole($guest);
$acl->addRole($admin);

2.建立資源,建立admin page,auth page

$admin_resource = new Zend_Acl_Resource('admin_index');
$auth_resource = new Zend_Acl_Resource('auth_index');

//把資源加到acl object裡
$acl->add($admin_resource);
$acl->add($auth_resource);

3.對每個來源加入角色權限控制,只有登入後才能有權限對admin page進行操作,如果使用者没有登入,就會redirect使用者到auth page

//第一個param是角色,第二個param是controller,第三個param是action,第四個param是自定的callback(註:null代表全部)
//上邊的意思就是說,不准許任何角色去訪問任何的資源(controller/action)
$acl->deny(null,null,null);

//guest有權限訪問auth module->index controller->login action
$this->allow('guest','auth_index',array('login','index'));

//admin有權限訪問所有的資源,最後的param是用來驗証使用者登入狀態,如果使用者登入才會准許
$this->allow('admin',null,null,new My_Acl_AuthAssert($auth));

ACL大致就是這樣設定的。

/********************************************************************
* Zend_Auth+Zend_Acl一起使用
*********************************************************************/

Zend_Auth我不多說了請自行看我之前所寫的文章

1.首先建立一個acl object,設定好acl的權限控制(同上)

2.使用controller plugin功能,在preDispatch中去檢查使用者是否對該controller/action有權限

index.php
代碼: [選擇]
<?
//建立Zend_Acl Object,權限控制
。。。。。

//建立Zend_Auth Object
。。。。

//把helper path加進去,因為controller plugin需要使用到My_Plugin_Auth class
$view = new Zend_View();
$view->addHelperPath(APPLICATION_DIRECTORY.'/library/My/View/Helper','My_View_Helper');
$viewRender = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRender->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRender);

$controller = Zend_Controller_Front::getInstance();
$controller->addControllerDirectory('application/auth/controllers','auth');
$controller->addControllerDirectory('application/admin/controllers','admin');

//加入controller plugin,檢查使用者權限
$controller->registerPlugin(new My_Plugin_Auth($auth,$acl));
?>

My/Acl/AuthAssert.php
代碼: [選擇]
/**
 * 檢查使用者是否登入
 */
class My_Acl_AuthAssert implements Zend_Acl_Assert_Interface {

/*
* @param Object
*/
private $_auth;

/*
* @param Object Zend_Auth
*/
public function __construct(Zend_Auth $auth)
{
$this->_auth = $auth;
}

/*
* @param Object Zend_Acl
* @param Object Zend_Acl_Role_Interface
* @param Object Zend_Acl_Resourece_interface
*
* @return boolean
*/
public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role = null, Zend_Acl_Resource_Interface $resource = null, $privilege = null) {
return $this->_isLogin();
}

/*
* check user login status
*
* @return boolean
*/
private function _isLogin()
{
//你所需要的檢查程式碼
}
}

?>


My/Plugin/Auth.php
代碼: [選擇]
<?php

/*
 *  在preDispatch中去檢查使用者是否對該controller/action有權限
 */
class My_Plugin_Auth extends Zend_Controller_Plugin_Abstract {

/*
 *@param Object
 */
private $_auth;

/*
 * @param Object
 */
private $_acl;

/*
 * @param Array
 */
private $_no_auth = array(
&#39;module&#39; => &#39;auth&#39;,
&#39;controller&#39; => &#39;index&#39;,
&#39;action&#39; => &#39;index&#39;
);

/*
 * @param Array
 */
private $_no_acl = array(
&#39;module&#39; => &#39;admin&#39;,
&#39;controller&#39; => &#39;index&#39;,
&#39;action&#39; => &#39;index&#39;
);

public function __construct(Zend_Auth $auth,Zend_Acl $acl)
{
$this->_auth $auth;
$this->_acl $acl;
}

public function preDispatch()
{
$request $this->getRequest();
$module $request->getModuleName();
$controller $request->getControllerName();
$action $request->getActionName();
$resource $module.&#39;_&#39;.$controller;
$acl $this->_acl;

//設定使用者角色,如果登入的話,把使用者設定成admin角色
if(isset($_SESSION[&#39;AUTH&#39;])) :
$role = &#39;admin&#39;;
else :
$role = &#39;guest&#39;;
endif;

//檢查資源是否加入到acl object
if($acl->has($resource)) :

//如果使用者没有對該資源的權限,把使用者redirect
if(!$acl->isAllowed($role,$resource,$action)) :
switch($role) :
case &#39;admin&#39;:
$module $this->_no_acl[&#39;module&#39;];
$controller $this->_no_acl[&#39;controller&#39;];
$action $this->_no_acl[&#39;action&#39;];
break;
default:
$module $this->_no_auth[&#39;module&#39;];
$controller $this->_no_auth[&#39;controller&#39;];
$action $this->_no_auth[&#39;action&#39;];
break;
endswitch;
endif;

//設定redirect的module/controller/action
$request->setModuleName($module);
$request->setControllerName($controller);
$request->setActionName($action);
else :

//設定redirect的module/controller/action
if($role == &#39;admin&#39;) :
$module $this->_no_acl[&#39;module&#39;];
$controller $this->_no_acl[&#39;controller&#39;];
$action $this->_no_acl[&#39;action&#39;];
else :
$module $this->_no_auth[&#39;module&#39;];
$controller $this->_no_auth[&#39;controller&#39;];
$action $this->_no_auth[&#39;action&#39;];
endif;

$request->setModuleName($module);
$request->setControllerName($controller);
$request->setActionName($action);
endif;
}
}

?>


天創科技網頁設計公司
« 上次編輯: 2011-10-03 15:06 由 uberr »

HaWay

  • 大隻佬!
  • 老人組
  • 俺是博士!
  • *****
  • 文章數: 3980
    • 檢視個人資料
回覆: Zend_Acl+Zend_Auth一起使用
« 回覆 #1 於: 2008-10-03 17:20 »
請問 My/Acl/AuthAssert.php 這段程式碼主要的作用是什麼? 謝謝
我做人那麼 nice, 肯定有什麼誤會.....

uberr

  • 懷疑的國中生
  • **
  • 文章數: 62
    • 檢視個人資料
    • 天創科技網頁設計公司
回覆: Zend_Acl+Zend_Auth一起使用
« 回覆 #2 於: 2008-10-06 09:47 »
是用來檢查使用者是否登入,回傳是true and false,如果回傳true,哪這個$this->allow('admin',null,null,new My_Acl_AuthAssert($auth));就會執行.

anderson1127

  • 訪客
回覆: Zend_Acl+Zend_Auth一起使用
« 回覆 #3 於: 2008-10-06 13:45 »
感謝樓主的分享...

其實,最近一陣子我也感覺到PHP提供的session功能不是很好用...
尤其是想控制session id 的時間長短 ,就有點麻煩,不然就是得用cookie
與我想像中在server端來控制session id長短,而不是在client來控制
有點落差...

忽然看到這個,心想,也許我可以不用自己下去為session 來新加Function來用...

呵呵...等等會去看Zend FrameWork的架構,看看有無適合的方法來應用!!