user.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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", "reset_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 sendPasswordResetEmail() {
  74. $this->reset_token = bin2hex(random_bytes(16));
  75. $this->commit();
  76. $mail = new Email();
  77. $mail->loadHtmlTemplate("password_reset");
  78. $mail->setValues([
  79. "name" => $this->first_name,
  80. "link" => PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token
  81. ]);
  82. $mail->setFrom("noreply@check-policy.co.uk", "Check Policy");
  83. $mail->addAddress($this->email, $this->getDisplayName());
  84. $mail->setSubject("Password reset instructions");
  85. $mail->send();
  86. return true;
  87. }
  88. public function canAdmin() {
  89. return $this->level >= User::LEVEL_ADMIN;
  90. }
  91. public function getDirectory() {
  92. $directory = ROOT_DIR . "/files/users/" . $this->id;
  93. if(!file_exists($directory)) mkdir($directory, 0777, true);
  94. return $directory;
  95. }
  96. public function getLevelText() {
  97. return User::LEVELS[$this->level];
  98. }
  99. public function getLevelClass() {
  100. switch($this->level) {
  101. case User::LEVEL_BANNED: return "danger";
  102. case User::LEVEL_REGISTERED: return "secondary";
  103. case User::LEVEL_UNVERIFIED: return "warning";
  104. case User::LEVEL_DEACTIVATED: return "secondary";
  105. case User::LEVEL_TESTER: return "success";
  106. case User::LEVEL_VENUE: return "info";
  107. case User::LEVEL_CUSTOMER: return "info";
  108. case User::LEVEL_ADMIN: return "info";
  109. case User::LEVEL_SUPERADMIN: return "info";
  110. default:
  111. return "primary";
  112. }
  113. }
  114. public function getGenderText() {
  115. if($this->gender == "M") return "Male";
  116. if($this->gender == "F") return "Female";
  117. return "Unspecified";
  118. }
  119. public function getAge() {
  120. if(is_null($this->date_of_birth)) return "N/A";
  121. $now = new DateTime("now");
  122. $dob = new DateTime($this->date_of_birth);
  123. return $dob->diff($now)->y;
  124. }
  125. public function getDisplayName() {
  126. $parts = array_filter([$this->first_name, $this->last_name]);
  127. return implode(" ", $parts);
  128. }
  129. public function hasPermission($object) {
  130. if($this->level == User::LEVEL_SUPERADMIN) return true;
  131. if($this->level == User::LEVEL_ADMIN) return true;
  132. $customers = $this->getCustomers();
  133. $customer_ids = array_column($customers, "id");
  134. if(is_a($object, "Customer")) {
  135. return in_array($object->id, $customer_ids);
  136. }
  137. if(is_a($object, "Location")) {
  138. return in_array($object->customer_id, $customer_ids);
  139. }
  140. if(is_a($object, "Project")) {
  141. return in_array($object->customer_id, $customer_ids);
  142. }
  143. if(is_a($object, "Template")) {
  144. return in_array($object->customer_id, $customer_ids);
  145. }
  146. return false;
  147. }
  148. public function deleteIDFiles() {
  149. $files = UserFile::query([
  150. "user_id" => $this->id,
  151. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  152. ]);
  153. foreach($files as $file) {
  154. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  155. if(file_exists($path)) unlink($path);
  156. $file->delete();
  157. }
  158. return true;
  159. }
  160. public function getAddresses() {
  161. $addresses = UserAddress::query([
  162. "user_id" => $this->id,
  163. ], [
  164. "sort" => "name"
  165. ]);
  166. return $addresses;
  167. }
  168. public function getCustomers() {
  169. if($this->level >= User::LEVEL_ADMIN) {
  170. return Customer::query([], ["sort" => "name"]);
  171. }
  172. if($this->level == User::LEVEL_CUSTOMER) {
  173. $links = UserCustomer::query(["user_id" => $this->id]);
  174. return Customer::query([
  175. "id" => array_column($links, "customer_id")
  176. ], ["sort" => "name"]);
  177. }
  178. return [];
  179. }
  180. public function getPendingTests() {
  181. return Test::query([
  182. "tester_id" => $this->id,
  183. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED],
  184. ], [
  185. "sort" => "id",
  186. ]);
  187. }
  188. public function getCompletedTests() {
  189. return Test::query([
  190. "tester_id" => $this->id,
  191. "status" => Test::STATUS_COMPLETED,
  192. ], [
  193. "sort" => "id",
  194. ]);
  195. }
  196. public function getPreferredPaymentMethodText() {
  197. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  198. }
  199. }
  200. ?>