Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- | Werxe_auth
- |
- | LICENSE
- |
- | This source file is subject to the GPL license that is bundled
- | with this package in the file LICENSE.txt.
- |
- | @package Werxe_auth
- | @copyright Copyright (c) 2012, Bruno Gaspar @ Werxe.net
- | @license http://www.gnu.org/licenses/gpl.txt GPL
- */
- /*
- | This is an Authentication and ACL library, a 2-in-1 package.
- |
- | @author Bruno Gaspar @ Werxe.net <bruno.gaspar@werxe.net>
- | @version 1.0 2012-04-15
- */
- class Werxe_auth
- {
- /*
- |---------------------------------------------------------------------
- | Variables.
- |---------------------------------------------------------------------
- */
- public $userID = 0; // Stores the ID of the current user.
- public $userPerms = array(); // Stores the permissions of the current user.
- public $userRoles = array(); // Stores the roles of the current user.
- public $tables = array(); // Stores the name of the database tables.
- public $message = false; // Stores a success message.
- public $error = false; // Stores a unsuccessful message.
- public $messages = array(); // Stores success messages.
- public $errors = array(); // Stores unsuccessful messages.
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: __construct()
- |---------------------------------------------------------------------
- |
- | Initializer.
- |
- | @access public
- | @return void
- | @author g l a z z
- */
- public function __construct()
- {
- // Instantiate CodeIgniter.
- //
- $this->CI =& get_instance();
- // Load the needed libraries and helpers.
- //
- $this->CI->load->library('email');
- $this->CI->load->library('encrypt');
- $this->CI->load->library('session');
- $this->CI->load->helper('cookie');
- $this->CI->load->helper('language');
- // Load the language file.
- //
- $this->CI->lang->load('werxe_auth');
- // Load the config file.
- //
- $this->CI->load->config('werxe_auth');
- // Assign config variables.
- //
- $this->tables = $this->CI->config->item('tables');
- // Is the auto login feature enabled and we are not logged in ?
- //
- if ( ! $this->logged_in() && config_item('auto_login') && get_cookie('email') && get_cookie('remember_code') ):
- // We have the data, try to login.
- //
- $this->remembered_user();
- endif;
- // Check if the user is logged in.
- //
- if ( $this->logged_in() ):
- // Get all the needed user information.
- //
- if ( ! $this->user() ):
- // The user was not found, or cookie/session was invalid.
- //
- redirect();
- endif;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: login()
- |---------------------------------------------------------------------
- |
- | Login function.
- |
- | @access public
- | @param string
- | @param string
- | @param boolean
- | @return boolean or integer
- | @author g l a z z
- */
- public function login($email = null, $password = null, $remember = false)
- {
- // Sanitaze the passed data.
- //
- $email = $this->sanitize( $email );
- $password = $this->sanitize( $password );
- $remember = (bool) $remember;
- // Do we have the login credentials ?
- //
- if ( ! $email or ! $password ):
- // Set the error message.
- //
- $this->set_error('login_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- // Query the database to see if we find the user.
- //
- $qry = $this->CI->db->where('email', $email)->limit(1)->get( $this->tables['users'] );
- // Check if the user exists.
- //
- if( $qry->num_rows() == 1 ):
- // Get user information.
- //
- $user = $qry->row();
- // Is this user activated ?
- //
- if ( $user->status ):
- // Validate the password.
- //
- if ( $this->check_password($user->password, $password) ):
- // Update last login.
- //
- $this->updateLastLogin($user->id);
- // Save the session.
- //
- $session_data = array(
- 'email' => $user->email,
- 'id' => $user->id,
- 'uid' => $user->id,
- 'user_id' => $user->id
- );
- $this->CI->session->set_userdata($session_data);
- // Remember login details for next visit ?
- //
- if ( $remember && config_item('auto_login') ):
- // Remember the user.
- //
- $this->remember_user($user->id);
- endif;
- // Set the success message.
- //
- $this->set_message('login_successful', null, true);
- // We are done here, return the user id.
- //
- return $user->id;
- else:
- // Set the error message.
- //
- $this->set_error('login_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- // Nope, this user account is not activated.
- //
- else:
- // Set the error message.
- //
- $this->set_error('login_unsuccessful_not_active');
- // We are done here.
- //
- return false;
- endif;
- // This user account doesn't seem to exist.
- //
- else:
- // Set the error message.
- //
- $this->set_error('login_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: logout()
- |---------------------------------------------------------------------
- |
- | Logout function.
- |
- | @access public
- | @return boolean
- | @author g l a z z
- */
- public function logout()
- {
- // Clean up the session.
- //
- $sessions = array(
- 'email' => '' ,
- 'id' => '' ,
- 'uid' => '' ,
- 'user_id' => ''
- );
- $this->CI->session->unset_userdata( $sessions );
- // Delete the remember me cookies, if they exist.
- //
- if ( get_cookie('email') or get_cookie('remember_code') ):
- delete_cookie('email');
- delete_cookie('remember_code');
- endif;
- // Destroy the session.
- //
- $this->CI->session->sess_destroy();
- // Set the success message.
- //
- $this->set_message('logout_successful', null, true);
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: register()
- |---------------------------------------------------------------------
- |
- | Register function.
- |
- | @access public
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function register($params = array())
- {
- // Sanitize the parameters.
- //
- $sanitized = array();
- foreach($params as $key => $value):
- $sanitized[ $key ] = $this->sanitize( $value );
- endforeach;
- // Do we have the $email and $password ?
- //
- if ( ! $sanitized['email'] or ! $sanitized['password'] ):
- // Set the success message.
- //
- $this->set_error('account_creation_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- // Check if email is already registered.
- //
- if ( $this->check_email($sanitized['email']) ):
- // Set the error message.
- //
- $this->set_error('account_creation_duplicate_email');
- // We are done here.
- //
- return false;
- endif;
- // Hash the password.
- //
- $sanitized['password'] = $this->hash($sanitized['password']);
- // Add more data to the sanitized array.
- //
- $sanitized['ip_address'] = sprintf( '%u', ip2long( $this->CI->input->ip_address() ) );
- $sanitized['date_added'] = time();
- $sanitized['status'] = 1;
- // Create the new user.
- //
- $this->CI->db->insert( $this->tables['users'], $sanitized );
- // If the user was created with success.
- //
- if ( $userID = $this->CI->db->insert_id() ):
- // Update this new user roles, most likely insert them into his record.
- //
- if ( ! array_key_exists('customer_roles', $sanitized )):
- $this->userUpdateRolesByName( $userID, $this->CI->config->item('default_roles') );
- else:
- $this->userUpdateRolesByID( $userID, $sanitized['customer_roles'] );
- endif;
- // Do we need to send an activation email ?
- //
- if ( $this->CI->config->item('email_activation') ):
- // Deactivate the new user and get an activation code.
- //
- $activation_code = $this->deactivate($userID);
- // Prepare the data for the activation email.
- //
- $data = array(
- 'id' => $userID,
- 'email' => $sanitized['email'],
- 'activation' => $activation_code,
- );
- // Prepare the email message.
- //
- $message = $this->CI->load->view('auth/emails/activate', $data, true);
- // Prepare the email library.
- //
- $this->CI->email->clear();
- $this->CI->email->set_newline( "\r\n" );
- $this->CI->email->from( $this->CI->config->item('admin_email'), $this->CI->config->item('email_title') );
- $this->CI->email->to( $sanitized['email'] );
- $this->CI->email->subject( $this->CI->config->item('email_title') . ' - Account Activation' );
- $this->CI->email->message( $message );
- // Was the email sended with success ?
- //
- if ($this->CI->email->send() == TRUE):
- // Set the success message.
- //
- $this->set_message('activation_email_successful');
- else:
- // Set the error message.
- //
- $this->set_error('activation_email_unsuccessful');
- endif;
- // No activation email needed.
- //
- else:
- // Set the success message.
- //
- $this->set_message('account_creation_successful');
- endif;
- // We are done here, return the user id.
- //
- return $userID;
- // The user was not created.
- //
- else:
- // Set the success message.
- //
- $this->set_error('account_creation_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: remember_user()
- |---------------------------------------------------------------------
- |
- | This function is used to remember a user login details.
- |
- | @access private
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- private function remember_user( $userID = null )
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Get some information about this user.
- //
- $user = $this->CI->db->where('id', $userID)->get( $this->tables['users'] )->row();
- // Create a new remember code
- //
- $remember_code = sha1( md5( microtime() ) );
- // Update the user record.
- //
- $this->CI->db->where('id', $userID)->update( $this->tables['users'], array('remember_code' => $remember_code) );
- // Was the user updated with success ?
- //
- if ( $this->CI->db->affected_rows() > -1 ):
- // Set the cookies.
- //
- # Email:
- #
- set_cookie(array(
- 'name' => 'email',
- 'value' => $user->email,
- 'expire' => $this->CI->config->item('user_expire'),
- ));
- # Remember code:
- #
- set_cookie(array(
- 'name' => 'remember_code',
- 'value' => $remember_code,
- 'expire' => $this->CI->config->item('user_expire'),
- ));
- // We are done here.
- //
- return true;
- endif;
- // We are done here.
- //
- return false;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: remembered_user()
- |---------------------------------------------------------------------
- |
- | Function to remember users automatically.
- |
- | @access public
- | @return boolean
- | @author g l a z z
- */
- public function remembered_user()
- {
- // Check for valid data.
- //
- if ( ! get_cookie('email') or ! get_cookie('remember_code') ):
- // We are done here.
- //
- return false;
- endif;
- // Get some user information.
- //
- $qry = $this->CI->db
- ->select('email, id')
- ->where('email', get_cookie('email'))
- ->where('remember_code', get_cookie('remember_code'))
- ->limit(1)
- ->get($this->tables['users']);
- // If the user was found, sign them in.
- //
- if ( $qry->num_rows() == 1 ):
- // Get information
- //
- $user = $qry->row();
- // Update this user last login.
- //
- $this->updateLastLogin($user->id);
- // Save the session.
- //
- $session_data = array(
- 'email' => $user->email,
- 'id' => $user->id,
- 'uid' => $user->id,
- 'user_id' => $user->id,
- );
- $this->CI->session->set_userdata($session_data);
- // Extend the users cookies.
- //
- $this->remember_user($user->id);
- // We are done here.
- //
- return true;
- endif;
- // No user found, we are done here.
- //
- return false;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: user()
- |---------------------------------------------------------------------
- |
- | This function is used to get all the user information.
- | It gets:
- | - Personal information;
- | - Roles;
- | - Permissions;
- |
- | @access public
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- public function user( $userID = null )
- {
- // No user id passed ?
- //
- if ( ! $userID ):
- // Okay, get user id from the session.
- //
- $this->userID = $userID = $this->CI->session->userdata('user_id');
- endif;
- // User email.
- //
- $email = $this->CI->session->userdata('email');
- // Get this user personal information.
- //
- if ( $this->user = $this->CI->db->where('id', $userID)->where('email', $email)->get('customers')->row() ):
- // Get this users roles.
- //
- $this->userRoles = $this->getUserRoles( $userID );
- // Get both roles permissions and user permissions.
- //
- $this->userPerms = $this->getRolePerms();
- $this->userPerms = array_merge( $this->userPerms, $this->getUserPerms( $userID ) );
- // User found.
- //
- return true;
- else:
- // Delete cookies and sessions, just to make sure.
- //
- $this->logout();
- // User not found.
- //
- return false;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: check_email()
- |---------------------------------------------------------------------
- |
- | This function is used to check if an email is already registered.
- |
- | @access public
- | @param string
- | @return integer
- | @author g l a z z
- */
- public function check_email($email)
- {
- return $this->CI->db->where('email', $email)->count_all_results( $this->tables['users'] ) > 0;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: logged_in()
- |---------------------------------------------------------------------
- |
- | This function checks if a user is logged id.
- |
- | @access public
- | @return boolean
- | @author g l a z z
- */
- public function logged_in()
- {
- // Set the user id.
- //
- $this->userID = (bool) $this->CI->session->userdata( 'user_id' );
- // Return True / false.
- //
- return (bool) $this->CI->session->userdata( 'email' );
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: updateLastLogin()
- |---------------------------------------------------------------------
- |
- | This function is used to update the last login of a user.
- |
- | @access public
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- public function updateLastLogin( $userID = null )
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Get the ip address.
- //
- $ip_address = $this->CI->input->ip_address();
- // Update the user record.
- //
- $data = array(
- 'last_login' => time(),
- 'ip_address' => sprintf('%u', ip2long( $ip_address ) )
- );
- $this->CI->db->where('id', $userID)->update( $this->tables['users'], $data );
- // Insert a new record on the logins table.
- //
- $data = array(
- 'userID' => $userID,
- 'ip_address' => sprintf('%u', ip2long( $ip_address ) ),
- 'date' => time()
- );
- $this->CI->db->insert( $this->tables['users_logins'], $data);
- // We are done here.
- //
- return $this->CI->db->affected_rows() == 1;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getUserRoles()
- |---------------------------------------------------------------------
- |
- | This function gets all the user roles.
- |
- | @access public
- | @param integer
- | @return array
- | @author g l a z z
- */
- public function getUserRoles( $userID = null)
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Initiate an empty array.
- //
- $return = array();
- // Get the user roles.
- //
- $roles = $this->CI->db
- ->select('u_roles.*, roles.roleName')
- ->from($this->tables['users_roles'] . ' as u_roles')
- ->join('roles', 'roles.id = u_roles.roleID', 'left')
- ->where('u_roles.userID', $userID)
- ->order_by('u_roles.roleOrder', 'ASC')
- ->get()->result();
- // Loop through the records.
- //
- foreach($roles as $role):
- $return[ $role->roleID ] = array(
- 'roleID' => $role->roleID,
- 'userID' => $role->userID,
- 'addDate' => $role->addDate,
- 'roleName' => $role->roleName
- );
- endforeach;
- // Return the user roles.
- //
- return $return;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getUserPerms()
- |---------------------------------------------------------------------
- |
- | This function gets the permissions from a user.
- |
- | @access public
- | @param integer
- | @return array
- | @author g l a z z
- */
- public function getUserPerms( $userID = null )
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Initiate an empty array.
- //
- $return = array();
- // Get the permissions.
- //
- $permissions = $this->CI->db
- ->select('u_perms.*, permissions.permKey, permissions.permName')
- ->from($this->tables['users_perms'] . ' as u_perms')
- ->join('permissions', 'permissions.id = u_perms.permID', 'LEFT')
- ->where('u_perms.userID', $userID)
- ->order_by('u_perms.addDate', 'ASC')
- ->get()->result();
- // Loop through the records.
- //
- foreach($permissions as $perm):
- $return[ $perm->permKey ] = array(
- 'perm' => $perm->permKey,
- 'inheritted' => ( ($perm->value == '1') ? true : false ),
- 'value' => ( ($perm->value == '1') ? true : false ),
- 'name' => $perm->permName,
- 'id' => $perm->permID
- );
- endforeach;
- // Return the permissions.
- //
- return $return;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getAllRoles()
- |---------------------------------------------------------------------
- |
- | This function gets all the roles from the database.
- |
- | @access public
- | @return array
- | @author g l a z z
- */
- public function getAllRoles()
- {
- // Return the roles.
- //
- return $this->CI->db
- ->select('roles.*, COUNT(ur.roleID) as usersActive')
- ->join( $this->tables['users_roles'] . ' as ur', 'ur.roleID = roles.id', 'left')
- ->order_by('roles.order', 'ASC')
- ->group_by('roles.id')
- ->get('roles')->result();
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getAllPerms()
- |---------------------------------------------------------------------
- |
- | This function gets all the permissions from the database.
- |
- | @access public
- | @return array
- | @author g l a z z
- */
- public function getAllPerms()
- {
- // Initiate an empty array.
- //
- $return = array();
- // Get the permissions.
- //
- return $perms = $this->CI->db->order_by('permName', 'ASC')->get('permissions')->result();
- // Loop through the records.
- //
- foreach($perms as $perm):
- $return[ $perm->permKey ] = array(
- 'id' => $perm->id,
- 'permName' => $perm->permName,
- 'permKey' => $perm->permKey
- );
- endforeach;
- // Return the permissions.
- //
- return $return;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getRolePerms()
- |---------------------------------------------------------------------
- |
- | This function gets the permissions from a role or from roles.
- |
- | @access public
- | @param integer | array
- | @return array
- | @author g l a z z
- */
- public function getRolePerms( $role = null )
- {
- // We dont have a role passed ?
- //
- if ( ! $role ):
- $role = $this->userRoles;
- endif;
- // Initiate an empty array.
- //
- $perms = array();
- // Multiple roles ?
- //
- if ( is_array($role) ):
- /*
- // Loop the roles, and get the role permissions.
- //
- foreach($role as $r):
- // Role ID.
- //
- $roleID = $r['roleID'];
- // Get the permissions of this role.
- //
- $teste = $this->CI->db
- ->select('acl_roles_perms.*, permissions.permKey, permissions.permName')
- ->join('permissions', 'permissions.id = acl_roles_perms.permID', 'LEFT')
- ->where('acl_roles_perms.roleID', $roleID )
- //->order_by('acl_roles_perms.roleK', 'DESC')
- ->get('acl_roles_perms')->result();
- $lol[ $roleID ] = $teste;
- endforeach;
- // array_reverse
- $lol = array_reverse($lol, true);
- $perms = array();
- foreach($lol as $roleID => $role):
- foreach($role as $r):
- $perms[ $r->permKey ] = array(
- 'perm' => $r->permKey,
- 'inheritted' => true,
- 'value' => ( ($r->value == '1') ? true : false ),
- 'name' => $r->permName,
- 'id' => $r->permID
- );
- endforeach;
- endforeach;
- return $perms;
- */
- // Get the roles id.
- //
- $roles_id = array();
- foreach($role as $r):
- $roles_id[] = $r['roleID'];
- endforeach;
- // Get the roles permissions.
- //
- $roles = $this->CI->db
- ->select('acl_roles_perms.*, permissions.permKey, permissions.permName')
- ->join('permissions', 'permissions.id = acl_roles_perms.permID', 'LEFT')
- ->where_in('acl_roles_perms.roleID', $roles_id )
- //->order_by('acl_roles_perms.p', 'DESC')
- ->get('acl_roles_perms')->result();
- // No, just only one role.
- //
- else:
- // Get the roles permissions.
- //
- $roles = $this->CI->db
- ->select('acl_roles_perms.*, permissions.permKey, permissions.permName')
- ->join('permissions', 'permissions.id = acl_roles_perms.permID', 'LEFT')
- ->where('acl_roles_perms.roleID', $role)
- //->order_by('acl_roles_perms.value', 'DESC')
- ->get('acl_roles_perms')->result();
- endif;
- // Loop through the records.
- //
- $perms = array();
- foreach($roles as $roleID => $role):
- //echo $roleID . ' - ' . $role->permKey . '<br>';
- $perms[ $role->permKey ] = array(
- 'perm' => $role->permKey,
- /* */'inheritted' => true,
- 'value' => $role->value,
- 'name' => $role->permName,
- 'id' => $role->permID
- );
- endforeach;
- // Return the permissions.
- //
- return $perms;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: createRole()
- |---------------------------------------------------------------------
- |
- | This function create a new role.
- |
- | @access public
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function createRole( $params )
- {
- // Sanitize the parameters.
- //
- $roleName = $this->sanitize( $params['roleName'] );
- $roleDesc = $this->sanitize( $params['roleDesc'] );
- // Create the roleKey.
- //
- $roleKey = $this->makeSlug( $roleName );
- // Check if values are empty.
- //
- if ( empty($roleKey) or empty($roleName) ):
- // Set the error message.
- //
- $this->set_error('role_create_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- // Get the last added role, so we can get the order value.
- //
- $qry = $this->CI->db->select('order')->order_by('order',' DESC')->get('roles')->row();
- $order = $qry->order + 10;
- // Create the new role.
- //
- if ( $this->CI->db->insert('roles', array( 'roleKey' => $roleKey, 'roleName' => $roleName, 'roleDesc' => $roleDesc, 'addDate' => time(), 'order' => $order ) ) ):
- // Get the new Role ID.
- //
- $roleID = $this->CI->db->insert_id();
- // Update the permissions of this role.
- //
- foreach ( $params['rolePerms'] as $permID => $value ):
- // Insert the role permission.
- //
- $this->CI->db->query('INSERT INTO `acl_roles_perms` SET `roleID` = ?, `permID` = ?, `value` = ?, `addDate` = ? ON DUPLICATE KEY UPDATE `value`=`value`', array($roleID, $permID, $value, time() ) );
- endforeach;
- // Set the success message.
- //
- $this->set_message('role_create_successful');
- // We are done here.
- //
- return true;
- else:
- // Set the error message.
- //
- $this->set_error('role_create_duplicate');
- // We are done here.
- //
- return false;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: editRole()
- |---------------------------------------------------------------------
- |
- | This function updates a specific role.
- |
- | @access public
- | @param integer
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function editRole( $roleID = null, $params )
- {
- // No role id ?
- //
- if ( ! $roleID ):
- // We are done here.
- //
- return false;
- endif;
- // Sanitize the parameters.
- //
- $roleName = $this->sanitize( $params['roleName'] );
- $roleDesc = $this->sanitize( $params['roleDesc'] );
- // Create the roleKey.
- //
- $roleKey = $this->makeSlug( $roleName );
- // Check if values are empty.
- //
- if ( empty($roleKey) or empty($roleName) ):
- // Set the success message.
- //
- $this->set_error('role_update_unsuccessful');
- // We are done here.
- //
- return false;
- endif;
- // Update the role record.
- //
- $this->CI->db->where('id', $roleID)->update('roles', array( 'roleKey' => $roleKey, 'roleName' => $roleName, 'roleDesc' => $roleDesc ) );
- // Update the permissions of this role.
- //
- foreach ( $params['rolePerms'] as $permID => $value ):
- // Are we update the role permission.
- //
- $this->CI->db->query('INSERT INTO `acl_roles_perms` SET `roleID` = ?, `permID` = ?, `value` = ?, `addDate` = ? ON DUPLICATE KEY UPDATE `value`= ?', array($roleID, $permID, $value, time(), $value ) );
- endforeach;
- // Set the success message.
- //
- $this->set_message('role_update_successful', null, true);
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: deleteRole()
- |---------------------------------------------------------------------
- |
- | This function deletes a specific role.
- |
- | @access public
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- public function deleteRole( $roleID = null )
- {
- // Does the user have permission to delete roles ?
- //
- if ( ! $this->hasPermission('roles_delete') ):
- // Set the error message.
- //
- $this->set_error('role_crud_no_permission', 'global:delete', true);
- // We are done here.
- //
- return false;
- endif;
- // No role id ?
- //
- if ( ! $roleID ):
- // We are done here.
- //
- return false;
- endif;
- // Is this role protected ?
- //
- if ( in_array($roleID, $this->CI->config->item('protected_roles')) ):
- // Set the error message on the session, so we can redirect the user.
- //
- $this->set_error('role_delete_protected', null, true);
- // We are done here.
- //
- return false;
- endif;
- // Delete all the records related to this role.
- //
- $this->CI->db->where( 'id' , $roleID )->delete( 'roles' );
- $this->CI->db->where( 'roleID' , $roleID )->delete( 'acl_roles_perms' );
- $this->CI->db->where( 'roleID' , $roleID )->delete( $this->tables['users_roles'] );
- // Set the success message.
- //
- $this->set_message('role_delete_successful');
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: roleExists()
- |---------------------------------------------------------------------
- |
- | This function checks if a role really exists on the database.
- |
- | @access public
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- public function roleExists( $roleID = null )
- {
- // No role id ?
- //
- if ( ! $roleID ):
- // We are done here.
- //
- return false;
- endif;
- // Check if role exists.
- //
- return (bool) $this->CI->db->where('id', $roleID)->get('roles')->num_rows;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: orderRoleByID()
- |---------------------------------------------------------------------
- |
- | This function orders a role.
- |
- | @access public
- | @param integer
- | @param string
- | @return boolean
- | @author g l a z z
- */
- public function orderRoleByID($roleID, $order)
- {
- // Update the role order.
- //
- if( $order == 'up'):
- $this->CI->db->query('UPDATE `roles` SET `order`=`order`-15 WHERE `id`=?', array($roleID) );
- else:
- $this->CI->db->query('UPDATE `roles` SET `order`=`order`+15 WHERE `id`=?', array($roleID) );
- endif;
- // Update all the other roles order.
- //
- $order = 10;
- foreach($this->CI->db->order_by('order', 'ASC')->get('roles')->result() as $role):
- $this->CI->db->where('id', $role->id)->update('roles', array('order' => $order) );
- $order += 10;
- endforeach;
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: createPerm()
- |---------------------------------------------------------------------
- |
- | This function creates a new permission.
- |
- | @access public
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function createPerm( $params )
- {
- // Create the new permission.
- //
- if ( $this->CI->db->insert('permissions', array( 'permKey' => $params['permKey'], 'permName' => $params['permName'], 'addDate' => time() ) ) ):
- // Get the new Permission ID.
- //
- return $this->CI->db->insert_id();
- else:
- // We are done here.
- //
- return false;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: editPerm()
- |---------------------------------------------------------------------
- |
- | This function updates a specific permission.
- |
- | @access public
- | @param integer
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function editPerm( $permID = null, $params )
- {
- // No perm id ?
- //
- if ( ! $permID ):
- // We are done here.
- //
- return false;
- endif;
- // Update the role record.
- //
- $this->CI->db->where('id', $permID)->update('permissions', array( 'permKey' => $params['permKey'], 'permName' => $params['permName'] ) );
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: deletePerm()
- |---------------------------------------------------------------------
- |
- | This function deletes a specific permission.
- |
- | @access public
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- public function deletePerm( $permID = null )
- {
- // No perm id ?
- //
- if ( ! $permID ):
- // We are done here.
- //
- return false;
- endif;
- // Delete all the records related to this permission.
- //
- $this->CI->db->where( 'id' , $permID )->delete( 'permissions' );
- $this->CI->db->where( 'permID' , $permID )->delete( 'acl_roles_perms' );
- $this->CI->db->where( 'permID' , $permID )->delete( $this->tables['users_perms'] );
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: hasRole()
- |---------------------------------------------------------------------
- |
- | This function checks if a user has a certain role.
- |
- | @access public
- | @param integer
- | @return boolean
- | @author g l a z z
- */
- public function hasRole( $roleID )
- {
- // Loop through the user roles.
- //
- foreach($this->userRoles as $role):
- // Check if we have that role id.
- //
- if ( $role['roleID'] === $roleID ):
- // We do have.
- //
- return true;
- endif;
- endforeach;
- // We are done here.
- //
- return false;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: hasPermission()
- |---------------------------------------------------------------------
- |
- | This function checks if a user has a certain permission key.
- |
- | @access public
- | @param string
- | @return boolean
- | @author g l a z z
- */
- public function hasPermission( $permKey )
- {
- // Make tne permkey lowercase.
- //
- $permKey = strtolower( $permKey );
- // Check if we have permission or not.
- //
- if ( array_key_exists( $permKey, $this->userPerms ) ):
- if ( $this->userPerms[$permKey]['value'] === '1' || $this->userPerms[$permKey]['value'] === true ):
- // We have access :)
- //
- return true;
- else:
- // No access, we are done here.
- //
- return false;
- endif;
- else:
- // No access, we are done here.
- //
- return false;
- endif;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getPermKeyFromID()
- |---------------------------------------------------------------------
- |
- | This function gets a permission key.
- |
- | @access public
- | @param integer
- | @return string
- | @author g l a z z
- */
- public function getPermKeyFromID( $permID )
- {
- // Query the database.
- //
- $perm = $this->CI->db->select('permKey')->where('id', $permID)->limit(1)->get('permissions')->row();
- // Return the permission key.
- //
- return $perm->permKey;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getPermNameFromID()
- |---------------------------------------------------------------------
- |
- | This function gets a permission name.
- |
- | @access public
- | @param integer
- | @return string
- | @author g l a z z
- */
- public function getPermNameFromID( $permID )
- {
- // Query the database.
- //
- $perm = $this->CI->db->select('permName')->where('id', $permID)->limit(1)->get('permissions')->row();
- // Return the permission name.
- //
- return $perm->permName;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getRoleInfoFromID()
- |---------------------------------------------------------------------
- |
- | This function gets a role name.
- |
- | @access public
- | @param string
- | @return boolean
- | @author g l a z z
- */
- public function getRoleInfoFromID( $roleID )
- {
- // Query the database.
- //
- return $this->CI->db->where('id', $roleID)->limit(1)->get('roles')->row();
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getPermInfoFromID()
- |---------------------------------------------------------------------
- |
- | This function gets a role name.
- |
- | @access public
- | @param string
- | @return boolean
- | @author g l a z z
- */
- public function getPermInfoFromID( $permID )
- {
- // Query the database.
- //
- return $this->CI->db->where('id', $permID)->limit(1)->get('permissions')->row();
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: userUpdateRolesByID()
- |---------------------------------------------------------------------
- |
- | This function updates the users roles giving its id's.
- |
- | @access public
- | @param integer
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function userUpdateRolesByID( $userID = null, $roles )
- {
- echo 'userUpdateRolesByID();';
- die;
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Check if there are any roles.
- //
- if ( ! is_array( $roles ) ):
- // No roles, we are done here.
- //
- return false;
- endif;
- // Loop through the roles.
- //
- foreach( $roles as $roleID => $value ):
- // We want to delete or update ?
- //
- if ( $value == '0' ):
- $this->CI->db->where('userID', $userID)->where('roleID', $roleID)->delete( $this->tables['users_roles'] );
- else:
- $this->CI->db->query('REPLACE INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ?', array( $userID, $roleID, time() ) );
- endif;
- endforeach;
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: userUpdateRolesByName()
- |---------------------------------------------------------------------
- |
- | This function updates the users roles giving its id's.
- |
- | @access public
- | @param integer
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function userUpdateRolesByName( $userID = null, $roles)
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Check if there are any roles.
- //
- if ( ! is_array( $roles ) ):
- // No roles, we are done here.
- //
- return false;
- endif;
- // Loop through the roles.
- //
- foreach( $roles as $roleName ):
- // Get the ID of this role.
- //
- $roleID = $this->getRoleIDbyName( $roleName );
- //$this->CI->db->query('REPLACE INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ?', array( $userID, $roleID, time() ) );
- $this->CI->db->query('INSERT INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ? ON DUPLICATE KEY UPDATE `roleID`=`roleID`', array($userID, $roleID, time() ) );
- endforeach;
- // Loop through the roles.
- //
- /*
- foreach( $roles as $roleID => $value ):
- // We want to delete or update ?
- //
- if ( $value == '0' ):
- $this->CI->db->where('userID', $userID)->where('roleID', $roleID)->delete( $this->tables['users_roles'] );
- else:
- $this->CI->db->query('REPLACE INTO `' . $this->tables['users_roles'] . '` SET `userID` = ?, `roleID` = ?, `addDate` = ?', array( $userID, $roleID, date("Y-m-d H:i:s") ) );
- endif;
- endforeach;
- */
- // We are done here.
- //
- return true;
- }
- /*
- |---------------------------------------------------------------------
- | Function: userUpdatePermissions()
- |---------------------------------------------------------------------
- |
- | This function updates the users permissions.
- |
- | @access public
- | @param integer
- | @param array
- | @return boolean
- | @author g l a z z
- */
- public function userUpdatePermissions( $userID = null, $permissions )
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Check if there are any roles.
- //
- if ( ! is_array( $permissions ) ):
- // No roles, we are done here.
- //
- return false;
- endif;
- // Loop through the roles.
- //
- foreach( $permissions as $permID => $value ):
- // We want to delete or update ?
- //
- if ( $value == 'i' ):
- $this->CI->db->where('userID', $userID)->where('permID', $permID)->delete('user_perms');
- else:
- $this->CI->db->query('REPLACE INTO `user_perms` SET `userID` = ?, `permID` = ?, `value`= ?, `addDate` = ?', array( $userID, $permID, $value, time() ) );
- endif;
- endforeach;
- // We are done here.
- //
- return true;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getUserLogins()
- |---------------------------------------------------------------------
- |
- | This function some successfull logins that a user have made.
- |
- | @access public
- | @param integer
- | @param integer
- | @return string
- | @author g l a z z
- */
- public function getUserLogins($userID = null, $logins = 10)
- {
- // Don't have the userID ?
- //
- if( $userID === null ):
- // Use the id of the current logged in user.
- //
- $userID = $this->userID;
- endif;
- // Return the user logins.
- //
- return $this->CI->db->where('userID', $userID)->order_by('date', 'DESC')->limit($logins)->get($this->tables['users_logins'])->result();
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getCountryByID()
- |---------------------------------------------------------------------
- |
- | This function returns the name of a country.
- |
- | @access public
- | @param integer
- | @return string
- | @author g l a z z
- */
- public function getCountryByID( $countryID )
- {
- // Query the database.
- //
- $qry = $this->CI->db->where('id', $countryID)->get('countries')->row();
- // Return the name of the country.
- //
- return $qry->name;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: getRoleIDbyName()
- |---------------------------------------------------------------------
- |
- | This function returns the id of a role.
- |
- | @access public
- | @param integer
- | @return string
- | @author g l a z z
- */
- public function getRoleIDbyName( $roleName )
- {
- // Query the database.
- //
- $role = $this->CI->db->where('roleName', $roleName)->get('roles')->row();
- // Return the RoleID.
- //
- return $role->id;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: makeSlug()
- |---------------------------------------------------------------------
- |
- | This is used to create more userfriendly url's.
- |
- | @access public
- | @param string
- | @param string
- | @return string
- | @author g l a z z
- */
- public function makeSlug($str, $delimiter = '_')
- {
- // Do some work on the string.
- //
- $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
- $str = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
- $str = preg_replace("/[\/_|+ -]+/", $delimiter, $str);
- $str = strtolower(trim($str, '-'));
- // Return the string.
- //
- return $str;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: sanitize()
- |---------------------------------------------------------------------
- |
- | This function sanitizes some string.
- |
- | @access public
- | @param string
- | @return string
- | @author g l a z z
- */
- public function sanitize( $str )
- {
- // We have an array ? No problem...
- //
- if ( is_array( $str ) ):
- // Loop through the array.
- //
- foreach ( $str as $s):
- // Sanitize this string.
- $this->sanitize($s);
- endforeach;
- // We are done here.
- //
- return true;
- endif;
- // Sanitize the string.
- //
- $str = ini_get( 'magic_quotes_gpc' ) ? stripslashes( $str ) : $str;
- $str = strip_tags( $str );
- $str = trim( $str );
- $str = htmlspecialchars( $str );
- //$str = mysql_real_escape_string( $str );
- // Return the sanitized string.
- //
- return $str;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: salt()
- |---------------------------------------------------------------------
- |
- | Generates a new salt.
- |
- | @access private
- | @param integer
- | @return string
- | @author g l a z z
- */
- private static function salt( $length = 22 )
- {
- // Return the new salt.
- //
- return substr( sha1( mt_rand() ), 0, $length );
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: hash()
- |---------------------------------------------------------------------
- |
- | Generates the hashed password.
- |
- | @access public
- | @param string
- | @return string
- | @author g l a z z
- */
- public static function hash( $password )
- {
- // Return the hashed password.
- //
- return crypt($password, '$2a$10$' . self::salt());
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: check_password()
- |---------------------------------------------------------------------
- |
- | This will be used to compare a password against a hash.
- |
- | @access private
- | @param string
- | @param string
- | @return boolean
- | @author g l a z z
- */
- public function check_password( $hash, $password )
- {
- // Get the salt of this password.
- //
- $full_salt = substr( $hash, 0, 29 );
- // Obtain a new hash.
- //
- $new_hash = crypt( $password, $full_salt );
- // Compare both hash's and return true or false.
- //
- return (bool) ( $hash == $new_hash );
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: set_error()
- |---------------------------------------------------------------------
- |
- | Function to set error messages.
- |
- | @access public
- | @param string
- | @param string | array
- | @param boolean
- | @return string
- | @author g l a z z
- */
- public function set_error($line, $args = null, $set_flashdata = false)
- {
- // Prepare the line.
- //
- $line = lang($line);
- // We have arguments ?
- //
- if ( $args ):
- // Do we have only one argument ?
- //
- if ( ! is_array($args) ):
- // Set the error.
- //
- $this->error = sprintf( $line, lang($args) );
- // Nope, we have multiple arguments.
- //
- else:
- // Loop through the arguments.
- //
- $arr = array();
- foreach($args as $arg):
- $arr[] = lang( $arg );
- endforeach;
- // Set the error.
- //
- $this->error = vsprintf($line, $arr);
- endif;
- // No arguments passed.
- //
- else:
- $this->error = $line;
- endif;
- // Set flashdata, just if the user gets redirected :)
- //
- if ( $set_flashdata ):
- $this->CI->session->set_flashdata('error', $this->error);
- endif;
- // Return the error message.
- //
- return $this->show_error();
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: set_message()
- |---------------------------------------------------------------------
- |
- | Function to set success messages.
- |
- | @access public
- | @param string
- | @param string | array
- | @param boolean
- | @return string
- | @author g l a z z
- */
- public function set_message($line, $args = null, $set_flashdata = false)
- {
- // Prepare the line.
- //
- $line = lang($line);
- // We have arguments ?
- //
- if ( $args ):
- // Do we have only one argument ?
- //
- if ( ! is_array($args) ):
- // Set the message.
- //
- $this->message = sprintf( $line, lang($args) );
- // Nope, we have multiple arguments.
- //
- else:
- // Loop through the arguments.
- //
- $arr = array();
- foreach($args as $arg):
- $arr[] = lang( $arg );
- endforeach;
- // Set the message.
- //
- $this->message = vsprintf($line, $arr);
- endif;
- // No arguments passed.
- //
- else:
- $this->message = $line;
- endif;
- // Set flashdata, just if the user gets redirected :)
- //
- if ( $set_flashdata ):
- $this->CI->session->set_flashdata('message', $this->message);
- endif;
- // Return the success message.
- //
- return $this->show_message();
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: show_error()
- |---------------------------------------------------------------------
- |
- | Function to return a error message or multiple error messages.
- |
- | @access public
- | @param string
- | @return string
- | @author g l a z z
- */
- public function show_error()
- {
- // Single error ?
- //
- if ( $this->error ):
- return $this->error;
- // Do we have Session Flashdata ?
- //
- elseif ( $this->CI->session->flashdata('error') ):
- return $this->CI->session->flashdata('error');
- endif;
- // No errors, we are done here.
- //
- return false;
- }
- /* end */
- /*
- |---------------------------------------------------------------------
- | Function: show_message()
- |---------------------------------------------------------------------
- |
- | Function to return a success message.
- |
- | @access public
- | @param string
- | @return string
- | @author g l a z z
- */
- public function show_message()
- {
- // Single success message ?
- //
- if ( $this->message ):
- return $this->message;
- // Do we have Session Flashdata ?
- //
- elseif ( $this->CI->session->flashdata('message') ):
- return $this->CI->session->flashdata('message');
- endif;
- // No success messages, we are done here.
- //
- return false;
- }
- /* end */
- }
- /* end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement