Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.class.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. require_once('Role.class.php');
  3. class User {
  4. public $id;
  5. public $username;
  6. public $hashedPassword;
  7. public $group;
  8. public $admin;
  9. public $join_date;
  10. public $last_seen;
  11. public $theme;
  12. public $profile_image;
  13. public $website;
  14. public $about;
  15. public $blog_title;
  16. public $blog_desc;
  17. public $roles;
  18. //Constructor is called whenever a new object is created.
  19. //Takes an associative array with the DB row as an argument.
  20. function __construct($data, $db) {
  21. $this->id = (isset($data['id'])) ? $data['id'] : "";
  22. $this->username = (isset($data['username'])) ? $data['username'] : "";
  23. $this->hashedPassword = (isset($data['password'])) ? $data['password'] : "";
  24. $this->group = (isset($data['group_name'])) ? $data['group_name'] : "";
  25. $this->admin = (isset($data['site_admin'])) ? (bool) $data['site_admin'] : "";
  26. $this->join_date = (isset($data['join_date'])) ? $data['join_date'] : "";
  27. $this->last_seen = (isset($data['last_seen'])) ? $data['last_seen'] : "";
  28. $this->theme = (isset($data['theme'])) ? $data['theme'] : "";
  29. $this->website = (isset($data['website'])) ? $data['website'] : "";
  30. $this->about = (isset($data['about'])) ? $data['about'] : "";
  31. $this->quote = (isset($data['quote'])) ? $data['quote'] : "";
  32. $this->blog_title = (isset($data['blog_title'])) ? $data['blog_title'] : "";
  33. $this->blog_desc = (isset($data['blog_desc'])) ? $data['blog_desc'] : "";
  34. $this->roles = array();
  35. $user_list = $db->select("user_role as ur JOIN roles as r ON ur.role_id = r.role_id", "ur.user_id=?", array($this->id), "ur.role_id, r.role_name");
  36. $users = array();
  37. foreach ($user_list as $user)
  38. {
  39. if (!is_array($user))
  40. {
  41. $users = array($user_list);
  42. break;
  43. }
  44. array_push($users, $user);
  45. }
  46. foreach ($users as $user)
  47. {
  48. $this->roles[$user["role_name"]] = Role::getRolePerms($user["role_id"], $db);
  49. }
  50. }
  51. public function save($db, $isNewUser = false) {
  52. //if the user is already registered and we're
  53. //just updating their info.
  54. if(!$isNewUser) {
  55. //set the data array
  56. $data = array(
  57. "username" => $this->username,
  58. "password" => $this->hashedPassword,
  59. "last_seen" => date("Y-m-d H:i:s",time()),
  60. "theme" => $this->theme,
  61. "website" => $this->website,
  62. "quote" => $this->quote,
  63. "about" => $this->about,
  64. "blog_title" => $this->blog_title,
  65. "blog_desc" => $this->blog_desc
  66. );
  67. //update the row in the database
  68. $db->update($data, 'users', 'id=?', array($this->id));
  69. }else {
  70. //if the user is being registered for the first time.
  71. $data = array(
  72. "username" => $this->username,
  73. "password" => $this->hashedPassword,
  74. "join_date" => date("Y-m-d H:i:s",time()),
  75. "last_seen" => date("Y-m-d H:i:s",time())
  76. );
  77. $this->id = $db->insert($data, 'users');
  78. $this->join_date = time();
  79. }
  80. return true;
  81. }
  82. public function delete($db) {
  83. //if the user is already registered and we're
  84. //just updating their info.
  85. //if the user is being registered for the first time.
  86. $db->delete('users', 'id=?', array($this->id));
  87. return true;
  88. }
  89. // check if user has a specific privilege
  90. public function hasPrivilege($perm)
  91. {
  92. foreach ($this->roles as $role)
  93. {
  94. if ($role->hasPerm($perm))
  95. {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. // check if a user has a specific role
  102. public function hasRole($role_name)
  103. {
  104. return isset($this->roles[$role_name]);
  105. }
  106. }
  107. ?>