Friday, October 28, 2011

FYI: manually logging in a user and acl in cakephp 2.0

If anyone else has problems with getting an Aro node lookup error
after manually logging in a user you might check the array that you
use to login with.

As in the example in the cookbook, the user gets logged in with the
array of submitted data from the login form in the format:

$this->request->data['User'] => array(
'username' => 'johndoe',
'password' => 'secretpassword'
);

If you login the user and then debug this->Auth->user() or use the
static method AuthComponent::user(), you'll see that it returns an
array without the 'User' key. Even though you submitted the data with
the 'User' key, it is being used and returned to you without it.

You can manually set user information to an array (maybe by running a
find on a specific user) and then use that data, with the 'User' key
to log a user in. Then Acl will give you an error about node lookup
because it's using an array formatted like so:

'User' => array(
'User' => array(
'username' => 'johndoe',
'password' => 'secretpassword'
)
)

To avoid errors you can manually log in a user by adding the user key
to the array variable that you use. i.e. $this->Auth-
>login($user['User']):

$user['User'] => array(
'username' => 'johndoe',
'password' => 'secretpassword'
);

To log in a user from posted form data use: $this->Auth->login($user);
To log in a user from other methods such as a find use: $this->Auth-
>login($user['User']);

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: