如果已经激活,如何在登录时检查用户

how to check users on login if its already activated

用户字段,activation_key 字符串,默认状态 int = 0,

激活的配置文件的状态 = 1 但即使用户尚未激活,它也会始终登录,

如果已经激活,则用户可以登录,但如果尚未激活,请显示消息“请先激活”

假设我的激活方法已经完成,

<?php
public function login()
  {
    if ($this->request->is('post')) {

        if (Validation::email($this->request->data['username'])) {
            $this->Auth->config('authenticate', [
                'Form' => [
                    'fields' => ['username' => 'email']
                ]
            ]);
            $this->Auth->constructAuthenticate();
            $this->request->data['email'] = $this->request->data['username'];
            unset($this->request->data['username']);
        }
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);

            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}
?>

您可以在身份验证之后和 setUser() 之前查看状态。

例子

$user = $this->Auth->identify();

    if($user) {
     if($user['status'] == 0) { 
        $this->Flash->error(__('Your account is not activated yet.please check your email.'),array('class' => 'alert alert-danger'));
     } else {
       $this->Auth->setUser($user);
        //rest code here
     }
    }