CakePhp2.4:Auth-> login()始终返回false

时间:2013-11-07 20:20:50

标签: cakephp cakephp-2.0 acl cakephp-2.4

虽然我的应用程序比Cake主站点上的Simple ACL Controlled Application有更多的模型和操作,但我已经跟着它写了一两个轻微的添加内容。同样,我无法授权任何登录操作,我不明白为什么。这里的任何帮助都将非常有用,我一直在倾注API,类似stackOverflow问题等等,并且我已经被卡住了。 以下是

的相关代码
  1. AppController的
  2. 用户控制器
  3. 用户模型
  4. Users.login查看
  5. 群组控制器
  6. 群组模型
  7. 更新 - 已解决

    如果这有帮助,请关注stackOverflow上的this回答,我给了它一个旋转: pr(AuthComponent::password($this->data[$this->alias]['password'])); 在调用$this->Auth->login()之前,当然,密码哈希与我的数据库中的密码哈希不匹配(根本)。经过仔细检查,我的password字段被限制为32个字符,实际上它需要36个。真正的facepalm时刻。

    1。 AppController的

    class AppController extends Controller {
        public  $components = array( 'Session',
                                     'Quick',
                                     'Acl',
                                     'Auth' => array(
                                         'loginAction' => array('controller' => 'users', 'action' => 'login'),
                                         'authenticate' => array(
                                             'Form'=> array(
                                                 'fields' => array(
                                                     'username' => 'email',
                                                     'password' => 'password'))),
                                         'authorize' => array(
                                             'Actions' => array('actionPath' => 'controllers')
                                         )
                                     )
        );
        public  $helpers = array( 'Html', 'Form', 'Session', "Js"=>array("SafejQuery"), "Foundation");
    
        public function beforeFilter() {
            $this->Auth->allow('display');
            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
            $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'home');
            $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
        }
    }
    

    2。用户控制器

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index','view');
    }
    
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Session->setFlash(__('Your username or password was incorrect.'));
        }
    }
    
    public function logout() {
        $this->Session->setFlash('Good-Bye');
        $this->redirect($this->Auth->logout());
    }
    
    /* USED FOR ACL ASSIGNMENT ONLY */
    public function initDB() {
        $group = $this->User->Group;
    
        $group->id = ADMINISTRATOR_ID;
        $this->Acl->allow($group, 'controllers');
    
        $group->id = EDUCATOR_ID;
        $this->Acl->allow($group, 'controllers');
    
        $group->id = STUDENT_ID;
        $this->Acl->deny($group, 'controllers');
        $this->Acl->allow($group, 'controllers/Users/home');
        $this->Acl->allow($group, 'controllers/Modules/run');
        $this->Acl->allow($group, 'controllers/Modules/consent');
        echo "all done";
        exit;
    }
    

    第3。用户模型

    // $belongsTo built with bake, but present
        public $actsAs = array('Acl' => array('type' => 'requester'));
    
        public function beforeSave($options = array()) {
            /* Note: I tried to use the SimplePasswordHasher object as per the 2.4 API,
            *  but the class wasn't found (but does exist at 
            *  lib/Cake/Controller/Component/Auth/SimplePasswordHasher.php */
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
            return true;
        }
    
        public function bindNode() {
            $data = AuthComponent::user();
            return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
        }
    
        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;
            } else {
                return array('Group' => array('id' => $groupId));
            }
        }
    

    4。 User.login查看

    <h2>Login</h2>
    <?php
    echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'login')));
    echo $this->Form->input('User.email');
    echo $this->Form->input('User.password');
    echo $this->Form->end('Login');
    ?>
    

    5。群组控制器

    public function beforeFilter() {
            parent::beforeFilter();
        $this->Auth->allow('index','view');
    }
    

    6。小组模型

    public $actsAs = array('Acl' => array('type' => 'requester'));
    
        public function parentNode() {
            return null;
        }
    

1 个答案:

答案 0 :(得分:2)

“注意:我尝试按照2.4 API使用SimplePasswordHasher对象, 但是没有找到这个班级(但确实存在于 lib / Cake / Controller / Component / Auth / SimplePasswordHasher.php“

在尝试使用之前是否包含该文件?

App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
$this->request->data['Login']['password'] = (new SimplePasswordHasher)->hash( $this->request->data['Login']['password'] );