Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Auth and dynamically changing user details on the UI

When my users are logged in I display their details (name, email) on the UI. When they update their profile, I would like to show the updated details, without requiring the user to log out and back in.

The UI details are retrieved from Zend_Auth via a view helper. Zend_Auth is storing the 'identity' details in a session.

How should I go about updating the details in the session?:

  • I was considering retrieving the user's login credentials from the database and using them to call Zend_Auth->authenticate() again. The problem is that I don't know the password, only it's md5 hash. I could consider a new method, reauthenticate(), which configured the adapter to bypass the md5 and salt, but this sounds laborious.

  • I was considering writing directly to the Zend_Auth session namespace, but this sounds like a recipe for trouble?

Have you come across a similar problem? How did you handle it?

Your ideas are much appreciated!

like image 310
DatsunBing Avatar asked Aug 04 '11 09:08

DatsunBing


2 Answers

You can update Zend_auth identity for the currently logged user. Very simplified action that updates only username could be as follows:

 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;
}

Hope this helps.

like image 168
Marcin Avatar answered Nov 04 '22 04:11

Marcin


What I really want is a method on Zend_Auth::setIdentity($user).

But in the absence of such a method, I have used a hack in which I have create an auth adapter that always returns success and sets the identity to the same user object I would have created in a "real" auth adapter. Then I just call Zend_Auth::authenticate($adapter) with that adapter and it sets the identity internally.

Now, looking mosre closely at the internals of Zend_Auth::authenticate(), I see that what we could do is just:

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

like image 21
David Weinraub Avatar answered Nov 04 '22 05:11

David Weinraub