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.
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
$this->Auth->authenticate = array(
AuthComponent::ALL => array(
'scope' => array('User.verified' => '1'),
),
);
$this->Auth->authenticate = array(
'Form' => array(
'scope' => array('User.verified' => '1')
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With