user.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // Constants
  7. const TITLES = ["Miss", "Mr", "Ms", "Dr"];
  8. const GENDERS = ["Female", "Male", "Unspecified"];
  9. const LEVEL_BANNED = 0;
  10. const LEVEL_REGISTERED = 1;
  11. const LEVEL_UNVERIFIED = 2;
  12. const LEVEL_DEACTIVATED = 4;
  13. const LEVEL_TESTER = 8;
  14. const LEVEL_VENUE = 16;
  15. const LEVEL_CUSTOMER = 32;
  16. const LEVEL_ADMIN = 64;
  17. const LEVEL_SUPERADMIN = 128;
  18. const LEVELS = [
  19. 0 => "Banned",
  20. 1 => "Registered",
  21. 2 => "Unverified",
  22. 4 => "Deactivated",
  23. 8 => "Tester",
  24. 16 => "Venue",
  25. 32 => "Customer",
  26. 64 => "Admin",
  27. 128 => "Super Admin",
  28. ];
  29. const PAYMENT_METHOD_BANK_TRANSFER = 1;
  30. const PAYMENT_METHOD_PAYPAL = 2;
  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 hasValidPaymentMethod() {
  46. switch($this->preferred_payment_method) {
  47. case User::PAYMENT_METHOD_BANK_TRANSFER: {
  48. if(empty($this->bank_name)) return false;
  49. if(empty($this->bank_sort_code)) return false;
  50. if(empty($this->bank_account_number)) return false;
  51. break;
  52. }
  53. case User::PAYMENT_METHOD_PAYPAL: {
  54. if(empty($this->paypal_email)) return false;
  55. break;
  56. }
  57. }
  58. return true;
  59. }
  60. public function sendActivationEmail() {
  61. $this->verify_token = bin2hex(random_bytes(16));
  62. $this->commit();
  63. // Content
  64. $name = htmlspecialchars($this->first_name);
  65. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
  66. // Build
  67. $builder = new EmailBuilder;
  68. $builder->addParagraph("<b>Hi $name</b>");
  69. $builder->addLeadParagraph("Thanks for registering!");
  70. $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:");
  71. $builder->addParagraph("<a href=\"$link\">$link</a>");
  72. $content = $builder->build();
  73. // Send
  74. $mail = new Email;
  75. $mail->setHtmlContent($content);
  76. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  77. $mail->addAddress($this->email, $this->getDisplayName());
  78. $mail->setSubject("Verify your email address");
  79. $mail->send();
  80. return true;
  81. }
  82. public function sendWelcomeEmail() {
  83. // Content
  84. $name = htmlspecialchars($this->first_name);
  85. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
  86. // Build
  87. $builder = new EmailBuilder;
  88. $builder->addParagraph("<b>Hi $name</b>");
  89. $builder->addLeadParagraph("Thanks for verifying your email address!");
  90. $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:");
  91. $builder->addParagraph("<a href=\"$link\">$link</a>");
  92. $content = $builder->build();
  93. // Send
  94. $mail = new Email;
  95. $mail->setHtmlContent($content);
  96. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  97. $mail->addAddress($this->email, $this->getDisplayName());
  98. $mail->setSubject("Welcome to Check Policy");
  99. $mail->send();
  100. return true;
  101. }
  102. public function sendPasswordResetEmail() {
  103. $this->reset_token = bin2hex(random_bytes(16));
  104. $this->commit();
  105. // Content
  106. $name = htmlspecialchars($this->first_name);
  107. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  108. // Build
  109. $builder = new EmailBuilder;
  110. $builder->addParagraph("<b>Hi $name</b>");
  111. $builder->addLeadParagraph("A reset password request has been received.");
  112. $builder->addParagraph("To reset your password, please tap on the following link:");
  113. $builder->addParagraph("<a href=\"$link\">$link</a>");
  114. $builder->addParagraph("If you <strong>did not</strong> request for your password to be reset, please ignore this email.");
  115. $content = $builder->build();
  116. // Send
  117. $mail = new Email;
  118. $mail->setHtmlContent($content);
  119. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  120. $mail->addAddress($this->email, $this->getDisplayName());
  121. $mail->setSubject("Password reset instructions");
  122. $mail->send();
  123. return true;
  124. }
  125. public function canAdmin() {
  126. return $this->level >= User::LEVEL_ADMIN;
  127. }
  128. public function getDirectory() {
  129. $directory = ROOT_DIR . "/files/users/" . $this->id;
  130. if(!file_exists($directory)) mkdir($directory, 0777, true);
  131. return $directory;
  132. }
  133. public function getLevelText() {
  134. return User::LEVELS[$this->level];
  135. }
  136. public function getLevelClass() {
  137. switch($this->level) {
  138. case User::LEVEL_BANNED: return "danger";
  139. case User::LEVEL_REGISTERED: return "secondary";
  140. case User::LEVEL_UNVERIFIED: return "warning";
  141. case User::LEVEL_DEACTIVATED: return "secondary";
  142. case User::LEVEL_TESTER: return "success";
  143. case User::LEVEL_VENUE: return "info";
  144. case User::LEVEL_CUSTOMER: return "info";
  145. case User::LEVEL_ADMIN: return "info";
  146. case User::LEVEL_SUPERADMIN: return "info";
  147. default:
  148. return "primary";
  149. }
  150. }
  151. public function getGenderText() {
  152. if($this->gender == "M") return "Male";
  153. if($this->gender == "F") return "Female";
  154. return "Unspecified";
  155. }
  156. public function getAge() {
  157. if(is_null($this->date_of_birth)) return "N/A";
  158. $now = new DateTime("now");
  159. $dob = new DateTime($this->date_of_birth);
  160. return $dob->diff($now)->y;
  161. }
  162. public function getDisplayName() {
  163. $parts = array_filter([$this->first_name, $this->last_name]);
  164. return implode(" ", $parts);
  165. }
  166. public function hasPermission($object) {
  167. if($this->level == User::LEVEL_SUPERADMIN) return true;
  168. if($this->level == User::LEVEL_ADMIN) return true;
  169. $customers = $this->getCustomers();
  170. $customer_ids = array_column($customers, "id");
  171. if(is_a($object, "Customer")) {
  172. return in_array($object->id, $customer_ids);
  173. }
  174. if(is_a($object, "Location")) {
  175. return in_array($object->customer_id, $customer_ids);
  176. }
  177. if(is_a($object, "Project")) {
  178. return in_array($object->customer_id, $customer_ids);
  179. }
  180. if(is_a($object, "Template")) {
  181. return in_array($object->customer_id, $customer_ids);
  182. }
  183. return false;
  184. }
  185. public function deleteIDFiles() {
  186. $files = UserFile::query([
  187. "user_id" => $this->id,
  188. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  189. ]);
  190. foreach($files as $file) {
  191. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  192. if(file_exists($path)) unlink($path);
  193. $file->delete();
  194. }
  195. return true;
  196. }
  197. public function getAddresses() {
  198. $addresses = UserAddress::query([
  199. "user_id" => $this->id,
  200. ], [
  201. "sort" => "name"
  202. ]);
  203. return $addresses;
  204. }
  205. public function getInvoiceAddress() {
  206. return UserAddress::queryOne([
  207. "user_id" => $this->id,
  208. "invoice" => 1
  209. ]);
  210. }
  211. public function getCustomers() {
  212. if($this->level >= User::LEVEL_ADMIN) {
  213. return Customer::query([], ["sort" => "name"]);
  214. }
  215. if($this->level == User::LEVEL_CUSTOMER) {
  216. $links = UserCustomer::query(["user_id" => $this->id]);
  217. return Customer::query([
  218. "id" => array_column($links, "customer_id")
  219. ], ["sort" => "name"]);
  220. }
  221. return [];
  222. }
  223. public function getPendingTests() {
  224. return Test::query([
  225. "tester_id" => $this->id,
  226. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_PENDING_VERIFICATION],
  227. ], [
  228. "sort" => "id",
  229. "order" => "desc"
  230. ]);
  231. }
  232. public function getCompletedTests() {
  233. return Test::query([
  234. "tester_id" => $this->id,
  235. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  236. ], [
  237. "sort" => "id",
  238. "order" => "desc"
  239. ]);
  240. }
  241. public function getPreferredPaymentMethodText() {
  242. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  243. }
  244. }
  245. ?>