CakePHP 2.0 Auth Login not working

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:58:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!