首先,我想测试用户表上的两个附加参数,即用户帐户是否已激活以及用户在允许访问该站点之前是否被管理员禁止。
教程代码使用香草版本,Zend_Auth_Adapter_DbTable
它使用特定的 api 进行身份验证。要让 Zend_Auth 以您希望的方式工作并不是很困难,但需要一些思考,因为您需要实现Zend_Auth_Adapter_Interface
. 听起来更糟,您只需要实现该authenticate()
方法。下面是一个可用于代替 auth 适配器的示例Zend_Auth_Adapter_DbTable
:
<?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()
}
你可以修改 auth 适配器来做你需要的任何事情。
就您的访问列表而言,要记住的是您的资源是由字符串定义的。在本教程中,资源定义为:
$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/
祝你好运