\Auth

Responsible for authentication and authentication of users into the system

Auth Signals

  • auth.login_success
  • auth.logout_success
  • auth.before_logout

Usage:

Summary

Methods
Properties
Constants
authorize()
login()
is_authenticated()
logout()
require_login()
require_perm()
has_role()
has_perm()
has_perms()
encode_password()
compare_passwords()
password_change()
$user
$permissions
$roles
$errors
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$user

$user : null

This will hold the currently logged in user, this is loaded each time this class is loaded.

This variable is not meant to hold the current user through sessions because of how codeigniter works with libraries, That is , this class loaded with every request, meaning this variable is reset with every request. thats where session comes into play.

Session will hold the user identifier only, and this variable is loaded with each request

Type

null

$permissions

$permissions : array

The permissions for the logged in user.

Type

array

$roles

$roles : array

The roles of the logged in user.

Type

array

$errors

$errors : null

Any authentication errors.

Type

null

Methods

authorize()

authorize(string  $username, string  $password, boolean|TRUE  $user_back = TRUE) : mixed

Determines if user has rights to access the system but does not log the in.

useful when authorizing in an enviroment that does not have sessions like mobile api.

Usage

To authorize a user using username and password.

$user = $this->auth->authorize($username, $password);

Parameters

string $username

the username

string $password

the password

boolean|TRUE $user_back

if true, returns the authorized user on success, if false returns boolean true on success.

Returns

mixed

login()

login(string  $username, string  $password, boolean|FALSE  $remember = FALSE) : boolean

Logs the user in and setsup the necessary session data.

Usage

To log in a user using username and password.

$user = $this->auth->login($username, $password);

Parameters

string $username
string $password
boolean|FALSE $remember

Returns

boolean

is_authenticated()

is_authenticated() : boolean

Tests if the user is authorized to use the system and hydrates the the user, role and permission fields.

Returns

boolean

logout()

logout(string  $redirect_url) 

Logs the user out

Parameters

string $redirect_url

url to redirect to on successful logout

require_login()

require_login(string  $route = NULL) 

Ensure that is able to access a controller only if the are logged in,

usage:

call this method inside a controller method to make sure the method is only accessed by logged in users call this method inside a controller constructor to make the whole controller require login

$this->auth->require_login();

Parameters

string $route

the route to redirect to if user is not authenticated

require_perm()

require_perm(string  $permissions, string  $route = NULL) 

Ensure that is able to access a controller only if the are logged in,

usage:

call this method inside a controller method to make sure the method is only accessed by logged in users call this method inside a controller constructor to make the whole controller require login

$this->auth->require_login();

Parameters

string $permissions

the permission to check for.

string $route

(optional)the route to redirect to if user is not authenticated

has_role()

has_role(\(array|\string)  $check_roles) : boolean

Checks if the user has the provide roles, if an array is provide it checks if a user has any one of the roles in the array

Returns false if user is not authenticated or does not have the required roles

USAGE:

To check if user has entrepreneur, investor, admin

$this->auth->has_role(array('entrepreneur', 'investor', 'admin')));

To check for one role

$this->auth->has_role('admin');

Parameters

\(array|\string) $check_roles

(array|string)

Returns

boolean

has_perm()

has_perm(string  $perm) : boolean

Checks if user has the permission specified.

USAGE:

To check if user has permission, can_edit

$this->auth->has_perm('can_edit');

Parameters

string $perm

the permission.

Returns

boolean

has_perms()

has_perms(array  $perms) : boolean

Checks if the user has all the passed in permissions.

USAGE:

To check if user has permission, can_edit, can_delete

$this->auth->has_perm(['can_edit', 'can_delete']);

Parameters

array $perms

a list of permissions.

Throws

\powerauth\AuthExceptions

Returns

boolean

encode_password()

encode_password(string  $plain_password) : mixed

Hashes the users plain password. and returns the hashed password

USAGE:

To encode a plain password

$ncoded_password this->auth->encode_password('homer');

Parameters

string $plain_password

plain password to encode.

Returns

mixed

compare_passwords()

compare_passwords(string  $password1, string  $password2) : boolean

Compare two passwords

Parameters

string $password1
string $password2

Returns

boolean —

True if the passwords are equal

password_change()

password_change(string  $old_password, string  $new_password, string  $new_password_repeat) : boolean|mixed

Allows currently authenticated users to change there passwords. and returns the new password hashed.

USAGE:

$pass = $this->auth->password_change($password_old, $password_new, $password_repeat_new);

Parameters

string $old_password

the users old password.

string $new_password

the new password

string $new_password_repeat

the new password repeated

Returns

boolean|mixed —

returns false if user is not able to change password, or the hash new password if they are able