CakeDC Users login with username

2019-06-04 19:21发布

问题:

The default login process requires e-mail, not username.

I've added this to AppController.php on beforeFilter(), which is also mentioned on CakePHP's Docs Authentication:

$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

This is the same as the default login.ctp found in the plugin's folder. I just changed the field email to username.

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.

Debugging

When calling the debugger:

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.

回答1:

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.

EDIT:

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