⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Ion_auth User Managment with grocery_crud



stavgian
  • profile picture
  • Member

Posted 03 October 2012 - 19:05 PM

Hello everyone. In order to manage my website Users i compined ion_auth with grocery CRUD.

The point was that i wanted the Administrator could Add Users easily and give them access to specific pages on the website.
So Grocery Crud gave me the solution.

You can increase security by using ion_auth library function like is_admin(), in_group('members') etc.
I am sorry for Greek characters. i hope you understand what i tried to say.

I hope it helps somebody.

[size=5][b]Users Managment[/b][/size]
[php] function users($operation = '') {

if ($this->ion_auth->in_group('members')) {

//Members can only edit his own info
if ($operation == '' || $operation == 'add' || $operation == 'list') {
redirect('back/index');
}
$operation = 'edit';
}

$crud = new grocery_CRUD();
//FORM LAYOUT
$crud->set_subject('Χρήστη');
$crud->columns('department_id', 'username', 'email', 'phone');
$crud->display_as('username', 'Όνομatεπώνυμο')
->display_as('first_name', 'Όνομa')
->display_as('last_name', 'Επίθετο')
->display_as('department_id', 'Τμήμa')
->display_as('phone', 'Τηλέφωνο')
->display_as('password', 'Κωδικός')
->display_as('password_confirm', 'Επιbεbaίωση Κωδικού')
->display_as('last_login', 'Τελευτaίa είσοδος');

$crud->add_fields('department_id', 'first_name', 'last_name', 'email', 'phone', 'password', 'password_confirm');
$crud->edit_fields('department_id', 'first_name', 'last_name', 'email', 'phone', 'last_login');


//VALIDATION
$crud->required_fields('department_id', 'first_name', 'last_name', 'email', 'phone', 'password', 'password_confirm');
$crud->set_rules('email', 'E-mail', 'required|valid_email');
$crud->set_rules('phone', 'Τηλέφωνο', 'required|numeric|exact_length[10]');
$crud->set_rules('password', 'Password', 'required|matches[password_confirm]');

//FIELD TYPES
$crud->change_field_type('last_login', 'readonly');
$crud->change_field_type('last_login', 'readonly');
$crud->change_field_type('password', 'password');
$crud->change_field_type('password_confirm', 'password');

//RELATIONS
$crud->set_relation('department_id', 'sxoles', 'title');

//CALLBACKS
$crud->callback_insert(array($this, 'create_user_callback'));
$crud->callback_update(array($this, 'edit_user_callback'));
$crud->callback_delete(array($this, 'delete_user'));[/b]

if ($this->ion_auth->in_group('members')) {
$crud->change_field_type('department_id', 'hidden');
$crud->change_field_type('last_login', 'hidden');
}

$this->template->title('Διaχειριση Χρηστών');
$this->template->build('admin/grocery_crud', $crud->render());
}[/php]

The Callbacks
[php]function delete_user($primary_key) {

if ($this->ion_auth_model->delete_user($primary_key)) {
return true;
} else {
return false;
}
}

function edit_user_callback($post_array, $primary_key = null) {

$username = $post_array['first_name'] . ' ' . $post_array['last_name'];
$email = $post_array['email'];
$data = array(
'username' => $username,
'email' => $email,
'phone' => $post_array['phone'],
'first_name' => $post_array['first_name'],
'last_name' => $post_array['last_name'],
'department_id' => $post_array['department_id']
);

$this->ion_auth_model->update($primary_key, $data);

return true;
}

function create_user_callback($post_array, $primary_key = null) {

$username = $post_array['first_name'] . ' ' . $post_array['last_name'];
$password = $post_array['password'];
$email = $post_array['email'];
$data = array(
'phone' => $post_array['phone'],
'first_name' => $post_array['first_name'],
'last_name' => $post_array['last_name'],
'department_id' => $post_array['department_id']
);

$this->ion_auth_model->register($username, $password, $email, $data);

return $this->db->insert_id();
}[/php]


[b]You can also Create Permision to pages:[/b]

Using ion_auth user_table,your pages table and the relation table following

SQL USERS_PAGES TABLE:

[sql]-- ----------------------------
-- Table structure for `users_pages`
-- ----------------------------
DROP TABLE IF EXISTS `users_pages`;
CREATE TABLE `users_pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` smallint(5) NOT NULL,
`page_id` smallint(5) NOT NULL,
`priority` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;[/sql]

//function for inserting page access from[b] Admins[/b]
function user_access() {

$crud = new grocery_CRUD();

$crud->set_table('users');
$crud->unset_add();
$crud->unset_delete();

$crud->change_field_type('username', 'readonly');

$crud->columns('username', 'phone', 'email');
$crud->edit_fields('username', 'access', 'id');
$crud->change_field_type('id', 'hidden');
$crud->display_as('username', 'Όνομa Υπεύθυνου');
$crud->display_as('phone', 'Τηλέφωνο');
$crud->display_as('email', 'E-mail');
$crud->display_as('access', 'Πρόσbaση');

$crud->set_relation_n_n('access', 'users_pages', 'pages', 'user_id', 'page_id', 'meta_title');

$this->template->build('admin/grocery_crud', $crud->render());
}




[b]Function Where Members Can edit their Access Pages[/b]

[php]function permission_pages() {

$user = $this->user;

$user_pages = $this->db->get_where('users_pages', array('user_id' => $user->id))->result();

$ok = array();
foreach ($user_pages as $key => $value) {
$ok[] .= ($value->page_id);
}
$ids = (implode(',', $ok));
$crud = new grocery_CRUD;
$crud->set_table('pages');


[b]$crud->where("id in ($ids)");[/b]

$crud->columns('meta_title');
$crud->edit_fields('title', 'meta_title', 'content', 'meta_description', 'status');
$crud->change_field_type('title', 'readonly');
$crud->unset_texteditor('meta_description');
$crud->add_action('Gallery', base_url() . 'assets/uploads/files/icons/photos.png', 'back/pages_gallery', 'class="modal"');
$crud->unset_add();
$crud->unset_delete();


$this->template->build('admin/grocery_crud', $crud->render());
}[/php]



[img]http://easycaptures.com/fs/uploaded/677/6287205901.png[/img]

Andrey
  • profile picture
  • Member

Posted 25 February 2013 - 07:05 AM

Tried this example and the add or edit feature did not work.

 

also it seems department_id is something extra.


Muzaffer Can Colpan
  • profile picture
  • Member

Posted 27 February 2013 - 15:44 PM

Nice.. Thanks