Cakephp Auth登录不使用其他用户表

时间:2017-08-22 09:26:40

标签: php cakephp login authorization cakephp-2.x

遇到此功能的问题。不幸的是,当用户登录时,登录页面显示错误:“无效的用户名或密码,请再试一次”,而不是将其签名并将其重定向到索引页。

我的User_Details表包含以下字段:User_Details(user_id,user_name,user_password,email,created,modified)。

这是我在BlogDetailsController中的登录功能的代码:

public function login() {
    $this->loadModel('UserDetail');
    $this->layout = 'login';

    if ($this->request->is('post')) {
        //print_r($this->request->data);
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

这是我的AppController代码:

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

class AppController extends Controller {

    // added the debug toolkit
    // sessions support
    // authorization for login and logut redirect
    public $components = array(
        'DebugKit.Toolbar',
        'Flash',
        'Session', //Session
        'Auth' => array(//Auth
            'loginAction' => array('controller' => 'BlogDetails', 'action' => 'login'),
            'loginRedirect' => array('controller' => 'BlogDetails', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'BlogDetails', 'action' => 'login'),
            'authError' => "Sorry, you're not allowed to access this page.",
            'Form' => array(
                'userModel' => 'UserDetail',
                'fields' => array('username' => 'user_name', 'password' => 'user_password'))
        )
    );

    // only allow the login controllers only
    public function beforeFilter() {
        $this->Auth->allow('login');
    }

}

这是我的login.ctp代码:

<?php
echo $this->Flash->render('auth');
echo $this->Form->create('UserDetail', array(
    'inputDefaults' => array(
        'div' => array('class' => 'form-group'),
        'class' => 'form-control',
        'label' => false
    )
));
echo $this->Form->input('user_name', array(
    'before' => '<span class="input-with-icon"><i class="fa fa-user"></i>',
    'after' => '</span>',
    'type' => 'text',
    'title' => 'Please enter username',
    'placeholder' => "User Name",
    'data-rule-requiredchardigitsonly' => true));
echo $this->Form->input('user_password', array(
    'before' => '<span class="input-with-icon"><i class="fa fa-key"></i>',
    'after' => '</span>',
    'type' => 'password',
    'title' => 'Please enter password',
    'placeholder' => "Your Password",
    'data-rule-digit' => true));
$options = array(
    'label' => 'Sign in',
    'div' => array(
        'class' => 'form-group',
    ),
    'class' => 'form-control btn btn-primary',
);
echo $this->Form->end($options);
?>

希望有人可以指出出了什么问题......

我正在使用cakephp版本2.8.1

1 个答案:

答案 0 :(得分:0)

public function beforeSave($options = array()) {
        // hash our password
        if (isset($this->data[$this->alias]['user_password'])) {
            $this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
        }

        // fallback to our parent
        return parent::beforeSave($options);
    }

添加到UserDetail模型并且有效。