2012-12-03 77 views
1

默认登录过程需要电子邮件,而不是用户名。CakeDC用户以用户名登录

我已经添加了这AppController.phpbeforeFilter(),这是在CakePHP's Docs Authentication还提到:

$this->Auth->fields = array(
    'username' => 'username', 
    'password' => 'password' 
); 

但不知何故,它不允许用户与他们的用户名登录。关于如何改变这种情况的任何想法?

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。我只是将字段电子邮件更改为用户名。

现在,这里有一些有趣的事情。无论我将哪些代码放入此文件,CakePHP都会从插件登录视图中提取内容,我创建的视图将被忽略。

调试

当调用调试器:

Debugger::dump($this->Auth); 

这说明一切,我设置的值。但它仍然不接受用户名。我仍然可以通过电子邮件登录,所以这不是我插入错误的凭据。它只是在等待电子邮件/密码,而不是用户名/密码。

+0

未经测试/不知道这是否是正确的方法,但蛋糕DC插件将在'用户\控制器\ UsersController.php'围绕线144的价值观 - 你可以尝试调整它们那里。 – Ross

+0

事情是,我不想改变核心,我不认为这是覆盖设置的正确方法。但如果没有其他选择,我必须遵循这条路线。 – rlcabral

+1

需要注意的是,CakeDC用户插件使用该电子邮件进行注册,忘记密码等等等。 – Dave

回答

3

尝试在AppController中配置它在$组件:

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'username') 
      ) 
     )    
    ) 
} 

我对逆方式问题,我想,以验证用户使用他们的邮件,而不是用户名,并使用上述配置与“ username'=>'email'为我工作。

编辑:

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'); 
} 
+0

Nop,没有工作。看起来插件的默认设置是优先的,无论我设置了什么,它都会随着插件的需要而变化。 – rlcabral

+1

您可以添加您的登录视图代码和完整的AppController代码吗?它应该工作! –

+0

已更新问题 – rlcabral

相关问题