2013-07-31 112 views
7

在我現在的系統中,我需要使用用戶名或電子郵件和密碼登錄。 任何人都可以知道如何實現這一目標?CakePHP登錄用戶名或電子郵件使用身份驗證組件

我的表格:

<?php echo $this->Form->create('User', array('action' => 'login')); 
    echo $this->Form->input('username', array('class' => 'TextField js-user-mode')); 
echo $this->Form->input('password', array('class' => 'TextField')); 
?> 

我的AppController:

public $components = array(


     'Email'=>array(), 
     'Auth'    => array(
      'loginAction'  => array(
       'admin'    => false, 
       'controller'  => 'users', 
       'action'   => 'login' 
      ), 
      'authError'  => 'Your session has ended due to inactivity. Please login to continue.', 
      'authenticate' => array(
       'Form'   => array(
        'fields'  => array('username' => array('username','email')), 
       ), 
       'all' => array(
        'userModel' => 'User', 
        'scope' => array('User.status' =>array('active')) 
       ) 

      ) 
     ) 
    ); 

讓我知道我需要做些什麼..?

回答

4

只要我們可以將您的身份驗證登錄動作之前:

$emailUsername = @$this->request->data['User']['email']; 
if (!filter_var($emailUsername, FILTER_VALIDATE_EMAIL)) { 
    $emailFromUsername = $this->User->find('first', array('conditions' => array('User.username' => $emailUsername), 'recursive' => -1, 'fields' => array('email'))); 
    //pr($emailFromUsername); 
    if (!empty($emailFromUsername)) { 
     $emailFromUsernameDB = $emailFromUsername['User']['email']; 
    } else { 
     $emailFromUsername = ''; 
    } 
    $this->request->data['User']['email'] = $emailFromUsername; 
} 
+0

當你有電子郵件時,這也很有用,但沒有用戶名。 – Amjo

2

您可以利用MultipleColumn驗證適配器:

​​

+0

您能否告訴我如何使用它,我是否需要修改我的驗證組件 –

+1

其全部記錄?在課堂和自述中都有? https://github.com/ceeram/Authenticate#configuration – mark

5

我不知道禮儀是什麼張貼回答舊的問題,但這是我爲此做的。

在我的登錄功能

 $username = $this->data['User']['username']; 
     $password = $this->request->data['User']['password']; 

     $user = $this->User->findByUsername($username); 
     if (empty($user)) { 
      $user = $this->User->findByEmail($username); 

      if (empty($user)) { 
       $this->Session->setFlash(__('Incorrect Email/Username or Password')); 
       return; 
      } 
      $this->request->data['User']['username'] = $user['User']['username']; 
     } 
+1

歡迎您回答舊的問題。即使提問者不再需要幫助,你也會幫助未來的訪問者。 – dcastro

0

我發現這種解決方案非常有用。 我已經創建了延伸FormAuthenticate兩類:

應用/控制器/組件/認證/ ClassNameAuthenticate.php

<?php 
App::uses('FormAuthenticate', 'Controller/Component/Auth'); 

class ClassNameAuthenticate extends FormAuthenticate { 
} 

應用/控制器/組件/認證/ ClassNameEmailAuthenticate.php

<?php 
App::uses('FormAuthenticate', 'Controller/Component/Auth'); 

class ClassNameEmailAuthenticate extends FormAuthenticate { 
} 

然後在我的控制器中添加認證組件到$組件

public $components = array(
    'Session', 
    'Auth' => array(
     'authenticate' => array(
      'ClassName' =>array(
       'userModel'=>'ClassName', 
       'fields' => array(
        'username' => 'username', 
       ), 
       'scope' => array('ClassName.active' => 1) 
      ), 
      'ClassNameEmail' =>array(
       'userModel'=>'ClassName', 
       'fields' => array(
        'username' => 'email', 
       ), 
       'scope' => array('ClassName.active' => 1) 
      )     
     ) 
    ), 
); 

登錄視圖:login.ctp

<div class="form"> 
    <?php echo $this->Form->create('ClassName'); ?> 
    <fieldset> 
     <legend><?php echo __('Login'); ?></legend> 
     <?php 
      echo $this->Form->input('username'); 
      echo $this->Form->input('password'); 
     ?> 
    </fieldset> 

    <?php echo $this->Form->end(array('label'=>__('Login'))); ?> 
</div> 

和登錄()動作:

public function login(){ 

    if ($this->Auth->loggedIn()) { 
     return $this->redirect($this->Auth->redirect()); 
    } 

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

     //Need to duplicate field email for ClassNameEmail Auth 
     $this->request->data['ClassName']['email'] = $this->request->data['ClassName']['username']; 

     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } 

     $this->Session->setFlash(__('Invalid username/email or password, try again')); 
    } 
} 

我希望有人會發現這很有用。

2

我從url發現下列代碼。我認爲這是最簡單的方式。您登錄操作使用下面的代碼:

public function login() { 
    if($this->request->is('post')&&!empty($this->request->data)) { 
     App::Import('Utility', 'Validation'); 
     if(isset($this->data['User']['username']) && Validation::email($this->request->data['User']['username'])) { 
      $this->request->data['User']['email'] = $this->request->data['User']['username']; 
      $this->Auth->authenticate['Form'] = array('fields' => array('username' => 'email')); 
     } 
     if($this->Auth->login()) { 
      /* login successful */ 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      /* login unsuccessful */ 
     } 
    } 
} 

,還可以使用下面的代碼登錄。CTP

<?php 
    echo $this->form->create('User'); 
    echo $this->form->input('username'); 
    echo $this->form->input('password'); 
    echo $this->form->end('Submit'); 
?> 
+0

可能看起來像一個黑客,但它是一個「接近正確」的答案,除非他們有一個更「優雅」的方式來做到這一點。 –

0

這是我的解決方案來解決這個問題

public function login(){ 
    if($this->request->is('post')){ 
     $this->User->set($this->request->data); 
     if($this->User->validates()){ 
      if(Validation::email($this->data['User']['username'])){ 
       $this->Auth->authenticate['Form'] = array_merge($this->Auth->authenticate['Form'],array('fields'=>array('username'=>'email'))); 
       $this->request->data['User']['email'] = $this->request->data['User']['username']; 
       unset($this->request->data['User']['username']); 
      } 
      if($this->Auth->login()){ 
       $this->User->id = $this->Auth->user('id'); 
       $this->User->saveField('last_login',time()); 
       if($this->data['User']['remember']){ 
        unset($this->request->data['User']['remember']); 
        $this->request->data['User']['password'] = Security::hash($this->request->data['User']['password'],'blowfish'); 
        $this->Cookie->write('rememberMe',$this->request->data['User'],true,'2 days'); 
       } 
       $this->redirect($this->Auth->loginRedirect); 
      } 
      $this->Session->setFlash('Invalid Username or Password entered, please try again.','default',array('class'=>'alert alert-warning'),'warning'); 
     } 
    } 
    $this->set('title','Login Page'); 
} 
2

假設你有用戶名電子郵件在users表這兩個領域

在你的AppController.php

public function beforeFilter() { 
    if ($this->request->is('post') && $this->action == 'login') { 
     $username = $this->request->data['User']['username']; 
     if (filter_var($username, FILTER_VALIDATE_EMAIL)) { 
      $this->Auth->authenticate['Form']['fields']['username'] = 'email'; 
      $this->request->data['User']['email'] = $username; 
      unset($this->request->data['User']['username']); 
     } 
    } 
} 

此代碼適用於CakePHP 2.x,未在版本3.x上測試,您應該在用戶表中包含電子郵件字段。

相關問題