Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.x Auth Condition

In my User Authentication I need to set a Condition (verified = 1) for the Login to happen. I know that I should be able to do it like this:

$this->Auth->userScope = array('User.verified' => '1');

I tried this in AppController and my UsersController beforeFilter function, but it doesn't do anything. Is there anything else I need to configure for this?

I ended up doing (AppController):

public function isAuthorized($user) {

if ($user['verified'] == '0') { 
$this->Session->setFlash('You need to verify your Account first.');
return false;
}

return false;
}

This seems to be inelegant, since there should be the proper (userScope) way to do it, plus I now get two Flashes when verified = 0: The first one is the setFlash from above, and the second one is the regular authError.

I checked both, the Docs and stackoverflow, but I found very little information on this topic.

like image 214
Sebastian Avatar asked Sep 11 '12 19:09

Sebastian


3 Answers

CakePHP 2.x:

public $components = array(
    'Auth' => array(
        'loginAction' => array(
            'controller'    => 'users',
            'action'        => 'login'
        ),
        'authError' => 'Je hebt geen toegang tot dit gedeelte',
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email'),
                'scope' => array('is_admin' => '1')
            ),

        )
    ),
    'Session'
);

Update: For cakePHP 3.1 finder option is available since 3.1. Prior to that you can use scope and contain options to modify query.

http://book.cakephp.org/3.0/en/controllers/components/authentication.html#customizing-find-query

like image 91
Melvin Avatar answered Nov 01 '22 13:11

Melvin


$this->Auth->authenticate = array(
    AuthComponent::ALL => array(
        'scope' => array('User.verified' => '1'),
   ),
);
like image 20
tigrang Avatar answered Nov 01 '22 13:11

tigrang


$this->Auth->authenticate = array(
      'Form' => array(
                'scope' => array('User.verified' => '1')
      )
);
like image 1
Santi Avatar answered Nov 01 '22 12:11

Santi