2016-12-15 92 views
0

我有一个应用程序最近从cakephp 1.3升级到cakephp 2.当尝试登录时,它坚持检查数据库中的“Customer.username”字段等于电子邮件,但我很确定我将其配置为使用电子邮件。Cakephp 2应用程序坚持尝试使用用户名进行身份验证,而不是电子邮件

继承人我的AppController:

class AppController extends Controller { 
    public $components = array('Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'Email', 'password' => 'password') 
      ) 
     ) 
    ), 'Security', 'AntiXss', 'Cookie'); 

    public $helpers = array('Js', 'Html', 'Form', 'Number', 'DateFormat', 'Currency', 'Session', 'DebugKit.Toolbar'); 
    public $uses = array('Language', 'Customer', 'Affiliate', 'Setting', 'Whitelabel'); 

    public function beforeFilter() { 
     Debugger::dump($this); 
     //Configure AuthComponent 
     $this->Auth->userModel = 'Customer'; 
     $this->Auth->fields = array('username' => 'Email', 'password' => 'password'); 
     $this->Auth->loginAction = array('controller' => 'customers', 'action' => 'login'); 
     $this->Auth->logoutRedirect = array('controller' => 'customers', 'action' => 'login'); 
     $this->Auth->loginRedirect = '/'; 
     $this->Auth->identifyMethod = 'login_identify'; 
     $this->Auth->authError = __("Please log in to continue."); 
     $this->Auth->authenticate = array(
     AuthComponent::ALL => array('userModel' => 'Customer'), 
      'Basic', 
      'Form' => array('fields' => array('username' => 'Email')) 
     ); 

然后在视图中登录代码:

<?php echo $this->Form->create('Customer', array('action' => 'login')); ?> 
<fieldset class="Login"> 
    <?php 
    echo $this->Form->input('Email', array("label"=>__('Email'))); 
    echo $this->Form->input('password', array("label"=>__('Password'))); 
    echo $this->whiteLabelElement('login_terms'); 
    echo $this->Form->button(__('Log In'), array('type'=>'submit', 'class' => 'button loginButton')); 
    ?> 
</fieldset> 
<?php echo $this->Form->end(); ?> 

而且从客户控制器登录代码:

function login() { 
    if($this->loggedCustomerData) { $this->redirect("/"); } // If user is logged in, redirect to home 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect('/accounts/')); 
     } else { 
      $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
     } 
    } 
} 


// This is not an action. It's called by the login process, passing in email and password, for this method to return 
// the customer that should be logged in (or null if invalid password). Here, we resolve to the right customer record 
// in the right whitelabel 
function login_identify($data, $conditions) { 
    if (isset($data['id'])) { // This means we got called by AutoLogin... 
     $this->LoginAudit->LogLogin($data['id'], "auto_login"); 
     return array('Customer' => $data); // Somehow we get a Customer array, but not in a sub-array. 
    } 

    $whitelabel = $this->Whitelabel->GetWhitelabelFromHost(); 
    $email = $data['Customer.Email']; 

    // First look for a *customer* (not a lead) in this whitelabel 
    $objCustomer = $this->Customer->findByWhitelabelAndEmail($whitelabel, $email, false, false); 

    // Then, a *customer* in another sharing whitelabel 
    if (!$objCustomer) { $objCustomer = $this->Customer->findByWhitelabelAndEmail($whitelabel, $email, true, false); } 

    // Finally, if there are no customers we can use, maybe we have a lead in this whitelabel 
    // We don't look for leads in other whitelabels, that makes no sense. The customer can register in this site at this point, but he can't login 
    if (!$objCustomer) { $objCustomer = $this->Customer->findByWhitelabelAndEmail($whitelabel, $email, false, true); } 

    // Finally, validate the password if we found a customer 
    if ($objCustomer) { 
     if ($data['Customer.password'] == $objCustomer['Customer']['password']) { 
      $this->LoginAudit->LogLogin($objCustomer['Customer']['id']); 
      return $objCustomer; 
     } 
    } 
    return null; 
} 
+0

您是否看到[this](http://stackoverflow.com/questions/8908489/authenticating-with-email-address-in-cakephp-v2-0?rq=1)文章?这对我来说似乎是同样的问题。 – DMuenstermann

+0

是你在db中colume的名字是** E ** mail或** e ** mail –

回答

0

FROM:http://book.cakephp.org/2.0/en/core-libraries/components/authetication.html

To configure different fields for user in $components array: 

// Pass settings in $components array 
public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email') 
      ) 
     ) 
    ) 
); 
+0

对不起,完全不是你的问题。 – Curlas

+0

虽然我的代码中有这个,但它仍然坚持寻找用户名字段。 –

+0

是的,我很抱歉。是相同的代码...哪里是identifyMethod“login_identify”?? $ this-> Auth-> identifyMethod ='login_identify'; – Curlas

相关问题