CakeDC用户登录使用用户名 [英] CakeDC Users login with username
问题描述
默认的登录过程需要的电子邮件,而不是用户名。
The default login process requires e-mail, not username.
我添加这 AppController.php
在 beforeFilter()
,这是在<一个也提到HREF =http://book.cakephp.org/1.3/en/The-Manual/Core-Components/Authentication.html相对=nofollow> CakePHP的文档验证:
$this->Auth->fields = array(
'username' => 'username',
'password' => 'password'
);
但不知何故,它不允许用户使用他们的用户名登录。我如何能更改任何想法?
But somehow it does not allow users to login with their usernames. Any ideas on how I can change that?
的AppController
App::uses('Controller', 'Controller');
class AppController extends Controller {
var $components = array(
'Session',
'RequestHandler',
'Security'
);
var $helpers = array('Form', 'Html', 'Session', 'Js');
public function beforeFilter() {
$this->Auth->authorize = 'Controller';
$this->Auth->fields = array('username' => 'username', 'password' => 'password');
$this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false);
$this->Auth->loginRedirect = '/';
$this->Auth->logoutRedirect = '/';
$this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
$this->Auth->loginError = __('Invalid e-mail / password combination. Please try again', true);
$this->Auth->autoRedirect = true;
$this->Auth->userModel = 'User';
$this->Auth->userScope = array('User.active' => 1);
if ($this->Auth->user()) {
$this->set('userData', $this->Auth->user());
$this->set('isAuthorized', ($this->Auth->user('id') != ''));
}
}
/View/Users/login.ctp
这是等同于默认的 login.ctp
在插件的文件夹中找到。我只是改变了实地电子邮件到用户名。
This is the same as the default login.ctp
found in the plugin's folder. I just changed the field email to username.
现在,这里是一些有趣的事情。无论code我把这个文件,CakePHP把从插件登录视图的内容,我创建的视图将被忽略。
Now, here is something interesting. Regardless the code I put in this file, CakePHP pulls the content from the plugin login view, the view I created is ignored.
调试
在调用调试器:
Debugger::dump($this->Auth);
这表明我设置的所有值。但它仍然不接受用户名。我仍然可以使用电子邮件登录,因此它不是我插入错误的凭据。这仅仅是它正在等待电子邮件/密码,而不是用户名/密码。
It shows all the values that I set. But it still does not accept the username. I can still login with email, so it is not that I am inserting the wrong credentials. It is just that it is waiting for email/password, not username/password.
推荐答案
尝试在AppController的配置它在$组件:
Try configuring it on the $components in AppController:
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username')
)
)
)
}
我对逆方式的问题,我想验证用户与他们的邮件,而不是用户名,并使用上面的配置以'用户名'=>'邮件'为我工作。
I had the problem on the inverse way, I wanted to validate users with their mail, and not username, and using the above configuration with 'username' => 'email' worked for me.
编辑:
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
),
'Session'
);
public function beforeFilter() {
//User settings
$this->activeUserId = $this->Auth->user;
$this->set('activeuserphoto', $this->Auth->user('photo'));
$this->set('activeusername', $this->Auth->user('username'));
//Configure AuthComponent
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'dashboard');
}
这篇关于CakeDC用户登录使用用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!