<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Users extends Admin_Controller_GC
{
public function __construct()
{
parent::__construct();
//only admin with group "1" has access to this controller
if(! $this->ion_auth->in_group(array(1)))
{
redirect();
}
$this->lang->load('users');
$this->lang->load('auth');
}
//users groups list
public function groups()
{
//"groups" table of ion_auth library
$this->table_bd = 'groups';
$this->crud
->unset_add()
->unset_edit()
->unset_delete()
->display_as('description',lang('users__groups'))
->add_action(lang('users__add'),'','admin/users/user_add','ui-icon-grip-dotted-vertical')
->columns('description');
$this->_example_output();
}
//list per user group
public function user_add($group = null)
{
if(!$group) show_404();
$this->table_bd = 'users';
//if new record - set rules required password and username
if(in_array($this->state, array('add','insert','insert_validation')))
{
$this->crud->set_rules('pass', lang('users__password'), 'required')
->set_rules('username', lang('users__login'), 'callback_username_check');
}
//I have only two groups - admin and members
//is admin users list?
if($group == 1)
{
$this->crud
->fields(
'username',
'email',
'pass',
'group_id',
'status'
)
->columns('username','email','status')
->required_fields('email','username','status')
->set_read_fields('username','email');
}
else // members list
{
$this->crud
->fields(
'username',
'address',
'email',
'pass',
'phone',
'group_id',
'status'
)
->set_read_fields(
'username',
'email',
'pass',
'phone',
'address',
'group_id'
)
->required_fields('email',
'username',
'status')
->columns(
'username',
'email',
'status'
);
}
$this->crud
->callback_field('status',array($this, '_status_field'))
->callback_column('status',array($this, '_status_column'))
// only list by certain group
->where('users.group_id', $group)
//hidden group id field
->field_type('group_id','hidden', $group)
//insert update in callback using ion_auth library
->callback_insert(array($this, '_insert_users'))
->callback_update(array($this,'_update_users'))
->set_rules('email','Email','callback_email_check')
->display_as('username',lang('users__login'))
->display_as('pass',lang('users__password'))
->display_as('phone',lang('users__phone'))
->display_as('address',lang('users__address'))
->display_as('email','Email')
->display_as('status',lang('users__status'));
$this->_example_output();
}
// insert user
public function _insert_users($post)
{
$username = isset($post['username']) ? $post['username'] : 'username';
$password = $post['pass'];
$email = $post['email'];
$groups = array($post['group_id']);
if($post['group_id']==2)
{
$insert['group_id'] = $post['group_id'];
$insert['phone'] = $post['phone'];
$insert['address'] = $post['address'];
$insert['status'] = $post['status'];
}
if($id = $this->ion_auth->register($username, $password, $email, $insert, $groups))
{
return TRUE;
}
return FALSE;
}
// edit user
public function _update_users($post, $id = null)
{
$password = trim($post['pass']);
//get ion_auth identity
$identity = $this->db->where('id',$id)
->select($this->config->item('identity', 'ion_auth').',username, status')
->get('users')
->row();
//if we have post password then we need to change it with ion_auth lib
if ( ! empty($password))
{
$is_reset = $this->ion_auth->reset_password($identity->{$this->config->item('identity', 'ion_auth')},$password);
if( ! $is_reset)
{
return false;
}
}
//update user data
$is_update = $this->ion_auth->update($id, $post);
return ($is_update) ? true : false;
}
//"status" field (active, inactive)
public function _status_field($value)
{
if(in_array('read',$this->uri->segment_array())){
return htmlspecialchars(lang("users__$value"), ENT_QUOTES);
} else {
return
"<select name='status' >"
."<option value=''></option>"
."<option value='active'". ($value == 'active' ? 'selected' : '') .">".lang('users__active')."</option>"
."<option value='inactive'". ($value == 'inactive' ? 'selected' : '') .">".lang('users__inactive')."</option>"
."</select>";
}
}
public function _status_column($value)
{
return htmlspecialchars(lang("users__$value"), ENT_QUOTES);
}
//check if username is unique
public function username_check($str)
{
$id = $this->uri->segment(6);
if(!empty($id) && is_numeric($id))
{
$username_old = $this->db->where("id",$id)->get('users')->row()->username;
$this->db->where("username !=",$username_old);
}
$num_row = $this->db->where('username',$str)->get('users')->num_rows();
if ($num_row >= 1)
{
$this->form_validation->set_message('username_check', lang('users__username_error'));
return FALSE;
}
else
{
return TRUE;
}
}
//check if email is unique
public function email_check($str)
{
$id = $this->uri->segment(6);
if(!empty($id) && is_numeric($id))
{
$email_old = $this->db->where("id",$id)->get('users')->row()->email;
$this->db->where("email !=",$email_old);
}
$num_row = $this->db->where('email',$str)->get('users')->num_rows();
if ($num_row >= 1)
{
$this->form_validation->set_message('email_check', lang('users__email_error'));
return FALSE;
}
else
{
return TRUE;
}
}
}
Above I post users controller on the admin site. Hope it helps you.