CakePHP 2.1 Auth-> login () does not work, but adding a user

I searched a lot of posts in stackoverflow for an answer, and maybe I'm just missing something, but I can't get $ this-> Auth-> login () to work. I have tried many different sentences from other posts. I will try to be as careful as possible when describing other methods that I have tried.

I have a user add. MD5 hashing works correctly. I hashed the password and then verified it using the mid salad md5 http://www.miraclesalad.com/webtools/md5.php

I do not use hash salt. I use MD5 without salt.

I am using Postgresql 9.0 database. I know that some of CakePhp magicians do not work for all databases (or so they told me).

application /Config/core.php

Configure::write('Security.level', 'medium');

/**
* A random string used in security hashing methods.
*/

    Configure::write('Security.salt', '');

Auth- > user_password . user_password user_name - core_users. beforeFilter().

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

/Controller/AppController.php

class AppController extends Controller {
public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login'),
        /*'fields' => array('password' => 'user_password', 'username' => 'user_name'),*/
        'userModel' => 'CoreUser'
    )
);

public function beforeFilter() {
    Security::setHash('md5');
    $this->Auth->allow('login');
    //debug($this->Auth);
} 
}

, , , .

/Controller/CoreUsersController.php

    public function login() {
    Security::setHash('md5');
    //debug($this->Auth);

    if ($this->request->is('post')) {
        debug(Security::hash($this->Auth->request->data['CoreUser']['user_password']));
        debug($this->Auth);
        debug(Configure::version());
        debug($this->Auth->request->data['CoreUser']['user_password']);
        debug($this->Auth->request->data['CoreUser']['user_name']);
        if ($this->Auth->login()) {
            debug($this->Auth->request->data['CoreUser']['user_password']);
            $this->redirect($this->Auth->redirect());

        } else {
            debug($this->Auth->request->data['CoreUser']['user_password']);
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}
public function logout() {
    $this->redirect($this->Auth->logout());
}

//CoreUser.php

 App::uses('AuthComponent', 'Controller/Component');
class CoreUser extends AppModel{
public $primaryKey = 'user_id';
public $sequence = 'core_user_id_seq';
public $name = 'CoreUser';
public $validate = array(
    'user_name' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'User name is required'
        )
    ),
    'user_password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Password is required'
        )
    ),
    'privilege_id' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Privilege ID is required'
        ),
        'legalValues' => array(
             'rule' => array('between',1,4),
            'message' => 'Privilege must be between 1 and 4'
        )
    ),
    'user_initial' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'User initials is required'
        )
    ),
    'email' => array(
        'rule' => array('email',true),
        'message' => 'Email must have an \'@\' symbol and a domain e.g. .com' 
    )
);

public function beforeSave() {
    Security::setHash('md5');
    if (isset($this->data[$this->alias]['user_password'])) {
        $this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
    }
    return true;
}
}

/View/CoreUsers/login.ctp

<h3>Login</h3>

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('CoreUser');?>
<fieldset>
    <legend><?php echo __('Please enter your username and password'); ?></legend>
<?php
    echo $this->Form->input('user_name');
    echo $this->Form->input('user_password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>
</div>

CoreUsersController , .

. , , .

'098f6bcd4621d373cade4e832627b4f6'

Auth

object(AuthComponent) {
components => array(
    (int) 0 => 'Session',
    (int) 1 => 'RequestHandler'
)
authenticate => array(
    (int) 0 => 'Form'
)
authorize => false
ajaxLogin => null
flash => array(
    'element' => 'default',
    'key' => 'auth',
    'params' => array()
)
loginAction => array(
    'admin' => false,
    'controller' => 'CoreUsers',
    'action' => 'login'
)
loginRedirect => array(
    'controller' => 'pages',
    'action' => 'index'
)
logoutRedirect => array(
    'controller' => 'pages',
    'action' => 'display',
    (int) 0 => 'home'
)
authError => 'You are not authorized to access that location.'
allowedActions => array(
    (int) 0 => 'login'
)
request => object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'CoreUsers',
        'action' => 'login',
        'named' => array(),
        'pass' => array()
    )
    data => array(
        'CoreUser' => array(
            'user_name' => 'testy5',
            'user_password' => 'test'
        )
    )
    query => array()
    url => 'CoreUsers/login'
    base => '/cpm_v2_dev'
    webroot => '/cpm_v2_dev/'
    here => '/cpm_v2_dev/CoreUsers/login'
}
response => object(CakeResponse) {

}
settings => array(
    'loginRedirect' => array(
        'controller' => 'pages',
        'action' => 'index'
    ),
    'logoutRedirect' => array(
        'controller' => 'pages',
        'action' => 'display',
        (int) 0 => 'home'
    ),
    'loginAction' => array(
        'admin' => false,
        'controller' => 'CoreUsers',
        'action' => 'login'
    ),
    'userModel' => 'CoreUser'
)
userModel => 'CoreUser'
  }

CakePHP

'2.1.0'

()

'test'

login()

 'testy5'

()

 'test'

, stackoverflow, . , .

1) . . beforeFilter(). :

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

:

$this->Form->input('username');
$this->Form->input('password');

2) :

 $this->Auth->request->data['CoreUser']['password'] = Security::hash($this->Auth->request->data['CoreUser']['password'])
   if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
   }

EDIT0

3) , CakePHP 2.0 Auth Login

AuthComponent :

     public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'CoreUser',
                'fields' => array(
                    'username' => 'user_name',
                    'password' => 'user_password'
                )
            )
        ),
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login')
    )
);

, . , . , . !

+3
3

, , , , CakePHP. 2.1.2. 2.1.0.

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

. Auth, :

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html

, . , . login.ctp .

 $this->Form->input('username');
 $this->Form->input('password');

:

 echo $this->Form->input('user_name');
 echo $this->Form->Label('Password');
 echo $this->Form->password('user_password');

.

EDIT0: . AppController .

class CoreUsersController extends AppController{
    public $helpers = array('Html','Form');

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

Auth :

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'CoreUser',
                'fields' => array(
                    'username' => 'user_name',
                    'password' => 'user_password'
                )
            )
        ),
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login')
    )
);

:

Configure::write('Security.salt', '');

md5 . beforeSave() :

public function beforeFilter() {
    Security::setHash('md5');
    $this->Auth->allow('login','add');  
} 

BeforeSave():

public function beforeSave() {
    if (isset($this->data[$this->alias]['user_password'])) {
        $this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
    }
    return true;
}
+2

db, . , , , , . (, , , ).

, Auth- > password Security:: hash(), . Security:: hash ($ password, 'md5', false). . , , Auth/login .

, , Auth... db.

+1

I had the same error, I made a copy of the cakephp project and could not enter the new one. After weeks of finding the right answer, I found that all I had to do was change permissions on the tmp folder. For an unknown reason, when I copy folders, some of them are copied with read-only permissions.

0
source

All Articles