paulpaul003

PHPOOP_classes/User.php

Apr 14th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2. class User
  3. {
  4. private $_db,
  5. $_data,
  6. $_sessionName,
  7. $_isLoggedIn;
  8.  
  9. public function __construct($user = null)
  10. {
  11. $this->_db = DB::getInstance();
  12. $this->_sessionName = Config::get('session/session_name');
  13.  
  14. if(!$user)
  15. {
  16. if(Session::exists($this->_sessionName))
  17. {
  18. $user = Session::get($this->_sessionName);
  19.  
  20. if($this->find($user))
  21. {
  22. $this->_isLoggedIn = true;
  23. }
  24. else
  25. {
  26. //process logout
  27. }
  28. }
  29. else
  30. {
  31. $this->find($user);
  32. }
  33. }
  34. }
  35.  
  36. public function create($table, $fields = array())
  37. {
  38. if(!$this->_db->insert($table, $fields))
  39. {
  40. throw new Exception("There was a problem creating an account.");
  41. }
  42. }
  43.  
  44. public function find($user = null)
  45. {
  46. if($user)
  47. {
  48. $field = (is_numeric($user)) ? 'id' : 'username';
  49. $data = $this->_db->get('users', array($field, '=', $user));
  50.  
  51. if($data->count())
  52. {
  53. $this->_data = $data->first();
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59.  
  60. public function login($username = null, $password = null)
  61. {
  62. $user = $this->find($username);
  63.  
  64. if($user)
  65. {
  66. if($this->data()->password === Hash::make($password, $this->data()->salt))
  67. {
  68. Session::put($this->_sessionName, $this->data()->id);
  69. return true;
  70. }
  71. }
  72.  
  73. return false;
  74. }
  75.  
  76. public function logout()
  77. {
  78. Session::delete($this->_sessionName);
  79. }
  80.  
  81. public function data()
  82. {
  83. return $this->_data;
  84. }
  85.  
  86. public function isLoggedIn()
  87. {
  88. return $this->_isLoggedIn;
  89. }
  90. }
Add Comment
Please, Sign In to add comment