Advertisement
Guest User

User.php

a guest
Oct 22nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <?php
  2. class User {
  3. private $_db,
  4. $_data,
  5. $_sessionName,
  6. $_cookieName,
  7. $_isLoggedIn;
  8.  
  9. public function __construct($user = null) {
  10. $this->_db = DB::getInstance();
  11.  
  12. $this->_sessionName = Config::get('session/session_name');
  13. $this->_cookieName = Config::get('remember/cookie_name');
  14.  
  15. if (!$user) {
  16. if (Session::exists($this->_sessionName)) {
  17. $user = Session::get($this->_sessionName);
  18.  
  19. if ($this->find($user)) {
  20. $this->_isLoggedIn = true;
  21. } else {
  22. // process logout
  23. }
  24.  
  25. }
  26. } else {
  27. $this->find($user);
  28. }
  29. }
  30.  
  31. public function update($fields = array(), $id = null) {
  32.  
  33. if (!$id && $this->_isLoggedIn) {
  34. $id = $this->data()->id;
  35. }
  36.  
  37. if (!$this->_db->update('users', $id, $fields)) {
  38. throw new Exception('There was a problem updating your account.');
  39. }
  40. }
  41.  
  42. public function create($fields = array()) {
  43. if (!$this->_db->insert('users', $fields)) {
  44. throw new Exception('There was a problem creating an account.');
  45. }
  46. }
  47.  
  48. public function find($user = null) {
  49. if ($user) {
  50. $field = (is_numeric($user)) ? 'id' : 'username';
  51. $data = $this->_db->get('users', array($field, '=', $user));
  52.  
  53. if ($data->count()) {
  54. $this->_data = $data->first();
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60.  
  61. public function login($username = null, $password = null, $remember = false) {
  62.  
  63. if (!$username && !$password && $this->exists()) {
  64. Session::put($this->_sessionName, $this->data()->id);
  65. } else {
  66. $user = $this->find($username);
  67. if ($user) {
  68. if($this->data()->password === Hash::make($password, $this->data()->salt)) {
  69. Session::put($this->_sessionName, $this->data()->id);
  70.  
  71. if($remember) {
  72. $hash = Hash::unique();
  73. $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
  74.  
  75. if (!$hashCheck->count()) {
  76. $this->_db->insert('users_session', array(
  77. 'user_id' => $this->data()->id,
  78. 'hash' => $hash
  79. ));
  80. } else {
  81. $hash = $hashCheck->first()->hash;
  82. }
  83.  
  84. Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
  85. }
  86.  
  87. return true;
  88. }
  89. }
  90. }
  91.  
  92. return false;
  93. }
  94.  
  95. public function hasPermission($key) {
  96. $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
  97.  
  98. if ($group->count()) {
  99. $permissions = json_decode($group->first()->permissions, true);
  100.  
  101. if ($permissions[$key] == true) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107.  
  108. public function exists() {
  109. return (!empty($this->_data)) ? true : false;
  110. }
  111.  
  112. public function logout() {
  113.  
  114. $this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
  115.  
  116. Session::delete($this->_sessionName);
  117. Cookie::delete($this->_cookieName);
  118. }
  119.  
  120. public function data() {
  121. return $this->_data;
  122. }
  123.  
  124. public function isLoggedIn() {
  125. return $this->_isLoggedIn;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement