原文:
http://weierophinney.net/matthew/archives/165-Login-and-Authentication-with-Zend-Framework.html
我收集到很多如何在Zend Framework控制身份认证的问题。典型的问题是他们不知道如何结合:
- 一个认证适配器
- 一个登陆表单
- 一个login/logout的controller动作
- 检测提交请求的验证用户
它不是很可怕,但是它需要知道不同MVC块之间如何搭配在一起,和如何使用Zend_Auth。让我们看一下。
Authentication Adapter【认证适配器】为了使这些工作,你需要一个
authentication adapter。这个例子不是针对那个的,而是根据文档需要的,你需要给予你的站。我将做一个假设,就是你的认证器需要用户名密码来作为验证凭据。
我们的登录controller将用这个适配器,但是简单的需要一个占位符来收回它。
Login Form【登录表单】登录表单本身非常的简单。你可以创建一些基本的认证规则 使你可以保护数据库或者其他的服务,但是否则保持事情相对简单。为了手册的目的,我们定义了一下标准:
- 用户名只能是字母,在3-20个之间。
- 密码必须是数字组成的,在6-20个之间。
表单如下:
class LoginForm extends Zend_Form
{
public function init()
{
$username = $this->addElement('text', 'username', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alpha',
array('StringLength', false, array(3, 20)),
),
'required' => true,
'label' => 'Your username:',
));
$password = $this->addElement('password', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'required' => true,
'label' => 'Password:',
));
$login = $this->addElement('submit', 'login', array(
'required' => false,
'ignore' => true,
'label' => 'Login',
));
// We want to display a 'failed authentication' message if necessary;
// we'll do that with the form 'description', so we need to add that
// decorator.
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
array('Description', array('placement' => 'prepend')),
'Form'
));
}
}
Login Controller【登录控制】现在,让我我们创建一个控制器来操纵登录和退出动作。典型的流程如下:
- 用户点击登录表单
- 用户提交登录表单
- 控制器处理表单
- 确认错误,重新显示错误和错误信息。
- 成功确认重定向到主页
- 已经登录的用户重定向到主页
- 退出动作退出用户重定向到登录表单
这个LoginController将使用你选择的登陆器,既这个登录表单。We will pass to the login form constructor the form action and method (since we now know what they will be for this usage of the form).。当我们获得一个有效值,我们将传递他们到我们的验证器中。
创建控制器. 首先, 我们将创建表达那存取和验证适配器.
class LoginController extends Zend_Controller_Action
{
public function getForm()
{
return new LoginForm(array(
'action' => '/login/process',
'method' => 'post',
));
}
public function getAuthAdapter(array $params)
{
// Leaving this to the developer...
// Makes the assumption that the constructor takes an array of
// parameters which it then uses as credentials to verify identity.
// Our form, of course, will just pass the parameters 'username'
// and 'password'.
}
}