php - CakePHP: Simple Acl Controlled Application - Not allowing me to add new user the groups are missing -
i can't add user application because group_id missing.. followed tutorial: http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html group select empty , need add group created before
my model/user.php
<?php app::uses('appmodel', 'model'); app::uses('authcomponent', 'controller/component'); class user extends appmodel { public $belongsto = array('group'); public $actsas = array('acl' => array('type' => 'requester')); /** validation rules */ public $validate = array( 'username' => array( 'notempty' => array( 'rule' => array('notempty'), ), ), 'password' => array( 'notempty' => array( 'rule' => array('notempty'), ), ), 'group_id' => array( 'numeric' => array( 'rule' => array('numeric'), ), ), ); public $hasmany = array( 'post' => array( 'classname' => 'post', 'foreignkey' => 'user_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderquery' => '', 'counterquery' => '' ) ); public function beforesave($options = array()) { $this->data['user']['password'] = authcomponent::password( $this->data['user']['password'] ); return true; } 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)); } } }
my model/group.php
<?php app::uses('appmodel', 'model'); class group extends appmodel { public $actsas = array('acl' => array('type' => 'requester')); public $validate = array( 'name' => array( 'notempty' => array( 'rule' => array('notempty'), ), ), ); public $hasmany = array( 'user' => array( 'classname' => 'user', 'foreignkey' => 'group_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderquery' => '', 'counterquery' => '' ) ); public function parentnode() { return null; } }
my view/users/add.ctp
<div class="users form"> <?php echo $this->form->create('user'); ?> <fieldset> <legend><?php echo __('add user'); ?></legend> <?php echo $this->form->input('username'); echo $this->form->input('password'); echo $this->form->input('group_id'); ?> </fieldset> <?php echo $this->form->end(__('submit')); ?> </div> <div class="actions"> <h3><?php echo __('actions'); ?></h3> <ul> <li><?php echo $this->html->link(__('list users'), array('action' => 'index')); ?></li> </ul> </div>
and controller/userscontroller.php
<?php app::uses('appcontroller', 'controller'); class userscontroller extends appcontroller { public $components = array('paginator'); public function index() { $this->user->recursive = 0; $this->set('users', $this->paginator->paginate()); } public function view($id = null) { if (!$this->user->exists($id)) { throw new notfoundexception(__('invalid user')); } $options = array('conditions' => array('user.' . $this->user->primarykey => $id)); $this->set('user', $this->user->find('first', $options)); } public function add() { if ($this->request->is('post')) { $this->user->create(); if ($this->user->save($this->request->data)) { $this->session->setflash(__('the user has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->session->setflash(__('the user not saved. please, try again.')); } } } public function edit($id = null) { if (!$this->user->exists($id)) { throw new notfoundexception(__('invalid user')); } if ($this->request->is(array('post', 'put'))) { if ($this->user->save($this->request->data)) { $this->session->setflash(__('the user has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->session->setflash(__('the user not saved. please, try again.')); } } else { $options = array('conditions' => array('user.' . $this->user->primarykey => $id)); $this->request->data = $this->user->find('first', $options); } } public function delete($id = null) { $this->user->id = $id; if (!$this->user->exists()) { throw new notfoundexception(__('invalid user')); } $this->request->onlyallow('post', 'delete'); if ($this->user->delete()) { $this->session->setflash(__('the user has been deleted.')); } else { $this->session->setflash(__('the user not deleted. please, try again.')); } return $this->redirect(array('action' => 'index')); } public function login() { if ($this->request->is('post')) { if ($this->auth->login()) { return $this->redirect($this->auth->redirect()); } $this->session->setflash(__('your username or password incorrect.')); } } public function logout() { //leave empty now. } public function beforefilter() { parent::beforefilter(); // cakephp 2.1 , $this->auth->allow(); } }
in add.ctp
, group_id
field empty... need change add
action as-
public function add() { if ($this->request->is('post')) { $this->user->create(); if ($this->user->save($this->request->data)) { $this->session->setflash(__('the user has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->session->setflash(__('the user not saved. please, try again.')); } } $groups = $this->user->group->find('list'); // add 2 lines set group id $this->set('groups', $groups); }
Comments
Post a Comment