Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. <?php
  2. class User {
  3. private $_db,
  4. $_data,
  5. $_shipdata,
  6. $_sessionName,
  7. $_cookieName,
  8. $_isLoggedIn;
  9.  
  10. public function __construct($user = null) {
  11. $this->_db = DB::getInstance();
  12.  
  13. $this->_sessionName = Config::get('session/session_name');
  14. $this->_cookieName = Config::get('remember/cookie_name');
  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. // process logout
  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');
  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 insertation($table, $fields = array()) {
  49. if(!$this->_db->insert($table, $fields)) {
  50. throw new Exception('There was sa problem inserting some data.');
  51. }
  52. }
  53.  
  54. public function find($user= null) {
  55. if($user) {
  56. $field = (is_numeric($user)) ? 'id' : 'username';
  57. $data = $this->_db->get('users', array($field, '=', $user));
  58.  
  59. if($data->count()) {
  60. $this->_data = $data->first();
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66.  
  67. public function login($username = null, $password = null, $remember = true) {
  68. $user = $this->find($username);
  69. if($user) {
  70. if (password_verify(Input::get('password'), $this->data()->password)) {
  71. Session::put($this->_sessionName, $this->data()->id);
  72.  
  73. if($remember) {
  74. $hash = Hash::unique();
  75. $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
  76.  
  77. if(!$hashCheck->count()) {
  78. $this->_db->insert(array(
  79. 'user_id' => $this->data()->id,
  80. 'hash' => $hash
  81. ));
  82. } else {
  83. $hash = $hashCheck->first()->hash;
  84. }
  85.  
  86. Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
  87.  
  88. }
  89. return true;
  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 shipdata() {
  125. return $this->_shipdata;
  126. }
  127.  
  128. public function isLoggedIn() {
  129. return $this->_isLoggedIn;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement