Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Auth {
- function __construct()
- {
- session_start();
- }
- /**
- * Set session
- * @param string $key session key
- * @param string $value session value
- */
- public function set($key,$value)
- {
- $_SESSION[$key] = $value;
- }
- /**
- * Get session value
- * @param string $key session key
- * @return session value
- */
- public function get($key)
- {
- return isset( $_SESSION[$key] ) ? $_SESSION[$key] : null;
- }
- /**
- * Unset session
- * @param string $key session key
- * @return none
- */
- public function destroy($key)
- {
- unset($_SESSION[$key]);
- }
- /**
- * Check if user is currently logged in
- * @return boolean if logged in will return true, else redirect to login
- */
- public function is_logged_in()
- {
- $userid = $this->get('userid');
- if (empty($userid))
- {
- $CI = & get_instance();
- $CI->session->set_flashdata('class','warning');
- $CI->session->set_flashdata('message',lang('message_login_failed_session'));
- redirect('staff/login');
- }
- else
- {
- return true;
- }
- }
- /**
- * Check whether user has access to the page or function
- * @param array $access_array allowed access in array
- * @param bool $bool by default, will redirect if no access, set true to return value- boolean
- * @return boolean based on @param bool
- */
- public function has_access($access_array,$bool=false)
- {
- $CI = & get_instance();
- $access = $this->get('access');
- if(empty($access))
- {
- $CI->session->set_flashdata('class','warning');
- $CI->session->set_flashdata('message',lang('message_login_failed_session'));
- redirect('staff/login');
- }
- else
- {
- $can_access = false;
- $access_list = explode(',',$access);
- foreach ($access_list as $item) {
- if (in_array($item, $access_array))
- {
- $can_access = true;
- }
- }
- if ($bool) {
- return $can_access;
- }
- else if (!$can_access)
- {
- $CI->session->set_flashdata('class','danger');
- $CI->session->set_flashdata('message',lang('message_login_failed_no_access'));
- redirect('staff/message');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement