我最近有一个应用程序从cakephp 1.3升级到cakephp 2.当尝试登录时,它坚持要检查数据库中的' Customer.username'字段等于电子邮件,但我很确定我将其配置为使用电子邮件。
继承我的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'))
);
然后是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(); ?>
客户控制器的登录代码:
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 :(得分: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')
)
)
)
);