Sebastian
Sebastian

Reputation: 923

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.

Upvotes: 9

Views: 7439

Answers (5)

Melvin
Melvin

Reputation: 3431

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

Upvotes: 10

Santi
Santi

Reputation: 631

$this->Auth->authenticate = array(
      'Form' => array(
                'scope' => array('User.verified' => '1')
      )
);

Upvotes: 1

petervaz
petervaz

Reputation: 14175

Try this:

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

Upvotes: 0

mishmash
mishmash

Reputation: 4458

Assuming the CakePHP Documentation is correct Auth::userScope was renamed to Auth::scope so now you would do something like this:

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

Configuring Auth in CakePHP 2.x

Hope this helps.

Upvotes: 0

tigrang
tigrang

Reputation: 6767

$this->Auth->authenticate = array(
    AuthComponent::ALL => array(
        'scope' => array('User.verified' => '1'),
   ),
);

Upvotes: 2

Related Questions