Documentation


Ion Auth

Ion Auth is a simple and lightweight authentication library for the CodeIgniter framework

License

Ion Auth is released under the Apache License v2.0. You can read the license here: http://www.apache.org/licenses/LICENSE-2.0

Installation

  1. Download the latest version: http://github.com/benedmunds/CodeIgniter-Ion-Auth/zipball/master
  2. Copy the files from this package to the correspoding folder in your application folder. For example, copy Ion_auth/config/ion_auth.php to system/application/config/ion_auth.php.

The default login is:

Loading Ion Auth

You load Ion Auth just link any other library:

$this->load->library('ion_auth');

You can also autoload the library.

Configuration Options

Ion Auth is extremely configurable. The following configuration options are available:

Using Config File

To change configuration options simply edit the config/ion_auth.php file.

Config

Edit the ion_auth $config array as needed:


Class Function Reference

NOTE: Methods available in the model are called through the controller using PHP5 magic. You should never use ion_auth_model->method() in your applications.

login()

Logs the user into the system.

Parameters

  1. 'Username' - string REQUIRED. Usually username or email but depends on your config.
  2. 'Password' - string REQUIRED.
  3. 'Remember' - boolean OPTIONAL. TRUE sets the user to be remembered if enabled in the config

Return

Usage

		$identity = 'ben.edmunds@gmail.com';
		$password = '12345678';
		$remember = TRUE; // remember the user
		$this->ion_auth->login($identity, $password, $remember);
	

logout()

Logs the user out of the system.

Usage

		$this->ion_auth->logout();
	

register()

Register (create) a new user.

Parameters

  1. 'Username' - string REQUIRED.
  2. 'Password' - string REQUIRED.
  3. 'Email' - string REQUIRED.
  4. 'Additional Data' - multidimensional array REQUIRED.
  5. 'Group Name' - string OPTIONAL. If not passed the default group name set in the config will be used.

Return

Usage

		$username = 'ben.edmunds@gmail.com';
		$password = '12345678';
		$email = 'benedmunds';
		$additional_data = array(
								'first_name' => 'Ben',
								'last_name' => 'Edmunds',
								);
		$group_name = 'users';
		$this->ion_auth->register($username, $password, $email, $additional_data, $group_name)
	

update_user()

Update a user.

Parameters

  1. 'Id' - integer REQUIRED.
  2. 'Data' - multidimensional array REQUIRED.

Return

Usage

		$id = 12;
		$data = array(
					'first_name' => 'Ben',
					'last_name' => 'Edmunds',
					'password' => '123456789',
					 );
		$this->ion_auth->update_user($id, $data)
	

delete_user()

Delete a user.

Parameters

  1. 'Id' - integer REQUIRED.

Return

Usage

		$id = 12;
		$this->ion_auth->delete_user($id)
	

forgotten_password()

Resets a users password by emailing the user a reset code.

Parameters

  1. 'Email' - string REQUIRED.

Return

Usage

		//Working code for this example is in the example Auth controller in the github repo
		function forgot_password()
		{
			$this->form_validation->set_rules('email', 'Email Address', 'required');
			if ($this->form_validation->run() == false) {
				//setup the input
				$this->data['email'] = array('name'    => 'email',
											 'id'      => 'email',
											);
				//set any errors and display the form
				$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
				$this->load->view('auth/forgot_password', $this->data);
			}
			else {
				//run the forgotten password method to email an activation code to the user
				$forgotten = $this->ion_auth->forgotten_password($this->input->post('email'));

				if ($forgotten) { //if there were no errors
					$this->session->set_flashdata('message', $this->ion_auth->messages());
					redirect("auth/login", 'refresh'); //we should display a confirmation page here instead of the login page
				}
				else {
					$this->session->set_flashdata('message', $this->ion_auth->errors());
					redirect("auth/forgot_password", 'refresh');
				}
			}
		}
	

forgotten_password_complete()

Final step of resetting a users password. The user comes to this page from their email.

Parameters

  1. 'Code' - string REQUIRED.

Return

Usage

		//Working code for this example is in the example Auth controller in the github repo
		public function reset_password($code)
		{
			$reset = $this->ion_auth->forgotten_password_complete($code);

			if ($reset) {  //if the reset worked then send them to the login page
				$this->session->set_flashdata('message', $this->ion_auth->messages());
				redirect("auth/login", 'refresh');
			}
			else { //if the reset didnt work then send them back to the forgot password page
				$this->session->set_flashdata('message', $this->ion_auth->errors());
				redirect("auth/forgot_password", 'refresh');
			}
		}
	

