⚠ 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

managing ion auth tables



Andrey
  • profile picture
  • Member

Posted 25 February 2013 - 07:22 AM

It seems there is no ready solution. Here is the closest thing i got to a working code yet its still not working... validation is not passed and users are not saved or created.

 

 

    
public function users_auth()    {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $crud->set_table('users_auth');
        $crud->set_relation_n_n('groups', 'users_groups', 'groups','user_id','group_id','description');
 
$crud
->columns('first_name', 'last_name', 'email', 'phone', 'company', 'groups', 'active')
->fields('first_name', 'last_name', 'email', 'phone', 'company', 'groups', 'active', 'change_password', 'edit_password', 'edit_password_confirm')
->add_fields('username', 'first_name', 'last_name', 'groups', 'password', 'password_confirm')
->edit_fields('username', 'first_name', 'last_name', 'groups', 'active', 'change_password', 'edit_password', 'edit_password_confirm') 
->change_field_type('change_password', 'true_false')
->change_field_type('edit_password', 'password')
->change_field_type('edit_password_confirm', 'password')
->change_field_type('password', 'password')
->change_field_type('password_confirm', 'password')
->set_rules('change_password', 'Change password', 'callback__change_password_check')
->set_rules('edit_password', 'Password', 'min_length[4]|max_length[20]')
->callback_edit_field('change_password',array($this,'_cb_change_password_field'))
->callback_edit_field('edit_password', array($this, '_cb_empty_password_input_field'))
->callback_edit_field('edit_password_confirm', array($this,'_cb_empty_password_confirm_input_field'))
->callback_before_update(array($this, '_cb_user_password_update'))
->callback_before_insert(array($this, '_cb_user_password_insert'))
->callback_after_update(array($this, '_cb_user_redirect'));
 
 
$output = $crud->render();
 
$this->_admin_output($output);    
}    
 
 
function _change_password_check($change_password)
{
$error = false;
if($change_password == 1)
{
$password = $this->input->post('edit_password');
$password_confirm = $this->input->post('edit_password_confirm');    
$message = $this->lang->line('required');
 
if(empty($password))
{
$message = sprintf($message,'Password');
$error = TRUE;
}
if(empty($password_confirm))
{
$message = sprintf($message,'Confirm password');
$error = TRUE;
}
if($password != $password_confirm)
{
$message = $this->lang->line('matches');
$message = sprintf($message, 'Password', 'Confirm Password');
$error = TRUE;
}
}
if($error == TRUE)
{
$this->form_validation->set_message('_change_password_check', $message);
return false;
}
else
{
return TRUE;
}
 
 
 
function _cb_user_password_insert($post_array, $primary_key)
{
     // because change_password is checked we know that the validation has run
     $data = array(
             'password' => $post_array['password']
     );
 
     // Here we can't use $this->ion_auth->update($primary_key, $data); because the primary key is not known
     // We can't use $this->ion_auth->register because it creates a record and gc tries to create one too !!!
     $post_array['salt']         = $this->config->item('store_salt', 'ion_auth') ? $this->ion_auth->salt() : FALSE;
     $post_array['password']     = $this->ion_auth->hash_password($post_array['password'], $salt);
 
     unset($post_array['password_confirm']);
 
     return $post_array;
}
 
function _cb_empty_password_input_field($value, $primary_key)
{
$data = array(
'name' => 'edit_password',
'type' => 'password',
'id' => 'field-edit_password'
);
return form_input($data);
}
 
function _cb_empty_password_confirm_input_field($value, $primary_key)
{
$data = array(
'name' => 'edit_password_confirm',
'id' => 'field-edit_password_confirm',
'type' => 'password'
);
return form_input($data);
 
function _cb_user_password_update($post_array, $primary_key)
{
// because change_password is checked we know that the validation has run
if($post_array['change_password'] == 1)
{
$data = array(
'password' => $post_array['edit_password']
);
$this->ion_auth->update($primary_key, $data);
}
unset($post_array['change_password']);
unset($post_array['edit_password']);
unset($post_array['edit_password_confirm']);
return $post_array;
}
 
 
 
function _cb_user_redirect($post_array, $primary_key)
{
redirect('main', 'refresh');
}
 
function _cb_change_password_field($value, $primary_key)
{
$data = array(
'id' => 'field_change_password',
'name' => 'change_password'
);
$str = form_checkbox($data, 0);
return $str;

ceroberoz
  • profile picture
  • Member

Posted 02 August 2013 - 20:10 PM

how to include the select user group (or replicate) the function just like in the /view/auth/edit_user?

---edit

apparently set_relation_n_n just works fine for this function :D


Filipovici Tudor
  • profile picture
  • Member

Posted 13 August 2013 - 15:20 PM

function list_users($operation = null) {
		$this -> load -> helper('url');
		$this -> class_name = strtolower(__CLASS__);
		try {
			$crud = new grocery_CRUD();

			$crud -> set_theme('flexigrid');
			$crud -> set_table('users');
			$crud -> set_relation_n_n('groupname', 'users_groups', 'groups', 'user_id', 'group_id', 'name');

			if ($operation == 'edit') {
				$crud -> fields('username', 'first_name', 'last_name', 'password', 'email', 'identifier', 'groupname', 'created_on', 'last_login', 'company',  'active');
				$crud -> change_field_type('created_on', 'readonly');
			} else {
				$crud -> fields('username', 'first_name', 'last_name', 'password', 'email', 'identifier', 'groupname', 'created_on', 'last_login', 'company',  'active');

				$crud -> change_field_type('created_on', 'hidden');
			}
			$crud -> required_fields('username', 'password', 'email', 'groupname', 'active', 'users_group', 'first_name', 'last_name', 'identifier');
			
			$crud	->display_as('username','Username')
             		->display_as('first_name','Nume')
             		->display_as('last_name','Prenume')
             		->display_as('password','Parola')
             		->display_as('email','Email')
             		->display_as('identifier','Identificator(Necesita sa fie unic)')
             		->display_as('groupname','Nume grup')
             		->display_as('created_on','Creat la data')
             		->display_as('last_login','Ultimul login')
             		->display_as('company','Companie')
             		->display_as('active','Activ')
					
					;
			
			
			$crud -> set_rules('password', 'Parola', 'min_length[' . $this -> config -> item('min_password_length', 'ion_auth') . ']|max_length[' . $this -> config -> item('max_password_length', 'ion_auth') . ']');
			$crud -> set_rules('email', 'Email','required|valid_email');
			$crud -> columns('username', 'first_name', 'last_name', 'email', 'identifier', 'groupname', 'created_on', 'last_login', 'social_network', 'active');
			$crud -> callback_column('created_on', array($this, 'datetime'));
			$crud -> callback_column('last_login', array($this, 'datetime'));
			$crud -> change_field_type('last_login', 'readonly');
			// $crud -> change_field_type('ip_address', 'readonly');
			$crud -> change_field_type('password', 'password');
			$crud -> change_field_type('ip_address', 'readonly');

			$crud -> callback_before_insert(array($this, 'insert_encrypt_password_callback'));
			$crud -> callback_before_update(array($this, 'edit_encrypt_password_callback'));
			$crud -> callback_edit_field('password', array($this, 'decrypt_password_callback'));
			// $crud->callback_edit_field('ip_address', array($this, 'ip_address'));
			$crud -> callback_edit_field('created_on', array($this, 'datetime'));
			$crud -> callback_edit_field('last_login', array($this, 'datetime'));

			$data['output'] = $crud -> render();
			$this -> gc_title = 'Administrare Useri';

			$this -> layout -> view($this -> admin_dir . 'gc_data/v_listdata', $data);
		} catch(Exception $e) {
			show_error($e -> getMessage() . ' --- ' . $e -> getTraceAsString());
		}
	}

	function ip_address($value, $row) {
		return @inet_ntop($value);
	}

	function datetime($value, $row) {
		return @date('d M Y H:i:s', $value);
	}

	function insert_encrypt_password_callback($post_array, $primary_key = null) {
		$this -> load -> model('ion_auth_model');

		$post_array['password'] = $this -> ion_auth_model -> hash_password($post_array['password']);
		$post_array['created_on'] = now();

		return $post_array;
	}

	function edit_encrypt_password_callback($post_array, $primary_key = null) {
		$this -> load -> model('ion_auth_model');

		if ($post_array['password'] == 'defaultvalue') {
			unset($post_array['password']);
		} else {
			$post_array['password'] = $this -> ion_auth_model -> hash_password($post_array['password']);
		}
		return $post_array;
	}

	function decrypt_password_callback($value) {
		return "<input type='password' name='password' value='defaultvalue' />";
	}

I`v made it work more simplier,

Note that i have 2 more fields in the above example in user table that you can remove (identifier,social_network).

An also you need to watch $this -> layout -> view($this -> admin_dir . 'gc_data/v_listdata', $data); , i`m using an layout library so you just need to pass the data to view.