首先,我想對用戶測試兩個附加參數 表用戶賬號是否被激活,如果用戶是 允許訪問網站之前,禁止由管理員。
教程代碼使用了一個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/
祝你好運