user.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class User extends Model {
  3. protected static $table = "user";
  4. protected static $id_field = "id";
  5. protected static $fillable_fields = ["email", "old_id", "password", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
  6. protected static $field_map = [
  7. ];
  8. // Constants
  9. const TITLES = ["Miss", "Mr", "Ms", "Dr"];
  10. const GENDERS = ["Female", "Male", "Unspecified"];
  11. const LEVEL_BANNED = 0;
  12. const LEVEL_REGISTERED = 1;
  13. const LEVEL_UNVERIFIED = 2;
  14. const LEVEL_DEACTIVATED = 4;
  15. const LEVEL_TESTER = 8;
  16. const LEVEL_VENUE = 16;
  17. const LEVEL_CUSTOMER = 32;
  18. const LEVEL_ADMIN = 64;
  19. const LEVEL_SUPERADMIN = 128;
  20. const LEVELS = [
  21. 0 => "Banned",
  22. 1 => "Registered",
  23. 2 => "Unverified",
  24. 4 => "Deactivated",
  25. 8 => "Tester",
  26. 16 => "Venue",
  27. 32 => "Customer",
  28. 64 => "Admin",
  29. 128 => "Super Admin",
  30. ];
  31. const PAYMENT_METHODS = [
  32. 1 => "Bank Transfer",
  33. 2 => "PayPal",
  34. ];
  35. // Static methods
  36. public static function validatePassword($password) {
  37. $lowercase = preg_match("/[a-z]/", $password);
  38. $uppercase = preg_match("/[A-Z]/", $password);
  39. $number = preg_match("/[0-9]/", $password);
  40. $special = preg_match("/[^0-9a-zA-Z]/", $password);
  41. $length = strlen($password) >= 6;
  42. return ($lowercase && $uppercase && $number && $special && $length);
  43. }
  44. // Object methods
  45. public function canAdmin() {
  46. return $this->level >= User::LEVEL_ADMIN;
  47. }
  48. public function getDirectory() {
  49. $directory = ROOT_DIR . "/files/users/" . $this->id;
  50. if(!file_exists($directory)) mkdir($directory, 0777, true);
  51. return $directory;
  52. }
  53. public function getLevelText() {
  54. return User::LEVELS[$this->level];
  55. }
  56. public function getLevelClass() {
  57. switch($this->level) {
  58. case User::LEVEL_BANNED: return "danger";
  59. case User::LEVEL_REGISTERED: return "secondary";
  60. case User::LEVEL_UNVERIFIED: return "warning";
  61. case User::LEVEL_DEACTIVATED: return "secondary";
  62. case User::LEVEL_TESTER: return "success";
  63. case User::LEVEL_VENUE: return "info";
  64. case User::LEVEL_CUSTOMER: return "info";
  65. case User::LEVEL_ADMIN: return "info";
  66. case User::LEVEL_SUPERADMIN: return "info";
  67. default:
  68. return "primary";
  69. }
  70. }
  71. public function getGenderText() {
  72. if($this->gender == "M") return "Male";
  73. if($this->gender == "F") return "Female";
  74. return "Unspecified";
  75. }
  76. public function getAge() {
  77. if(is_null($this->date_of_birth)) return "N/A";
  78. $now = new DateTime("now");
  79. $dob = new DateTime($this->date_of_birth);
  80. return $dob->diff($now)->y;
  81. }
  82. public function getDisplayName() {
  83. $parts = array_filter([$this->first_name, $this->last_name]);
  84. return implode(" ", $parts);
  85. }
  86. public function hasPermission($object) {
  87. if($this->level == User::LEVEL_SUPERADMIN) return true;
  88. if($this->level == User::LEVEL_ADMIN) return true;
  89. $customers = $this->getCustomers();
  90. $customer_ids = array_column($customers, "id");
  91. if(is_a($object, "Customer")) {
  92. return in_array($object->id, $customer_ids);
  93. }
  94. if(is_a($object, "Location")) {
  95. return in_array($object->customer_id, $customer_ids);
  96. }
  97. if(is_a($object, "Project")) {
  98. return in_array($object->customer_id, $customer_ids);
  99. }
  100. if(is_a($object, "Template")) {
  101. return in_array($object->customer_id, $customer_ids);
  102. }
  103. return false;
  104. }
  105. public function deleteIDFiles() {
  106. $files = UserFile::query([
  107. "user_id" => $this->id,
  108. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  109. ]);
  110. foreach($files as $file) {
  111. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  112. if(file_exists($path)) unlink($path);
  113. $file->delete();
  114. }
  115. return true;
  116. }
  117. public function getAddresses() {
  118. $addresses = UserAddress::query([
  119. "user_id" => $this->id,
  120. ], [
  121. "sort" => "name"
  122. ]);
  123. return $addresses;
  124. }
  125. public function getCustomers() {
  126. if($this->level >= User::LEVEL_ADMIN) {
  127. return Customer::query([], ["sort" => "name"]);
  128. }
  129. if($this->level == User::LEVEL_CUSTOMER) {
  130. $links = UserCustomer::query(["user_id" => $this->id]);
  131. return Customer::query([
  132. "id" => array_column($links, "customer_id")
  133. ], ["sort" => "name"]);
  134. }
  135. return [];
  136. }
  137. public function getPendingTests() {
  138. return Test::query([
  139. "tester_id" => $this->id,
  140. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED],
  141. ], [
  142. "sort" => "id",
  143. ]);
  144. }
  145. public function getCompletedTests() {
  146. return Test::query([
  147. "tester_id" => $this->id,
  148. "status" => Test::STATUS_COMPLETED,
  149. ], [
  150. "sort" => "id",
  151. ]);
  152. }
  153. public function getPreferredPaymentMethodText() {
  154. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  155. }
  156. }
  157. ?>