-1

I'm using CakePHP 2.2.4, and I have started to work with Atuh Componenet.

This is my AppController:

class AppController extends Controller {
    public $components = array('Auth', 'Session');

    public function beforeFilter() {
        $this->Auth->authorize = array('Controller');
        $this->Auth->authenticate = array(
            'Form' => array (
                'scope'  => array('User.active' => 1),
                'fields' => array('username' => 'email', 'password' => 'password'),
            )
        );      
    }

    public function isAuthorized($user) {

        debug($user);

        return true;
    }
}

This is my User.php model

class User extends AppModel {

    public $name = 'User';

    /* Relazioni */
    public $hasOne = 'Profile';

    public $belongsTo = 'Role';

    public $hasMany = array(
        'Lead' => array(
            'className' => 'Lead'
        )
    );  

}

and this is my UserController.php

<?php

App::uses('AppController', 'Controller');

class UsersController extends AppController 
{   
    public $name = 'Users';

    public $uses = array();

    public function beforeFilter() 
    {
        parent::beforeFilter();        
    }

    public function login()
    {
        if ($this->request->is('post')) 
        {
            if ($this->Auth->login()) 
            {
                debug('Logged');
            } 
            else 
            {
                $this->Session->setFlash('Login non autorizzato', 'default', array('class' => 'errore'), 'login');
            }
        }   
    }

    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }

}

I have a strange problem using Auth Component, because at the end of the layout I have sql_dump element, that prints NO QUERY.

However, If i put correct values I do not login

Why does Auth component is not working ?

EDIT:

The data of the request is:

Array
(
    [User] => Array
        (
            [email] => test@test.it
            [pwd] => abc
        )

)
4

2 回答 2

4

Your code in AppController is wrong

public function beforeFilter() {
    $this->Auth->authorize = array('Controller');
    $this->Auth->authenticate = array(
        'Form' => array (
            'scope'  => array('User.active' => 1),
              // password != pwd as you post it
            'fields' => array('username' => 'email', 'password' => 'password'), 
        )
    );      
}

Change it to

'fields' => array('username' => 'email', 'password' => 'pwd'),

or make sure to post password instead of pwd in your form

Please see https://github.com/cakephp/cakephp/blob/master/lib/Cake/Controller/Component/Auth/FormAuthenticate.php for documentation on the matter

于 2013-01-05T14:44:57.010 回答
1

I'd like to post-fix this answer for anyone arriving here, who is unable to get logged in using Auth for which this example does not DIRECTLY Apply.

An important thing to remember is that Auth is expecting the underlying database columns to be "username" and "password". If for whatever reason you defer from this, for example if you want to validate on a users email (very common) and you change the table's column name to reflect this, than you must tell Auth about this.

The reason is because the underlying query will fail. Ultimately all that's happening behind the scene is a simply query matching the specified fields. For example (not exact - simply for demonstration purposes -- select *'s are bad):

SELECT * FROM users WHERE username = 'blahblahblah' AND password = 'someInsan31yh4sh3dpassw0rd'

If your underlying table is missing a "username" column in loo of an "email" column, than this query will obviously fail. Resulting in the inability to login and usually with no indication that the query failed (it is even omitted from the SQL dump). The following code in your AppController however will solve you issues:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
          'Form' => array(
             'fields' => array('username' => 'columnUsedForValidatingUsername', 'password' => 'columnUserForValidatingPassword')
          )
        )
    )
);

Jippi's answer was completely correct in this case. But I feel as though as an answerER on StackOverflow you owe it to anyone finding this to explain WHY the problem is occurring and provide an unspecific answer.

Cheers

于 2014-01-03T22:06:29.177 回答