Sunday, March 27, 2011

Re: Update loginStatus in Database by username

On Sat, Mar 26, 2011 at 3:53 PM, enigmartin <ma.skroch@googlemail.com> wrote:
> Hello,
>
> i have a problem and hope you can help me.
>
> Can I set the loginStatus in my database during logins to true? I have
> test anything, but it don't works. During the login of an user, the id
> of the user isn't available. I wanted to use the username, but it do
> not work for me.

Normally, AuthComponent doesn't require that a login() method be
available. But, if you want to do some routine at login, you then
create the method in your UsersController.

AppController:

public function beforeFilter()
{
parent::beforeFilter();

$this->Auth->autoRedirect = false;

// other stuff
}

This tells Auth to run your login() method.

UsersController:

function beforeFilter()
{
parent::beforeFilter();

/* Do NOT add login here
*/
$this->Auth->allowedActions = array('logout', 'other_action');
}

public function login()
{
if ($user = $this->Auth->user())
{
/* Auth::user() takes any field name from users table
* and returns the value for the current User.
* If you don't pass any param you get the full record array.
*/
$this->User->id = $user['User']['id'];
$this->User->saveField('loginStatus', 1);

$this->redirect($this->Auth->redirect());
}
}

public function logout()
{
if ($user = $this->Auth->user())
{
$this->User->id = $user['User']['id'];
$this->User->saveField('loginStatus', 0);
$redirect = $this->Auth->logout();
$this->redirect($redirect);
}
$this->redirect('/');
}

> Sorry for my english. It is not the best.

Works for me.

--
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: