cake php无法登录"用户名和密码错误"

时间:2015-09-09 06:44:30

标签: cakephp

帮助,我无法使用我的用户名和密码登录我看到脚本控制器模型和视图但我不明白错误 它是我的AppController.php

<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

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

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package     app.Controller
 * @link        http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller 
{
    public $components = array('DebugKit.Toolbar','Session','Auth','Acl' => array
                                (
                                    'authorize' => array('Actions' => array('actionPath' => 'controllers'))
                                )
                                );
    public function isAuthorized($user) 
        {
            // Admin can access every action
            if (isset($user['role']) && $user['role'] === 'admin') 
                {
                    return true;
                }
            // Default deny
            return false;
        }
    public function beforeFilter() 
        {
            //$this->Auth->allow();
            //Configure AuthComponent
            $this->Auth->loginAction = array
                (
                    'controller' => 'users',
                    'action' => 'login'
                );
            $this->Auth->logoutRedirect = array
                (
                    'controller' => 'users',
                    'action' => 'login'
                );
            $this->Auth->loginRedirect = array
                (
                    'controller' => 'posts',
                    'action' => 'add'
                );
        }
    public $helpers = array('Session','Html','Form');

}

?>

这是我的user.php

    <?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
/**
 * User Model
 *
 * @property Group $Group
 * @property Post $Post
 */
class User extends AppModel {
    public function bindNode($user) 
        {
            return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
        }
    public $actsAs = array('Acl' => array('type' => 'requester', 'enabled' => false));
    public function parentNode() 
        {
            if (!$this->id && empty($this->data)) 
                {
                    return null;
                }
            if (isset($this->data['User']['group_id'])) 
                {
                    $groupId = $this->data['User']['group_id'];
                } else {
            $groupId = $this->field('group_id');
                }
            if (!$groupId) 
                {
                    return null;
                }
            return array('Group' => array('id' => $groupId));
        }
    public function beforeSave($options = array()) 
        {
            $this->data['User']['password'] = AuthComponent::password(
            $this->data['User']['password']
            );
            return true;
        }


    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

这是我的login.ctp

    <?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->inputs(array('legend' => __('Login'),'username','password'));
echo $this->Form->end('Login');
?>

请帮帮我。 我的字段只是用户名,密码 但用户名和密码又错了..

2 个答案:

答案 0 :(得分:1)

这是一个明显的错误:

public function beforeSave($options = array()) 
{
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    return true;
}

无论何时保存用户记录并且密码不在模型的数据中,&#34;更改&#34,将会将用户的密码设置为空白字符串的哈希值;密码。

解决方案

关注the documentation for the version of CakePHP you are using例如:

App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public function beforeSave($options = array()) {
        if (!isset($this->data[$this->alias]['password'])) {
            return true;
        }

        $passwordHasher = new BlowfishPasswordHasher();
        $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
        );
        return true;
    }
}

请务必遵循您正在使用的CakePHP版本的文档,因为Auth组件的行为会随着时间的推移而发生显着变化。

答案 1 :(得分:0)

在UsersTable.php中添加此内容

public function beforeSave(Event $ event)             {                 $ entity = $ event-&gt; data [&#39; entity&#39;];

O(logn)+O(n)+O(nlogn) = O(nlogn^2)