Zend_Auth并在UI上动态更改用户详细信息

时间:2011-08-04 09:17:45

标签: php zend-framework

当我的用户登录时,我会在UI上显示他们的详细信息(姓名,电子邮件)。当他们更新自己的个人资料时,我希望显示更新的详细信息,而无需用户注销并重新登录。

通过视图助手从Zend_Auth检索UI详细信息。 Zend_Auth在会话中存储“身份”细节。

我应该如何更新会话中的详细信息?:

  • 我正在考虑从数据库中检索用户的登录凭据,并使用它们再次调用Zend_Auth-> authenticate()。问题是我不知道密码,只有它是md5哈希。我可以考虑一个新方法reauthenticate(),它将适配器配置为绕过md5和salt,但这听起来很费力。

  • 我正在考虑直接写入Zend_Auth会话命名空间,但这听起来像是一个麻烦的方法?

你遇到过类似的问题吗?你是怎么处理的?

非常感谢您的想法!

3 个答案:

答案 0 :(得分:4)

您可以更新当前登录用户的Zend_auth标识。仅更新用户名的非常简化的操作可以如下:

 public function editAction() {


    // check if user is logged, etc, and then
    // show the edit user form and process the data after submission.

    $userForm = new My_Form_EditUser();       


    if ($this->getRequest()->isPost()) {
        if ($userForm->isValid($_POST)) {

            // process the submitted data,
            // and when you are sure  that everything went ok,
            // update the zend_auth identity


            $authData = Zend_Auth::getInstance()->getIdentity();

            // this line would depend on the format of your 
            // identity data and a  structure of your 
            // actual form. 
            $authData->property->nickname = $formData['user']['nickname'];

            $this->_helper->FlashMessenger('Your data was changed');
            return $this->_redirect('/');
        }
    }       

    $this->view->form = $userForm;
}

希望这有帮助。

答案 1 :(得分:2)

我真正想要的是Zend_Auth::setIdentity($user)上的方法。

但是在没有这样的方法的情况下,我使用了一个hack,我已经创建了一个auth适配器,它始终返回成功并将标识设置为我在“真正的”auth适配器中创建的相同用户对象。然后我只用那个适配器调用Zend_Auth::authenticate($adapter),然后在内部设置标识。

现在,密切关注Zend_Auth::authenticate()的内部情况,我发现我们能做的只是:

Zend_Auth::getInstance()->getStorage()->write($user);

答案 2 :(得分:0)

以下是我暂时解决问题的方法。不确定它是否是最佳解决方案:

  • 为Zend_Auth
  • 创建一个包装器
  • 包含getAuthAdapter($ raw)方法
  • 以不同方式配置适配器,具体取决于$ raw

    的值
    if (false === $raw) {
        $this->_authAdapter->setCredentialTreatment('SHA1(CONCAT(?,salt))');
    } else {
        $this->_authAdapter->setCredentialTreatment();
    }