各位:
http://devzone.zend.com/article/3322” 为了扩大其他答复,我举了一个简短的例子,说明你如何利用冻结的框架来管理认证:
首先,如果允许客户查阅某些数据,你需要打上“<>前>发送<>>的原始数据。 这一gin子可能看起来像这样:
class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {
private $_acl = null;
public function __construct(Zend_Acl $acl) {
$this->_acl = $acl;
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
//get request information
$module = $request->getModuleName ();
$resource = $request->getControllerName ();
$action = $request->getActionName ();
try {
if(!$this->_acl->isAllowed(Zend_Registry::get( role ),
$module . : . $resource, $action)){
$request->setControllerName ( authentication )
->setActionName ( login );
}
}catch(Zend_Acl_Exception $e) {
$request->setControllerName( index )->setActionName ( uups );
}
}
}
因此,每个用户类型都有你在图书馆中界定的某些许可。 根据每项要求,如果允许用户获得资源,请进行检查。 如果您不改用正文页,则预分机将用户带上资源。
http://framework.zend.com/manual/en/zend.acl.introduction.html” 您界定了允许或拒绝接触的作用、资源和许可,例如:
class Model_LibraryAcl extends Zend_Acl {
public function __construct() {
$this->addRole(new Zend_Acl_Role( guests ));
$this->addRole(new Zend_Acl_Role( users ), guests );
$this->addRole(new Zend_Acl_Role( admins ), users );
$this->add(new Zend_Acl_Resource( default ))
->add(new Zend_Acl_Resource( default:authentication ), default )
->add(new Zend_Acl_Resource( default:index ), default )
->add(new Zend_Acl_Resource( default:error ), default );
$this->allow( guests , default:authentication , array( login ));
$this->allow( guests , default:error , error );
$this->allow( users , default:authentication , logout );
}
}
之后,你不得不在你的boot锁档案中安装cl子和 au子:
private $_acl = null;
protected function _initAutoload() {
//...your code
if (Zend_Auth::getInstance()->hasIdentity()){
Zend_Registry::set ( role ,
Zend_Auth::getInstance()->getStorage()
->read()
->role);
}else{
Zend_Registry::set( role , guests );
}
$this->_acl = new Model_LibraryAcl ();
$fc = Zend_Controller_Front::getInstance ();
$fc->registerPlugin ( new Plugin_AccessCheck ( $this->_acl ) );
return $modelLoader;
}
Finally in your authentication controller you have to use a custom auth adapter and setup actions for login and logout:
public function logoutAction() {
Zend_Auth::getInstance ()->clearIdentity ();
$this->_redirect ( index/index );
}
private function getAuthAdapter() {
$authAdapter = new Zend_Auth_Adapter_DbTable (
Zend_Db_Table::getDefaultAdapter ());
$authAdapter->setTableName( users )
->setIdentityColumn( email )
->setCredentialColumn ( password )
->setCredentialTreatment ( SHA1(CONCAT(?,salt)) );
return $authAdapter;
}
在你的行动中,你需要将日志数据传递给进行认证的适应者。
$authAdapter = $this->getAuthAdapter ();
$authAdapter->setIdentity ( $username )->setCredential ( $password );
$auth = Zend_Auth::getInstance ();
$result = $auth->authenticate ( $authAdapter );
if ($result->isValid ()) {
$identity = $authAdapter->getResultRowObject ();
if ($identity->approved == true ) {
$authStorage = $auth->getStorage ();
$authStorage->write ( $identity );
$this->_redirect ( index/index );
} else {
$this->_redirect ( authentication/login );
}
And that s all. I recommend you this HOW TO on youtube on zend auth and zend acl.