user.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // Content
  49. $name = htmlspecialchars($this->first_name);
  50. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
  51. // Build
  52. $builder = new EmailBuilder;
  53. $builder->addParagraph("<b>Hi $name</b>");
  54. $builder->addLeadParagraph("Thanks for registering!");
  55. $builder->addParagraph("Welcome to Check Policy! Before you can log in, you will need to verify your email address. You can do so by tapping the following link:");
  56. $builder->addParagraph("<a href=\"$link\">$link</a>");
  57. $content = $builder->build();
  58. // Send
  59. $mail = new Email;
  60. $mail->setHtmlContent($content);
  61. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  62. $mail->addAddress($this->email, $this->getDisplayName());
  63. $mail->setSubject("Verify your email address");
  64. $mail->send();
  65. return true;
  66. }
  67. public function sendWelcomeEmail() {
  68. // Content
  69. $name = htmlspecialchars($this->first_name);
  70. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
  71. // Build
  72. $builder = new EmailBuilder;
  73. $builder->addParagraph("<b>Hi $name</b>");
  74. $builder->addLeadParagraph("Thanks for verifying your email address!");
  75. $builder->addParagraph("Welcome to Check Policy! Before you can take part in tests, you will need to provide us with some ID. You can do so by tapping the following link:");
  76. $builder->addParagraph("<a href=\"$link\">$link</a>");
  77. $content = $builder->build();
  78. // Send
  79. $mail = new Email;
  80. $mail->setHtmlContent($content);
  81. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  82. $mail->addAddress($this->email, $this->getDisplayName());
  83. $mail->setSubject("Welcome to Check Policy");
  84. $mail->send();
  85. return true;
  86. }
  87. public function sendPasswordResetEmail() {
  88. $this->reset_token = bin2hex(random_bytes(16));
  89. $this->commit();
  90. // Content
  91. $name = htmlspecialchars($this->first_name);
  92. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  93. // Build
  94. $builder = new EmailBuilder;
  95. $builder->addParagraph("<b>Hi $name</b>");
  96. $builder->addLeadParagraph("A reset password request has been received.");
  97. $builder->addParagraph("To reset your password, please tap on the following link:");
  98. $builder->addParagraph("<a href=\"$link\">$link</a>");
  99. $builder->addParagraph("If you <strong>did not</strong> request for your password to be reset, please ignore this email.");
  100. $content = $builder->build();
  101. // Send
  102. $mail = new Email;
  103. $mail->setHtmlContent($content);
  104. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  105. $mail->addAddress($this->email, $this->getDisplayName());
  106. $mail->setSubject("Password reset instructions");
  107. $mail->send();
  108. return true;
  109. }
  110. public function canAdmin() {
  111. return $this->level >= User::LEVEL_ADMIN;
  112. }
  113. public function getDirectory() {
  114. $directory = ROOT_DIR . "/files/users/" . $this->id;
  115. if(!file_exists($directory)) mkdir($directory, 0777, true);
  116. return $directory;
  117. }
  118. public function getLevelText() {
  119. return User::LEVELS[$this->level];
  120. }
  121. public function getLevelClass() {
  122. switch($this->level) {
  123. case User::LEVEL_BANNED: return "danger";
  124. case User::LEVEL_REGISTERED: return "secondary";
  125. case User::LEVEL_UNVERIFIED: return "warning";
  126. case User::LEVEL_DEACTIVATED: return "secondary";
  127. case User::LEVEL_TESTER: return "success";
  128. case User::LEVEL_VENUE: return "info";
  129. case User::LEVEL_CUSTOMER: return "info";
  130. case User::LEVEL_ADMIN: return "info";
  131. case User::LEVEL_SUPERADMIN: return "info";
  132. default:
  133. return "primary";
  134. }
  135. }
  136. public function getGenderText() {
  137. if($this->gender == "M") return "Male";
  138. if($this->gender == "F") return "Female";
  139. return "Unspecified";
  140. }
  141. public function getAge() {
  142. if(is_null($this->date_of_birth)) return "N/A";
  143. $now = new DateTime("now");
  144. $dob = new DateTime($this->date_of_birth);
  145. return $dob->diff($now)->y;
  146. }
  147. public function getDisplayName() {
  148. $parts = array_filter([$this->first_name, $this->last_name]);
  149. return implode(" ", $parts);
  150. }
  151. public function hasPermission($object) {
  152. if($this->level == User::LEVEL_SUPERADMIN) return true;
  153. if($this->level == User::LEVEL_ADMIN) return true;
  154. $customers = $this->getCustomers();
  155. $customer_ids = array_column($customers, "id");
  156. if(is_a($object, "Customer")) {
  157. return in_array($object->id, $customer_ids);
  158. }
  159. if(is_a($object, "Location")) {
  160. return in_array($object->customer_id, $customer_ids);
  161. }
  162. if(is_a($object, "Project")) {
  163. return in_array($object->customer_id, $customer_ids);
  164. }
  165. if(is_a($object, "Template")) {
  166. return in_array($object->customer_id, $customer_ids);
  167. }
  168. return false;
  169. }
  170. public function deleteIDFiles() {
  171. $files = UserFile::query([
  172. "user_id" => $this->id,
  173. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  174. ]);
  175. foreach($files as $file) {
  176. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  177. if(file_exists($path)) unlink($path);
  178. $file->delete();
  179. }
  180. return true;
  181. }
  182. public function getAddresses() {
  183. $addresses = UserAddress::query([
  184. "user_id" => $this->id,
  185. ], [
  186. "sort" => "name"
  187. ]);
  188. return $addresses;
  189. }
  190. public function getCustomers() {
  191. if($this->level >= User::LEVEL_ADMIN) {
  192. return Customer::query([], ["sort" => "name"]);
  193. }
  194. if($this->level == User::LEVEL_CUSTOMER) {
  195. $links = UserCustomer::query(["user_id" => $this->id]);
  196. return Customer::query([
  197. "id" => array_column($links, "customer_id")
  198. ], ["sort" => "name"]);
  199. }
  200. return [];
  201. }
  202. public function getPendingTests() {
  203. return Test::query([
  204. "tester_id" => $this->id,
  205. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_PENDING_VERIFICATION],
  206. ], [
  207. "sort" => "id",
  208. "order" => "desc"
  209. ]);
  210. }
  211. public function getCompletedTests() {
  212. return Test::query([
  213. "tester_id" => $this->id,
  214. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  215. ], [
  216. "sort" => "id",
  217. "order" => "desc"
  218. ]);
  219. }
  220. public function getPreferredPaymentMethodText() {
  221. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  222. }
  223. }
  224. ?>