问题
I have been fighting with this for awhile. I have a CakePHP 2.0 application that needs Authentication, due to the fact that it must be in another language, I stepped away from the automagic 'User' with 'Username' and 'Password' convention and made my own database table:
utilizatori('id', 'nume', 'parola')
In my AppController I have the following defined which if I understood correctly should override the default CakePHP associations and make it use my new structure:
class AppController extends Controller {
var $components = array('Session',
'Auth' => array('Form' => array(
'userModel' => 'Utilizator',
'fields' => array('username' => 'nume', 'password' => 'parola'),
'loginAction' => array('controller' => 'utilizatori', 'action' => 'login'),
'loginRedirect' => array('controller' => 'categorii', 'action' => 'admin_index'))));
function beforeFilter() { }
}
In my UtilizatoriController I have the following login function:
function login()
{
$this->layout = 'admin';
debug($this->data);
debug(Security::hash($this->data['Utilizator']['parola']));
debug($this->Auth->login());
/*more here but don't think it's important*/
The data debug comes up with the expected values, let's presume:
**data['Utilizator']['nume']** = 'Cosmin'
**data['Utilizator']['parola']** = 'Cosmin'
The password hash debug yield the expected hash which I also manually inserted in the database and yet the login function returns false.
I'm not sure what to do next and any feedback would be appreciated. Also, is there a way to access what gets sent to $this->Auth->login()?
Thank you
回答1:
As it turns out, the reason behind $this->Auth->login() failing was my declaration of the AuthComponent inside AppController. After some more research and a surprisingly small but helpful post at How can I use different model for Auth component in CakePHP 2.0.4? I reviewed my code and now it seems to be working.
I will post the review here in case anyone will bump into this.
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Utilizator',
'fields' => array(
'username' => 'nume',
'password' => 'parola'
)
)
),
'loginAction' => array('controller' => 'utilizatori', 'action' => 'login'), //Not related to the problem
'loginRedirect' => array('controller' => 'utilizatori', 'action' => 'index'), //Not related to the problem
'logoutRedirect' => array('controller' => 'utilizatori', 'action' => 'index') //Not related to the problem
)
);
来源:https://stackoverflow.com/questions/10014928/cakephp-2-0-auth-login-not-working