AclNode :: node() - 找不到由“Array([Aro0.model] => User [Aro0.foreign_key] =>)”标识的Aro节点

时间:2014-06-17 16:50:51

标签: php cakephp acl

user.php的

App::uses('AuthComponent', 'Controller/Component');

类用户扩展AppModel {

public $primaryKey = 'usu_codigo';
public $foreignKey = 'usu_gru_codigo';
public $useTable = 'painel_usuario';

//public $recursive = 0;


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

public $actsAs = array('Acl' => array(
        'type' => 'requester',
        'enabled' => true
));

public function parentNode() {
    if (!$this->id && empty($this->data)) {
        return null;
    }
    if (isset($this->data['User']['usu_gru_codigo'])) {
        $groupId = $this->data['User']['usu_gru_codigos'];
    } else {
        $groupId = $this->field('usu_gru_codigo');
    }
    if (!$groupId) {
        return null;
    } else {
        return array('Group' => array('id' => $groupId));
    }
}

public $name = 'User';
public $validate = array(
    'usu_email' => array(
        'required' => array(
            'rule' => 'notEmpty',
            'required' => 'create'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'required' => 'create'
        ),
        'email' => array('rule' => 'email')
    ),
    'usu_senha' => array(
        'required' => array(
            'rule' => 'notEmpty',
            'required' => 'create'
        ),
        'size' => array(
            'rule' => array(
                'minLength',
                '8'
            ),
            'message' => 'Minimum 8 characters long'
        )
    )
);
public $belongsTo = array(
    'Group' => array(
        'className' => 'Group',
        'foreignKey' => 'usu_gru_codigo'
    )
);

}

UsersController.php

class UsersController extends AppController {

public function beforefilter() {
    parent::beforeFilter();
    $this->Auth->userModel = 'User';
    $this->Auth->allow('add', 'logout');
}

public function isAuthorized() {
    return true;
}

public function bindNode($user) {
    return array(
        'model' => 'Group',
        'foreign_key' => $user['User']['usu_gru_codigo']
    );
}

public function initDB() {
    $group = $this->User->Group;

    // Allow admins to everything
    $group->id = 10;
    $this->Acl->allow($group, 'controllers');

    // allow basic users to log out
    $this->Acl->allow($group, 'controllers/users/logout');

    // we add an exit to avoid an ugly "missing views" error message
    echo "all done";
    exit;
}

public function index() {
    $this->set('users', $this->User->find('all'));
}

public function add() {
    $this->set('groups', $this->User->Group->find('list', array(
                'fields' => array('gru_nome'),
                'order' => array('gru_nome')
    )));
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('Usuário adicionado com sucesso!'));
            return $this->redirect(array('action' => 'login'));
        }
        $this->Session->setFlash(__('Não foi possível adicionar o usuário!'));
    }
}

public function login() {
    if ($this->request->is('post')) {
        $q = $this->User->query('SELECT usu_codigo FROM painel_usuario WHERE usu_email = "' . $this->request->data['User']['usu_email'] . '" AND usu_senha = "' . AuthComponent::password($this->request->data['User']['usu_senha']) . '";');
        $this->request->data['User']['usu_codigo'] = $q[0]['painel_usuario']['usu_codigo'];
        if ($this->Auth->login($this->request->data)) {//o erro que estava dando era por que não estava passado o request->data com parâmetro
            debug($this->request->data);
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Nome de usuário ou senha incorrestos!'));
        }
    }
}

public function logout() {
    if ($this->Auth->logout()) {
        $this->Session->setFlash(__("Adeus!"));
        $this->redirect($this->Auth->redirect());
    }
}

}

Group.php

class Group extends AppModel {

public $primaryKey = 'gru_codigo';
public $useTable = 'painel_grupo';
var $virtualFields = array(
    'id' => 'gru_codigo',
    'name' => 'gru_nome'
);
public $actsAs = array('Acl' => array('type' => 'requester'));

public function parentNode() {
    return null;
}

}

GroupsController.php

<?php
class GroupsController extends AppController {
    public function beforefilter() {
        parent::beforeFilter();
        $this -> Auth -> allow();
    }

    public function index() {
        $this -> set('groups', $this -> Group -> find('all', array('order' => array('Group.gru_nome'))));
    }

    public function add() {
        if ($this -> request -> is('post')) {
            $this -> Group -> create();
            if ($this -> Group -> save($this -> request -> data)) {
                $this -> Session -> setFlash(__('Grupo criado com sucesso!'));
                return $this -> redirect(array('action' => 'index'));
            }
            $this -> Session -> setFlash(__('Não foi possível criar o grupo!'));
        }
    }

}
在StackOverflow中的

已经用Google搜索,发现没有什么可以帮助我。

总是得到错误: AclNode :: node() - 找不到Aro节点“Array([Aro0.model] =&gt;用户[Aro0.foreign_key] =&gt;)”

相信不知何故组的ForeignKey没有被设置......但是我找不到设置它的位置。

2 个答案:

答案 0 :(得分:3)

您的阵列中是否有group_id(当然还有用户名和密码)列,用于登录?我的意思是这一行

$this->Auth->login($this->request->data))

$this->request->data应包含类似

的数组
array('username' => 'my_username', 'password' => 'password', 'group_id' => '1')

来源:http://hashmode.com/aclnode-node-couldn-t-find-aro-node-identified-by-array-aro0-model-group-aro0-foreign_key-cakephp/30

答案 1 :(得分:0)

如果将用户的组手动更改为另一个组,然后尝试更改属于同一组的用户的密码,则会发生此错误;还原该用户的组或删除其组已手动更改的用户。问题将得到解决。