1

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

我已将此添加到AppController.phpon中, CakePHP 的 Docs AuthenticationbeforeFilter()中也提到了这一点:

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

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

应用控制器

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') != ''));
    }
}

/查看/用户/login.ctp

login.ctp这与插件文件夹中的默认设置相同。我刚刚将字段电子邮件更改为用户名。

现在,这里有一些有趣的东西。不管我在这个文件中放什么代码,CakePHP 从插件登录视图中提取内容,我创建的视图被忽略。

调试

调用调试器时:

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

它显示了我设置的所有值。但它仍然不接受用户名。我仍然可以使用电子邮件登录,所以并不是我插入了错误的凭据。只是它正在等待电子邮件/密码,而不是用户名/密码。

4

1 回答 1

3

尝试在 AppController 中的 $components 上配置它:

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');
}
于 2012-12-03T16:21:58.127 回答