2015-04-18 50 views
0

我在网站上完成了用户系统。 在我UsersController.php我有这样的方法:登录前检查字段

public function login() 
{ 
    if($this->request->is('post')) { 
     if($this->Auth->login()) { 
      $this->Session->setFlash('Connexion établie', 'flash_success'); 
      $this->redirect($this->Auth->redirectUrl()); 
     } else { 
      $this->Session->setFlash("Nom d'user ou mot de passe invalide, réessayer", 'flash_error'); 
      $this->redirect(array('controller' => 'indexes', 'action' => 'index')); 
     } 
    } 
} 

它工作得很好,但我需要改变它。在我的数据库中,我有一个字段“验证”,它是一个布尔值。 在登录时,如果字段为true,我想记录用户,但如果字段为false,我不想记录他。

感谢您的帮助

+0

的可能重复的[cakephp的认证基本只有合法的用户(http://stackoverflow.com/questions/21932483/cakephp-authenticate-basic-only-valid-users) – ndm

回答

0

您需要范围字段。您可以将其添加在beforeFilter方法例如:

public function beforeFilter() { 
    $this->Auth->authenticate = array(
     'YourAuthComponent' => array(
      'fields' => array(
       'username' => 'username', 
       'password' => 'password' 
      ), 
      'userModel' => 'Users.User', 

      //This is what you need 
      'scope' => array(
       'User.active' => 1, 
       'User.verified' => 1) 
     ) 
    ); 
} 

或者你可以在你的AppController或UsersController的选项添加到您的组件阵列。

class AppController extends Controller { 

/** 
* Components used from the application 
* 
* @var array 
*/ 
public $components = array(

    'Auth'=> array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email'), 
       'scope' => array(
        'User.active' => 1, 
        'User.verified' => 1) 
        ) 
       ) 
      ) 
     ), 
    ); 
}