Seb*_*ian 9 authentication cakephp login conditional-statements
在我的用户身份验证中,我需要为登录设置条件(已验证= 1).我知道我应该能够这样做:
$this->Auth->userScope = array('User.verified' => '1');
Run Code Online (Sandbox Code Playgroud)
我在AppController和我的UsersController beforeFilter函数中尝试了这个,但它没有做任何事情.还有什么我需要配置的吗?
我最终做了(AppController):
public function isAuthorized($user) {
if ($user['verified'] == '0') {
$this->Session->setFlash('You need to verify your Account first.');
return false;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这似乎是不优雅的,因为应该有适当的(userScope)方式来做,加上我现在得到两个闪烁验证= 0:第一个是上面的setFlash,第二个是常规authError.
我检查了Docs和stackoverflow,但我发现这个主题的信息很少.
Mel*_*vin 10
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'
);
Run Code Online (Sandbox Code Playgroud)
更新:对于cakePHP 3.1 finder option自3.1起可用.在此之前,您可以使用scope和contain选项来修改查询.
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'),
),
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7095 次 |
| 最近记录: |