⚠ 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

How to add users to Ion Auth using the standard add page.



kevbo75217

kevbo75217
  • profile picture
  • Member

Posted 31 March 2020 - 11:20 AM

This took a little while to figure out but I got it. This tutorial is going to show you how to encrypt the users password for Ion Auth. 

 

Add this to your crud call. This will change the password field.

$crud->change_field_type('password','password');

Then insert the tow lines into your crud call. The insert will encrypt the password when the add form is submitted and the update will update the password on the form edit. 

$crud->callback_before_insert(array($this,'encrypt_password_callback'));
$crud->callback_before_update(array($this,'encrypt_password_callback'));

Next add the call back function that will actually encrypt the password. No other library is needed since BCrypt is a build in function for PHP. 

function encrypt_password_callback($post_array, $primary_key = null)
	{
		$this->load->helper('security');
		if ($groups == 1)
		{
			$options = ['cost' => 12,];
		}
		else
		{
			$options = ['cost' => 10,];
		}
		$post_array['password'] = password_hash($post_array['password'], PASSWORD_BCRYPT, $options);
		return $post_array;
	}

Also make sure that you set the relation for groups by adding this code so that the password will be encrypted correctly.

$crud->set_relation_n_n('groups', 'users_groups', 'groups', 'user_id', 'group_id', 'name');

If you would like to add avatar images you can add a avatar to the users table in phpMyAdmin. Set the value to varchar 255. Make sure you set the upload path like the example below mine is set to 'assets/uploads/files/avatar'.

$crud->set_field_upload('avatar', 'assets/uploads/files/avatar');

create a call back function so that a default image is loaded if no image is selected. Again the insert will be used on the add form and the update will be used on the edit form. 

$crud->callback_after_update(array($this, 'default_img'));
$crud->callback_after_insert(array($this, 'default_img'));

Here is the callback function (thanks to another post on here). Where it says avatar5.png set that to the filename of the default picture. 

public function default_img($post_array, $primary_key)
	{
		if (!$this->input->post('avatar')) {
			$default_img = array(
				"id" => $primary_key,
				"avatar" => "avatar5.png"
			);

			$this->db->update('users', $default_img, array('id' => $primary_key));
			return true;
		}
	}