2013-07-31 146 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上测试,您应该在用户表中包含电子邮件字段。

相关问题