logged_in()

Check to see if a user is logged in.

Return

Usage

		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login');
		}
	

is_admin()

Check to see if the currently logged in user is an admin.

Return

Usage

		if (!$this->ion_auth->is_admin())
		{
			$this->session->set_flashdata('message', 'You must be an admin to view this page');
			redirect('welcome/index');
		}
	

is_group()

Check to see if the currently logged in user is in the passed in group.

Parameters

  1. 'Group Name' - string or array of stringsREQUIRED.

Return

Usage

		$group = 'gangstas';
		if (!$this->ion_auth->is_group($group))
		{
			$this->session->set_flashdata('message', 'You must be a gangsta to view this page');
			redirect('welcome/index');
		}
	

username_check()

Check to see if the username is already registered.

Parameters

  1. 'Username' - string REQUIRED.

Return

Usage

		//This is a lame example but it works.  Usually you would use this method with form_validation.
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email = $this->input->post('email');
		$additional_data = array(
								'first_name' => $this->input->post('first_name'),
								'last_name' => $this->input->post('last_name'),
								);
		if (!$this->ion_auth->username_check($username))
		{
			$group_name = 'users';
			$this->ion_auth->register($username, $password, $email, $additional_data, $group_name)
		}
	

email_check()

Check to see if the email is already registered.

Parameters

  1. 'Email' - string REQUIRED.

Return

Usage

		//This is a lame example but it works.  Usually you would use this method with form_validation.
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$email = $this->input->post('email');
		$additional_data = array(
								'first_name' => $this->input->post('first_name'),
								'last_name' => $this->input->post('last_name'),
								);
		if (!$this->ion_auth->email_check($email))
		{
			$group_name = 'users';
			$this->ion_auth->register($username, $password, $email, $additional_data, $group_name)
		}
	

identity_check()

Check to see if the identity is already registered.

Parameters

  1. 'Identity' - string REQUIRED.

Return

Usage

		//This is a lame example but it works.
		$user = $this->ion_auth->get_user();
		$data = array(
					'identity' => $this->input->post('identity'),
					'first_name' => $this->input->post('first_name'),
					'last_name' => $this->input->post('last_name'),
					 );
		if ($data['identity'] === $user->username || $data['identity'] === $user->email || $this->ion_auth->identity_check($data['identity']) === FALSE)
		{
			$this->ion_auth->update_user($user->id, $data)
		}
	

get_user()

Get a user.

Parameters

  1. 'Id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.

Return

Usage

		$user = $this->ion_auth->get_user();
		echo $user->email;
	

get_user_array()

Get a user.

Parameters

  1. 'Id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.

Return

Usage

		$user = $this->ion_auth->get_user_array();
		echo $user['email'];
	

get_user_by_email()

Get a user by their email address.

Parameters

  1. 'Email' - string REQUIRED.

Return

Usage

		$email = 'ben.edmunds@gmail.com';
		$user = $this->ion_auth->get_user_by_email($email);
		echo $user->first_name;
	

get_users()

Get the users.

Parameters

  1. 'Group Name' - string OPTIONAL. If a group is not supplied all users will be returned.

Return

Usage

		$admin_group = 'admin';
		$admin_users = $this->ion_auth->get_users($admin_group);
	

get_users_array()

Get the users.

Parameters

  1. 'Group Name' - string OPTIONAL. If a group is not supplied all users will be returned.

Return

Usage

		$admin_group = 'admin';
		$admin_users = $this->ion_auth->get_users($admin_group);
	

get_newest_users()

Get the newest users.

Parameters

  1. 'Limit' - integer OPTIONAL. Default value is 10.

Return

Usage

		$admin_group = 'admin';
		$newest_admin_users = $this->ion_auth->get_newest_users($admin_group);
	

get_newest_users_array()

Get the newest users.

Parameters

  1. 'Limit' - integer OPTIONAL. Default value is 10.

Return

Usage

		$admin_group = 'admin';
		$newest_admin_users = $this->ion_auth->get_newest_users_array($admin_group);
	

get_active_users()

Get the active users.

Parameters

  1. 'Group name' - string OPTIONAL. If a group is not supplied all users will be returned.

Return

Usage

		$admin_group = 'admin';
		$active_admin_users = $this->ion_auth->get_active_users($admin_group);
	

