Guest User

Untitled

a guest
May 22nd, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4. * Library for handling all user-related operations
  5. *
  6. */
  7. class Auth
  8. {
  9.  
  10. private $CI = null;
  11.  
  12. function __construct(){
  13. $this->CI = & get_instance();
  14. $this->CI->load->library('native_session');
  15. }
  16.  
  17. /**
  18. * Handles login operation
  19. *
  20. * @param string $email
  21. * @param string $password
  22. * @return mixed
  23. */
  24. function login($email = null, $password = null) {
  25. if (is_null($email) && is_null($password)) {
  26. $email = $this->CI->input->post('email');
  27. $password = $this->CI->input->post('password');
  28. }
  29.  
  30. $this->CI->load->model("UserModel");
  31. $user = $this->CI->UserModel->findUser($email, $password);
  32. if ($user) {
  33. $this->CI->UserModel->logLastLogin($user->id, $this->CI->input->ip_address());
  34. return $this->setUserData($user);
  35. } else {
  36. return false;
  37. }
  38. }
  39.  
  40. /**
  41. * Handles logout operation
  42. *
  43. */
  44. function logout() {
  45. $this->CI->native_session->sess_destroy();
  46. }
  47.  
  48. /**
  49. * Set user's profile information into session
  50. *
  51. * @param array $user
  52. * @return array
  53. */
  54. public function setUserData($user) {
  55. unset($user["pasword"]);
  56. $this->CI->native_session->set_item('user', $user);
  57. $this->CI->native_session->set_item('loginTime', time());
  58. return $user;
  59. }
  60.  
  61. /**
  62. * Retrieves user's information. Can return full array or just a single value by key
  63. *
  64. * @param string $key
  65. * @return mixed
  66. */
  67. function user($key = null) {
  68. $user = $this->CI->native_session->get_item("user");
  69. if ($user && !is_null($key)) {
  70. return isset($user->$key) ? $user->$key : null;
  71. } else {
  72. return $user;
  73. }
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment