简体   繁体   English

Cakephp 2应用程序坚持尝试使用用户名而不是电子邮件进行身份验证

[英]Cakephp 2 app insists on trying to authenticate with username, instead of email

I have an app recently upgraded from cakephp 1.3 to cakephp 2. When trying to log in, it insists on checking the db for a 'Customer.username' field equal to the email, but I'm pretty sure I configured it to use email. 我最近有一个应用程序从cakephp 1.3升级到cakephp2。尝试登录时,它坚持要在数据库中检查是否等于电子邮件的“ Customer.username”字段,但我敢肯定我已将其配置为使用电子邮件。

Heres my AppController: 这是我的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'))
        );

And then the login code in the View: 然后在视图中登录代码:

<?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(); ?>

And the login code from the Customers Controller: 以及来自客户控制器的登录代码:

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;
}

FROM: http://book.cakephp.org/2.0/en/core-libraries/components/authetication.html 来自: 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')
            )
        )
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM