Guest User

Untitled

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