User.php
App::uses('AuthComponent', 'Controller/Component');
class User extends 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!')); } } }
in StackOverflow already googled and found nothing that could help me.
always got the error: AclNode :: node () – Could not find Aro node Identified by “Array ([Aro0.model] => User [Aro0.foreign_key] =>)”
believe that somehow the ForeignKey of groups is not being set … but I can not find where to set it.
Advertisement
Answer
Do you have group_id
(of course with username and password) column in your array, that is used to login ? I mean this line
$this->Auth->login($this->request->data))
$this->request->data
should contain array like
array('username' => 'my_username', 'password' => 'password', 'group_id' => '1')