user.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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", "activated", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "verify_token", "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 sendActivationEmail() {
  46. $this->verify_token = bin2hex(random_bytes(16));
  47. $this->commit();
  48. $mail = new Email();
  49. $mail->loadHtmlTemplate("registration");
  50. $mail->setValues([
  51. "name" => $this->first_name,
  52. "link" => PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token
  53. ]);
  54. $mail->setFrom("noreply@check-policy.co.uk", "Check Policy");
  55. $mail->addAddress($this->email, $this->getDisplayName());
  56. $mail->setSubject("Verify your email address");
  57. $mail->send();
  58. return true;
  59. }
  60. public function sendWelcomeEmail() {
  61. $mail = new Email();
  62. $mail->loadHtmlTemplate("welcome");
  63. $mail->setValues([
  64. "name" => $this->first_name,
  65. "link" => PROTOCOL . "://" . DOMAIN . "/account/submit-id"
  66. ]);
  67. $mail->setFrom("noreply@check-policy.co.uk", "Check Policy");
  68. $mail->addAddress($this->email, $this->getDisplayName());
  69. $mail->setSubject("Welcome to Check Policy");
  70. $mail->send();
  71. return true;
  72. }
  73. public function canAdmin() {
  74. return $this->level >= User::LEVEL_ADMIN;
  75. }
  76. public function getDirectory() {
  77. $directory = ROOT_DIR . "/files/users/" . $this->id;
  78. if(!file_exists($directory)) mkdir($directory, 0777, true);
  79. return $directory;
  80. }
  81. public function getLevelText() {
  82. return User::LEVELS[$this->level];
  83. }
  84. public function getLevelClass() {
  85. switch($this->level) {
  86. case User::LEVEL_BANNED: return "danger";
  87. case User::LEVEL_REGISTERED: return "secondary";
  88. case User::LEVEL_UNVERIFIED: return "warning";
  89. case User::LEVEL_DEACTIVATED: return "secondary";
  90. case User::LEVEL_TESTER: return "success";
  91. case User::LEVEL_VENUE: return "info";
  92. case User::LEVEL_CUSTOMER: return "info";
  93. case User::LEVEL_ADMIN: return "info";
  94. case User::LEVEL_SUPERADMIN: return "info";
  95. default:
  96. return "primary";
  97. }
  98. }
  99. public function getGenderText() {
  100. if($this->gender == "M") return "Male";
  101. if($this->gender == "F") return "Female";
  102. return "Unspecified";
  103. }
  104. public function getAge() {
  105. if(is_null($this->date_of_birth)) return "N/A";
  106. $now = new DateTime("now");
  107. $dob = new DateTime($this->date_of_birth);
  108. return $dob->diff($now)->y;
  109. }
  110. public function getDisplayName() {
  111. $parts = array_filter([$this->first_name, $this->last_name]);
  112. return implode(" ", $parts);
  113. }
  114. public function hasPermission($object) {
  115. if($this->level == User::LEVEL_SUPERADMIN) return true;
  116. if($this->level == User::LEVEL_ADMIN) return true;
  117. $customers = $this->getCustomers();
  118. $customer_ids = array_column($customers, "id");
  119. if(is_a($object, "Customer")) {
  120. return in_array($object->id, $customer_ids);
  121. }
  122. if(is_a($object, "Location")) {
  123. return in_array($object->customer_id, $customer_ids);
  124. }
  125. if(is_a($object, "Project")) {
  126. return in_array($object->customer_id, $customer_ids);
  127. }
  128. if(is_a($object, "Template")) {
  129. return in_array($object->customer_id, $customer_ids);
  130. }
  131. return false;
  132. }
  133. public function deleteIDFiles() {
  134. $files = UserFile::query([
  135. "user_id" => $this->id,
  136. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  137. ]);
  138. foreach($files as $file) {
  139. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  140. if(file_exists($path)) unlink($path);
  141. $file->delete();
  142. }
  143. return true;
  144. }
  145. public function getAddresses() {
  146. $addresses = UserAddress::query([
  147. "user_id" => $this->id,
  148. ], [
  149. "sort" => "name"
  150. ]);
  151. return $addresses;
  152. }
  153. public function getCustomers() {
  154. if($this->level >= User::LEVEL_ADMIN) {
  155. return Customer::query([], ["sort" => "name"]);
  156. }
  157. if($this->level == User::LEVEL_CUSTOMER) {
  158. $links = UserCustomer::query(["user_id" => $this->id]);
  159. return Customer::query([
  160. "id" => array_column($links, "customer_id")
  161. ], ["sort" => "name"]);
  162. }
  163. return [];
  164. }
  165. public function getPendingTests() {
  166. return Test::query([
  167. "tester_id" => $this->id,
  168. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED],
  169. ], [
  170. "sort" => "id",
  171. ]);
  172. }
  173. public function getCompletedTests() {
  174. return Test::query([
  175. "tester_id" => $this->id,
  176. "status" => Test::STATUS_COMPLETED,
  177. ], [
  178. "sort" => "id",
  179. ]);
  180. }
  181. public function getPreferredPaymentMethodText() {
  182. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  183. }
  184. }
  185. ?>