开发者

cakephp custom login conditions

开发者 https://www.devze.com 2023-02-01 18:28 出处:网络
I would like to check, whether user\'s account is开发者_运维技巧 activated while loggin in, but cake\'s Auth component takes care of the login in a way I don\'t know how to control. Cake basically use

I would like to check, whether user's account is开发者_运维技巧 activated while loggin in, but cake's Auth component takes care of the login in a way I don't know how to control. Cake basically uses blank login function and I have no idea how to check value of User.active.

Thanks in advance


The AuthComponent has a property for setting additional conditions just like this, called $userScope.

Just include this line in your beforeFilter() Auth setup block:

$this->Auth->userScope = array('User.active' => true);

Note: the above applies to Cake 1.x. For 2.x use:

$this->Auth->scope = array('User.active' =>true);

Then you can leave your login method blank and the AuthComponent will append this extra condition when authenticating the visitor.

You can see all the additional properties here: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers

If you don't include this extra scope, then inactive users will still be able to log in and you'd have to log them out in your login() method after checking.


On your Users controller, or wherever you want to place it (the action that the login form links to):

function login() {
    if ($this->Session->read('Auth.User')) {
        $active = $this->Auth->user('active');
        if ($active) {
            //(do stuff)
        }
        else {
            //(do other stuff)
        }
    }
}

This assumes that there is an "active" column in your User table that contains either true or false (or 1 or 0). $this->Auth->user() allows you to access the current logged in user's data. More information in here: http://book.cakephp.org/view/1264/user

0

精彩评论

暂无评论...
验证码 换一张
取 消