get_active_users_array()

Get the active users.

Parameters

  1. 'Group name' - string OPTIONAL. If a group is not supplied all users will be returned.

Return

Usage

		$admin_group = 'admin';
		$active_admin_users = $this->ion_auth->get_active_users_array($admin_group);
	

get_inactive_users()

Get the inactive users.

Parameters

  1. 'Group name' - string OPTIONAL. If a group is not supplied all users will be returned.

Return

Usage

		$admin_group = 'admin';
		$inactive_admin_users = $this->ion_auth->get_inactive_users($admin_group);
	

get_inactive_users_array()

Get the inactive users.

Parameters

  1. 'Group name' - string OPTIONAL. If a group is not supplied all users will be returned.

Return

Usage

		$admin_group = 'admin';
		$inactive_admin_users = $this->ion_auth->get_inactive_users_array($admin_group);
	

get_groups()

Get the groups.

Return

Usage

		$groups = $this->ion_auth->get_groups();
	

get_group()

Get a group.

Parameters

  1. 'Id' - integer REQUIRED.

Return

Usage

		$group_id = 2;
		$group = $this->ion_auth->get_group($group_id);
	

get_group_by_name()

Get a group.

Parameters

  1. 'Name' - string REQUIRED.

Return

Usage

		$group_name = 'users';
		$group = $this->ion_auth->get_group_by_name($group_name);
	

messages()

Get messages.

Return

Usage

		$id = 12;
		$data = array(
					'first_name' => 'Ben',
					'last_name' => 'Edmunds',
					 );
		if ($this->ion_auth->update_user($id, $data))
		{
			$messages = $this->ion_auth->messages();
			echo $messages;
		}
		else
		{
			$errors = $this->ion_auth->errors();
			echo $errors;
		}
	

set_message_delimiters()

Set the message delimiters.

Parameters

  1. 'Start Delimiter' - string REQUIRED.
  2. 'End Delimiter' - string REQUIRED.

Usage

		$id = 12;
		$data = array(
					'first_name' => 'Ben',
					'last_name' => 'Edmunds',
					 );
		if ($this->ion_auth->update_user($id, $data))
		{
			$this->ion_auth->set_message_delimiters('<p><strong>','</strong></p>');
			$messages = $this->ion_auth->messages();
			echo $messages;
		}
		else
		{
			$this->ion_auth->set_error_delimiters('<p><strong>','</strong></p>');
			$errors = $this->ion_auth->errors();
			echo $errors;
		}
	

errors()

Get the errors.

Return

Usage

		$id = 12;
		$data = array(
					'first_name' => 'Ben',
					'last_name' => 'Edmunds',
					 );
		if ($this->ion_auth->update_user($id, $data))
		{
			$messages = $this->ion_auth->messages();
			echo $messages;
		}
		else
		{
			$errors = $this->ion_auth->errors();
			echo $errors;
		}
	

set_error_delimiters()

Set the error delimiters.

Parameters

  1. 'Start Delimiter' - string REQUIRED.
  2. 'End Delimiter' - string REQUIRED.

Usage

		$id = 12;
		$data = array(
					'first_name' => 'Ben',
					'last_name' => 'Edmunds',
					 );
		if ($this->ion_auth->update_user($id, $data))
		{
			$this->ion_auth->set_message_delimiters('<p><strong>','</strong></p>');
			$messages = $this->ion_auth->messages();
			echo $messages;
		}
		else
		{
			$this->ion_auth->set_error_delimiters('<p><strong>','</strong></p>');
			$errors = $this->ion_auth->errors();
			echo $errors;
		}
	

extra_where()

Allows extra where field to be used for user fetching/unique checking etc. Basically this allows users to be unique based on one other thing than the identifier which is helpful for sites using multiple domains on a single database.

Parameters

  1. string or array REQUIRED.

String Usage

		$this->ion_auth->extra_where("meta.domain = 'yourmother.com'");
	

Array Usage

		$where = array('meta.domain', 'yourmother.com');
		$this->ion_auth->extra_where($where);
	

extra_set()

Allows extra set field to be used for user registration. This is helpful for sites using multiple domains on a single database.

Parameters

  1. string or array REQUIRED.

String Usage

		$this->ion_auth->extra_set("meta.domain = 'yourmother.com'");
	

Array Usage

		$where = array('meta.domain', 'yourmother.com');
		$this->ion_auth->extra_set($where);