Advertisement
Guest User

Untitled

a guest
May 4th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Auth {
  4.  
  5. function __construct()
  6. {
  7. session_start();
  8. }
  9.  
  10. /**
  11. * Set session
  12. * @param string $key session key
  13. * @param string $value session value
  14. */
  15. public function set($key,$value)
  16. {
  17. $_SESSION[$key] = $value;
  18. }
  19.  
  20. /**
  21. * Get session value
  22. * @param string $key session key
  23. * @return session value
  24. */
  25. public function get($key)
  26. {
  27. return isset( $_SESSION[$key] ) ? $_SESSION[$key] : null;
  28. }
  29.  
  30. /**
  31. * Unset session
  32. * @param string $key session key
  33. * @return none
  34. */
  35. public function destroy($key)
  36. {
  37. unset($_SESSION[$key]);
  38. }
  39.  
  40. /**
  41. * Check if user is currently logged in
  42. * @return boolean if logged in will return true, else redirect to login
  43. */
  44. public function is_logged_in()
  45. {
  46. $userid = $this->get('userid');
  47. if (empty($userid))
  48. {
  49. $CI = & get_instance();
  50. $CI->session->set_flashdata('class','warning');
  51. $CI->session->set_flashdata('message',lang('message_login_failed_session'));
  52. redirect('staff/login');
  53. }
  54. else
  55. {
  56. return true;
  57. }
  58.  
  59. }
  60.  
  61. /**
  62. * Check whether user has access to the page or function
  63. * @param array $access_array allowed access in array
  64. * @param bool $bool by default, will redirect if no access, set true to return value- boolean
  65. * @return boolean based on @param bool
  66. */
  67. public function has_access($access_array,$bool=false)
  68. {
  69. $CI = & get_instance();
  70.  
  71. $access = $this->get('access');
  72. if(empty($access))
  73. {
  74. $CI->session->set_flashdata('class','warning');
  75. $CI->session->set_flashdata('message',lang('message_login_failed_session'));
  76. redirect('staff/login');
  77. }
  78. else
  79. {
  80. $can_access = false;
  81. $access_list = explode(',',$access);
  82. foreach ($access_list as $item) {
  83. if (in_array($item, $access_array))
  84. {
  85. $can_access = true;
  86. }
  87. }
  88.  
  89. if ($bool) {
  90. return $can_access;
  91. }
  92.  
  93. else if (!$can_access)
  94. {
  95. $CI->session->set_flashdata('class','danger');
  96. $CI->session->set_flashdata('message',lang('message_login_failed_no_access'));
  97. redirect('staff/message');
  98. }
  99. }
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement