Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 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. $this->_sessionName = Config::get('sessions/session_name');
  12. $this->_cookieName = Config::get('remember/cookie_name');
  13.  
  14. if(!$user) {
  15. if(Session::exists($this->_sessionName)) {
  16. $user = Session::get($this->_sessionName);
  17.  
  18. if($this->find($user)) {
  19. $this->isLoggedIn = true;
  20. } else {
  21. //Logout
  22. }
  23. }
  24. } else {
  25. $this->find($user);
  26. }
  27. }
  28.  
  29. public function create($fields = array()) {
  30. if(!$this->_db->insert('users', $fields)) {
  31. throw new Exception('Sorry, there was a problem creating your account;');
  32. }
  33. }
  34. public function delete($fields = array()) {
  35. if(!$this->_db->delete('users', $fields)) {
  36. throw new Exception('Sorry, there was a problem deleting your account;');
  37. }
  38. }
  39. public function update($fields = array(), $id = null) {
  40.  
  41. if(!$id && $this->isLoggedIn()) {
  42. $id = $this->data()->id;
  43. }
  44.  
  45. if(!$this->_db->update('users', $id, $fields)) {
  46. throw new Exception('There was a problem updating');
  47. }
  48. }
  49.  
  50. public function addChild($id,$childID,$leftOrRight){
  51. $errors=0;
  52. if(!strlen($leftOrRight)){
  53. $errors++;
  54. //throw new Exception('There was a problem');
  55. }
  56. $field = ($leftOrRight === '2')? "rightID" : "leftID";
  57. $fields=array(
  58. $field => $childID
  59. );
  60. //var_dump($fields);
  61. $data = $this->_db->get('users', array("username", '=', $id))->first();
  62. if((strlen($this->_db->get('users', array('id', '=', $data->id))->first()->rightID)>3 && $field === "rightID") || (strlen($this->_db->get('users', array('id', '=', $data->id))->first()->leftID)>3 && $field === "leftID")){
  63. $errors++;
  64. //throw new Exception('There was a problems, child exists');
  65. }
  66. //var_dump(strlen($this->_db->get('users', array('id', '=', $data->id))->first()->rightID));
  67. if(!$this->_db->update('users', $data->id, $fields)) {
  68. $errors++;
  69. //throw new Exception('There was a problem assigning child');
  70. }
  71. if($errors>0)return false;
  72. return true;
  73. }
  74.  
  75. public function find($user = null) {
  76. if($user) {
  77. $field = (is_numeric($user)) ? 'id' : 'username';
  78. $data = $this->_db->get('users', array($field, '=', $user));
  79.  
  80. if($data->count()) {
  81. $this->_data = $data->first();
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87.  
  88. public function login($username = null, $password = null, $remember = false) {
  89. if(!$username && !$password && $this->exists()) {
  90. Session::put($this->_sessionName, $this->data()->id);
  91. } else {
  92. $user = $this->find($username);
  93.  
  94. if ($user) {
  95. if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
  96. Session::put($this->_sessionName, $this->data()->id);
  97.  
  98. if ($remember) {
  99. $hash = Hash::unique();
  100. $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
  101.  
  102. if (!$hashCheck->count()) {
  103. $this->_db->insert('users_session', array(
  104. 'user_id' => $this->data()->id,
  105. 'hash' => $hash
  106. ));
  107. } else {
  108. $hash = $hashCheck->first()->hash;
  109. }
  110.  
  111. Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
  112. }
  113.  
  114. return true;
  115. }
  116. }
  117. }
  118. return false;
  119. }
  120.  
  121. public function hasPermission($key) {
  122. $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
  123.  
  124. if($group->count()) {
  125. $permissions = json_decode($group->first()->permissions, true);
  126.  
  127. return !empty($permissions[$key]);
  128. }
  129.  
  130. return false;
  131. }
  132.  
  133. public function exists() {
  134. return (!empty($this->_data)) ? true : false;
  135. }
  136.  
  137. public function logout() {
  138. $this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
  139.  
  140. Session::delete($this->_sessionName);
  141. Cookie::delete($this->_cookieName);
  142. }
  143.  
  144. public function data(){
  145. return $this->_data;
  146. }
  147.  
  148. public function isLoggedIn() {
  149. return $this->isLoggedIn;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement