2013-06-27 209 views
0

我是cakephp 2.x的新手,所以我不知道如何做到這一點..我想從他的電子郵件地址和電話號碼登錄用戶..我的意圖是,如果數據庫中的數字是這個「12345」和用戶正試圖通過這個號碼「+12345」登錄,他可以登錄到系統..我已經寫了一個代碼,但我不知道如何使用此或調整我的代碼在auth組件作爲AUTH組件autometically登錄用戶..cakephp電話號碼驗證

這裏是我的控制器

public function beforeFilter() { 
    parent::beforeFilter(); 


    $this->Auth->authenticate = array(
     'Authenticate.Cookie' => array(
      'fields' => array(
       'username' => 'email', 
       'password' => 'password' 
      ), 
      'userModel' => 'User', 
      'scope' => array('User.active' => 1) 
     ), 
     'Authenticate.MultiColumn' => array(
      'fields' => array(
       'username' => 'email', 
       'password' => 'password' 
      ), 
      'columns' => array('email', 'mobileNo'), 
      'userModel' => 'User', 
     ) 
    ); 
} 




public function login() { 


    if ($this->Auth->login() || $this->Auth->loggedIn()) { 
     $this->redirect('/users/dashboard'); 
    }else{ 
    $this->layout='logindefault'; 
    $this->set('title_for_layout', 'Account Login'); 
    /*$this->Auth->logout(); 
    $cookie = $this->Cookie->read('Auth.User'); */ 


    if ($this->request->is('post')) { 




     if ($this->Auth->login() || $this->Auth->loggedIn()) { 
      if ($this->Session->check('Auth.User')){ 


      $this->_setCookie($this->Auth->user('idUser')); 
      $this->redirect('/users/dashboard'); 

     } }else { 
      $this->Session->setFlash('Incorrect Email/Password Combination'); 
     } 
    }} 
} 

這裏是我試圖添加代碼..

$mobileNo='+123456789'; 
    if (strpos($mobileNo,'+') !== false) { 
     $mobileNo=str_replace("+", "",$mobileNo); 
    } 

?>

回答

0

該文檔(http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules)對自定義的驗證規則的示例。

但您的用例與驗證無關(對於登錄,沒有正常驗證,只能應用「範圍」)。 它關於自定義進入登錄的數據。 您可以使用login()方法在Auth-> login()調用之前修改傳入數據,也可以使用新的2.x方法執行此操作:使用自定義身份驗證適配器。然後

// in your AppController::beforeFilter() method 
$this->Auth->authorize = array('PhoneNumber'); 

和你******中國適配器應該叫PhoneNumberAuthenticate並放在/控制器/組件/驗證。

有關其他適配器如何工作並將其應用於您自己的詳細信息,請參閱https://github.com/ceeram/Authenticate

但因爲你還使用「電子郵件」登陸,你可能是最好的使用替代選擇在您的登錄方法:

public function login() { 
    $this->request->data['User']['email'] = $this->_replace($this->request->data['User']['email']); 
    if ($this->Auth->login()) {...} 

與_replace()包含您replacment代碼。

+0

我不知道我能做到這一點......如果您已實現或此功能的工作,如果你不介意,否則它的工作要做。作爲我被困在此之前共享代碼這裏現在 – hellosheikh

+0

沒有得到你..誰我替​​換...爲什麼我應該替換...我想要做的是..我想從電子郵件,數字和+號碼登錄用戶.. – hellosheikh

+0

確切 - arent你在你的方法中使用'str_replace'來「統一」格式?只需將該代碼放在那裏(作爲控制器或模型中的自己的方法 - 或內聯)。 – mark