Hi. I'm Ben Edmunds. This Is Ion Auth
Hi. I'm Ben Edmunds. This Is Ion Auth
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
Server requirements
Installation
benedmunds.com/ion_auth/ 1/15
7/8/2020 Ion Auth
4. Run the appropriate SQL file from the /sql directory.
Upgrading
Upgrading from Ion Auth 2? Check the UPGRADING.md file in the package.
$this->load->library('ion_auth');
Do make sure to load your database connection first, that can be loaded manually or autloaded.
You can also autoload the library.
Configuration Options
Tables
Hash method
Authentication options
site_title
The title of your site, used for email.
admin_email Default is '[email protected]'
Your administrator email address.
default_group Default is 'members'
Name of the default user group.
admin_group Default is 'admin'
Name of the admin group.
identity Default is 'email'
Column to use for uniquely identifing user/logging in/etc. Usual choices are 'email' OR 'username', but any unique key from your
table can be used as identity.
IMPORTANT: If you are changing it from the default (email), update the UNIQUE constraint in your DB.
min_password_length Default is '8'
Minimum length of passwords.
This minimum is not enforced directly by the library.
The controller should define a validation rule to enforce it.
See the Auth controller for an example implementation.
Additional note: the library will fail for empty password or password size above 4096 bytes.
This is an arbitrary (long) value to protect against DOS attack.
email_activation Default is 'false'
TRUE or FALSE. Sets whether to require email activation or not.
manual_activation Default is 'false'
TRUE or FALSE. Sets whether to require manual activation (probably by an admin user) or not.
remember_users Default is 'true'
TRUE or FALSE. Sets whether to enable 'remember me' functionality or not.
user_expire Default is '86500'
Sets how long to remember the user for in seconds. Set to zero for no expiration.
user_extend_on_login Default is 'false'
TRUE or FALSE. Extend the users session expiration on login.
track_login_attempts Default is 'false'
Track the number of failed login attempts for each user or ip (see track_login_ip_address option).
track_login_ip_address Default is 'true'
Track login attempts by IP Address, if FALSE will track based on identity.
maximum_login_attempts Default is 3
Set the maximum number of failed login attempts. This maximum is not enforced by the library, but is used by $this->ion_auth-
>is_max_login_attempts_exceeded(). The controller should check this function and act appropriately. If set to 0, there is no
maximum.
forgot_password_expiration Default is 1800
Number of seconds before a forgot password request expires. If set to 0, requests will not expire.
30 minutes to 1 hour are good values (enough for a user to receive the email and reset its password).
You should not set a value too high (or 0), as it could lead to security issue!
recheck_timer Default is 0
The number of seconds after which the session is checked again against database to see if the user still exists and is active.
Leave 0 if you don't want session recheck. if you really think you need to recheck the session against database, we would
recommend a higher value, as this would affect performance.
Cookie options
Email options
Templates options
Message Delimiters
benedmunds.com/ion_auth/ 3/15
7/8/2020 Ion Auth
error_end_delimiter Default is '</p>'
Ending delimiter for errors.
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()
1. 'Identity' - string REQUIRED. Username, email or any unique value in your users table, depending on your configuration.
2. 'Password' - string REQUIRED.
3. 'Remember' - boolean OPTIONAL. TRUE sets the user to be remembered if enabled in the configuration.
Return
boolean. TRUE if the user was successfully logged in FALSE if the user was not logged in.
Usage
$identity = '[email protected]';
$password = '12345678';
$remember = TRUE; // remember the user
$this->ion_auth->login($identity, $password, $remember);
logout()
$this->ion_auth->logout();
register()
1. 'Identity' - string REQUIRED. This must be the value that uniquely identifies the user when he is registered. If you chose "email"
as $config['identity'] in the configuration file, you must put the email of the new user.
2. 'Password' - string REQUIRED.
3. 'Email' - string REQUIRED.
4. 'Additional Data' - multidimensional array OPTIONAL.
5. 'Group' - array OPTIONAL. If not passed the default group name set in the config will be used.
Return
mixed. The ID of the user if the user was successfully created, FALSE if the user was not created.
Usage
$username = 'benedmunds';
$password = '12345678';
$email = '[email protected]';
$additional_data = array(
'first_name' => 'Ben',
'last_name' => 'Edmunds',
);
$group = array('1'); // Sets user to admin.
create_user()
update()
benedmunds.com/ion_auth/ 4/15
7/8/2020 Ion Auth
Update a user.
Parameters
Return
boolean. TRUE if the user was successfully updated FALSE if the user was not updated.
Usage
$id = 12;
$data = array(
'first_name' => 'Ben',
'last_name' => 'Edmunds',
'password' => '123456789',
);
$this->ion_auth->update($id, $data)
update_user()
delete_user()
Delete a user.
Parameters
Return
boolean. TRUE if the user was successfully deleted FALSE if the user was not deleted.
Usage
$id = 12;
$this->ion_auth->delete_user($id)
forgotten_password()
Return
boolean. TRUE if the users password was successfully reset FALSE if the users password was not reset.
Usage
- this example assumes you have 'email' selected as the identity in config/ion_auth.php
//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'));
benedmunds.com/ion_auth/ 5/15
7/8/2020 Ion Auth
forgotten_password_check()
Return
object / bool. Returns the user record if valid, return FALSE if invalid.
Usage
$user = $this->ion_auth->forgotten_password_check($code);
if ($user)
{
//display the password reset form
}
logged_in()
if (!$this->ion_auth->logged_in())
{
redirect('auth/login');
}
is_admin()
1. 'id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.
Return
boolean. TRUE if the user is an admin FALSE if the user is not an admin.
Usage
if (!$this->ion_auth->is_admin())
{
$this->session->set_flashdata('message', 'You must be an admin to view this page');
redirect('welcome/index');
}
in_group()
Return
boolean. TRUE if the user is in all or any (based on passed param), FALSE otherwise.
Usage
username_check()
Return
boolean. TRUE if the user is registered FALSE if the user is not registered.
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()
Return
boolean. TRUE if the user is registered FALSE if the user is not registered.
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()
Return
boolean. TRUE if the user is registered FALSE if the user is not registered.
Usage
is_max_login_attempts_exceeded()
If login attempt tracking is enabled, checks to see if the number of failed login attempts for this identity or ip address has been
exceeded. The controller must call this method and take any necessary actions. Login attempt limits are not enforced in the library.
Parameters
Return
boolean. TRUE if maximum_login_attempts is exceeded FALSE if not or if login attempts not tracked.
Usage
$identity = '[email protected]';
if ($this->ion_auth->is_max_login_attempts_exceeded($identity))
{
$this->session->set_flashdata('message', 'You have too many login attempts');
redirect('welcome/index');
}
get_attempts_num()
Returns the number of failed login attempts for this identity or ip address.
Parameters
Return
int. The number of failed login attempts for this identity or ip address.
Usage
$identity = '[email protected]';
$num_attempts = $this->ion_auth->get_attempts_num($identity);
increase_login_attempts()
If login attempt tracking is enabled, records another failed login attempt for this identity or ip address. This method is automatically
called during the login() method if the login failed.
Parameters
Usage
$identity = '[email protected]';
$password = '12345678';
if ($this->ion_auth->login($identity, $password) == FALSE) {
$this->ion_auth->increase_login_attempts($identity)
}
clear_login_attempts()
benedmunds.com/ion_auth/ 8/15
7/8/2020 Ion Auth
Clears all failed login attempt records for this identity or this ip address. This method is automatically called during the login() method
if the login succeded.
Parameters
Usage
$identity = '[email protected]';
$password = '12345678';
if ($this->ion_auth->login($identity, $password) == TRUE) {
$this->ion_auth->clear_login_attempts($identity)
}
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
stdClass Object (
[id] => 1
[ip_address] => 127.0.0.1
[username] => administrator
[password] => 59beecdf7fc966e2f17fd8f65a4a9aeb09d4a3d4
[email] => [email protected]
[activation_code] => 19e181f2ccc2a7ea58a2c0aa2b69f4355e636ef4
[forgotten_password_code] => 81dce1d0bc2c10fbdec7a87f1ff299ed7e4c9e4a
[remember_code] => 9d029802e28cd9c768e8e62277c0df49ec65c48c
[created_on] => 1268889823
[last_login] => 1279464628
[active] => 0
[first_name] => Admin
[last_name] => Account
[company] => Some Corporation
[phone] => (123)456-7890
)
Usage
$user = $this->ion_auth->user()->row();
echo $user->email;
users()
1. 'Group IDs, group names, or group IDs and names' - array OPTIONAL. If an array of group ids, of group names, or of group ids
and names are passed (or a single group id or name) this will return the users in those groups.
Return
model object
Usage
benedmunds.com/ion_auth/ 9/15
7/8/2020 Ion Auth
$users = $this->ion_auth->users(array('admin',4,'members'))->result(); // get users from 'admin' group, 'members' group and group with id '4'
$users = $this->ion_auth->users(array('admin',4,'members'))->result(); // get users from 'admin' group, 'members' group and group with id '4'
group()
Get a group.
Parameters
Return
object
Usage
$group_id = 2;
$group = $this->ion_auth->group($group_id)->result();
groups()
$groups = $this->ion_auth->groups()->result();
messages()
Get messages.
Return
string
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;
}
messages_array()
benedmunds.com/ion_auth/ 10/15
7/8/2020 Ion Auth
Get messages as an array.
Return
array
Parameters
1. 'Langify' - boolean OPTIONAL. TRUE means that the messages will be langified.
Usage
$id = 12;
$data = array(
'first_name' => 'Ben',
'last_name' => 'Edmunds',
);
if ($this->ion_auth->update_user($id, $data))
{
$messages = $this->ion_auth->messages_array();
foreach ($messages as $message)
{
echo $message;
}
}
else
{
$errors = $this->ion_auth->errors_array();
foreach ($errors as $error)
{
echo $error;
}
}
get_users_groups()
1. 'Id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.
Return
stdClass Object (
[id] => 1
[name] => admins
[description] => Administrator
)
Usage
$user_groups = $this->ion_auth->get_users_groups($user->id)->result();
add_to_group()
Return
boolean. TRUE if the user was added to group(s) FALSE if the user is not added to group(s).
Usage
remove_from_group()
1. 'Group_id' - NULL, integer or array REQUIRED. NULL will remove the user from all groups.
2. 'User_id' - integer REQUIRED.
benedmunds.com/ion_auth/ 11/15
7/8/2020 Ion Auth
Return
boolean. TRUE if the user was removed from group(s) FALSE if the user is not removed from group(s).
Usage
create_group()
Create a group
Parameters
Return
brand new group_id if the group was created, FALSE if the group creation failed.
Usage
if(!$group)
{
$view_errors = $this->ion_auth->messages();
}
else
{
$new_group_id = $group;
// do more cool stuff
}
update_group()
Return
boolean. TRUE if the group was updated, FALSE if the update failed.
Usage
if(!$group_update)
{
$view_errors = $this->ion_auth->messages();
}
else
{
// do more cool stuff
}
delete_group()
Remove a group. Removes the group details from the configured 'groups' table. Users belonging to the group are stripped of this status
(references to this group are removed from users_groups), but user data itself remains untouched.
Parameters
if(!$group_delete)
{
$view_errors = $this->ion_auth->messages();
}
else
{
// do more cool stuff
}
set_message_delimiters()
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()
$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;
}
errors_array()
1. 'Langify' - boolean OPTIONAL. TRUE means that the error messages will be langified.
benedmunds.com/ion_auth/ 13/15
7/8/2020 Ion Auth
Usage
$id = 12;
$data = array(
'first_name' => 'Ben',
'last_name' => 'Edmunds',
);
if ($this->ion_auth->update_user($id, $data))
{
$messages = $this->ion_auth->messages_array();
foreach ($messages as $message)
{
echo $message;
}
}
else
{
$errors = $this->ion_auth->errors_array();
foreach ($errors as $error)
{
echo $error;
}
}
set_error_delimiters()
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;
}
set_hook()
Set a single or multiple functions to be called when trigged by trigger_events(). See an example here:
https://gist.github.com/657de89b26decda2b2fa
Parameters
Usage
/*
make sure we loaded ion_auth2
The following does not need to go in __construct() it just needs to be set before
you trigger_events().
*/
$event = 'socialpush';
$class = 'Accounts';
$args = array('this is the content of the message', 'billy');
$name = 'activate_sendmail';
$method = 'email';
$this->ion_auth->set_hook($event, $name, $class, $method, $args);
$name = 'call_Twitter';
$method = 'twitter';
$this->ion_auth->set_hook($event, $name, $class, $method, $args);
$name = 'call_MailChimp_API';
$method = 'mailchimp';
benedmunds.com/ion_auth/ 14/15
7/8/2020 Ion Auth
$this->ion_auth->set_hook($event, $name, $class, $method, $args);
$name = 'call_Facebook_API';
$method = 'facebook';
$this->ion_auth->set_hook($event, $name, $class, $method, $args);
$name = 'call_gPlus_API';
$method = 'gplus';
$this->ion_auth->set_hook($event, $name, $class, $method, $args);
}
trigger_events()
Usage
$this->ion_auth->trigger_events('socialpush');
Top of Page
benedmunds.com/ion_auth/ 15/15