user.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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", "referrer_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 sendIDPassEmail() {
  103. // Content
  104. $name = htmlspecialchars($this->first_name);
  105. // Build
  106. $builder = new EmailBuilder;
  107. $builder->addParagraph("<b>Hi $name</b>");
  108. $builder->addLeadParagraph("Your application has been successful!");
  109. $builder->addParagraph("Welcome to Check Policy! Remember to check your online account for when tests become available.");
  110. $builder->addParagraph("Keep an eye out for emails inviting you to accept a test.");
  111. $builder->addParagraph("If you accept to do a test within the agreed period (usually 7 days) please click Accept to confirm that you would like to do the test.");
  112. $builder->addParagraph("After you click Accept, you will be given the option to Begin the test whenever you want. Just load up your account and click Begin.");
  113. $builder->addParagraph("Once again welcome to Check Policy - Accept, Begin, Complete and get paid for up to 8 minutes work!");
  114. $content = $builder->build();
  115. // Send
  116. $mail = new Email;
  117. $mail->setHtmlContent($content);
  118. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  119. $mail->addAddress($this->email, $this->getDisplayName());
  120. $mail->setSubject("ID accepted and verified");
  121. $mail->send();
  122. return true;
  123. }
  124. public function sendIDFailEmail($reason = "No reason given.") {
  125. // Content
  126. $name = htmlspecialchars($this->first_name);
  127. $reason = htmlspecialchars($reason);
  128. // Build
  129. $builder = new EmailBuilder;
  130. $builder->addParagraph("<b>Hi $name</b>");
  131. $builder->addLeadParagraph("Thank you for submitting your ID details.");
  132. $builder->addParagraph("Unfortunately we could not verify and accept your documents and/or image.");
  133. $builder->addParagraph("The reason for this is: $reason");
  134. $builder->addParagraph("Please log in again, resubmit and we will try again.");
  135. $builder->addParagraph("We look forward to welcoming you on-board soon!");
  136. $content = $builder->build();
  137. // Send
  138. $mail = new Email;
  139. $mail->setHtmlContent($content);
  140. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  141. $mail->addAddress($this->email, $this->getDisplayName());
  142. $mail->setSubject("We could not verify your ID details");
  143. $mail->send();
  144. return true;
  145. }
  146. public function sendPasswordResetEmail() {
  147. $this->reset_token = bin2hex(random_bytes(16));
  148. $this->commit();
  149. // Content
  150. $name = htmlspecialchars($this->first_name);
  151. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  152. // Build
  153. $builder = new EmailBuilder;
  154. $builder->addParagraph("<b>Hi $name</b>");
  155. $builder->addLeadParagraph("A reset password request has been received.");
  156. $builder->addParagraph("To reset your password, please tap on the following link:");
  157. $builder->addParagraph("<a href=\"$link\">$link</a>");
  158. $builder->addParagraph("If you <strong>did not</strong> request for your password to be reset, please ignore this email.");
  159. $content = $builder->build();
  160. // Send
  161. $mail = new Email;
  162. $mail->setHtmlContent($content);
  163. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  164. $mail->addAddress($this->email, $this->getDisplayName());
  165. $mail->setSubject("Password reset instructions");
  166. $mail->send();
  167. return true;
  168. }
  169. public function canAdmin() {
  170. return $this->level >= User::LEVEL_ADMIN;
  171. }
  172. public function getReferrer() {
  173. return is_null($this->referrer_id) ? false : User::get($this->referrer_id);
  174. }
  175. public function getDirectory() {
  176. $directory = ROOT_DIR . "/files/users/" . $this->id;
  177. if(!file_exists($directory)) mkdir($directory, 0777, true);
  178. return $directory;
  179. }
  180. public function getLevelText() {
  181. return User::LEVELS[$this->level];
  182. }
  183. public function getLevelClass() {
  184. switch($this->level) {
  185. case User::LEVEL_BANNED: return "danger";
  186. case User::LEVEL_REGISTERED: return "secondary";
  187. case User::LEVEL_UNVERIFIED: return "warning";
  188. case User::LEVEL_DEACTIVATED: return "secondary";
  189. case User::LEVEL_TESTER: return "success";
  190. case User::LEVEL_VENUE: return "info";
  191. case User::LEVEL_CUSTOMER: return "info";
  192. case User::LEVEL_ADMIN: return "info";
  193. case User::LEVEL_SUPERADMIN: return "info";
  194. default:
  195. return "primary";
  196. }
  197. }
  198. public function getGenderText() {
  199. if($this->gender == "M") return "Male";
  200. if($this->gender == "F") return "Female";
  201. return "Unspecified";
  202. }
  203. public function getAge() {
  204. if(is_null($this->date_of_birth)) return "N/A";
  205. $now = new DateTime("now");
  206. $dob = new DateTime($this->date_of_birth);
  207. return $dob->diff($now)->y;
  208. }
  209. public function getDisplayName() {
  210. $parts = array_filter([$this->first_name, $this->last_name]);
  211. return implode(" ", $parts);
  212. }
  213. public function hasPermission($object) {
  214. if($this->level == User::LEVEL_SUPERADMIN) return true;
  215. if($this->level == User::LEVEL_ADMIN) return true;
  216. $customers = $this->getCustomers();
  217. $customer_ids = array_column($customers, "id");
  218. if(is_a($object, "Customer")) {
  219. return in_array($object->id, $customer_ids);
  220. }
  221. if(is_a($object, "Location")) {
  222. return in_array($object->customer_id, $customer_ids);
  223. }
  224. if(is_a($object, "Project")) {
  225. return in_array($object->customer_id, $customer_ids);
  226. }
  227. if(is_a($object, "Template")) {
  228. return in_array($object->customer_id, $customer_ids);
  229. }
  230. return false;
  231. }
  232. public function deleteIDFiles() {
  233. $files = UserFile::query([
  234. "user_id" => $this->id,
  235. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  236. ]);
  237. foreach($files as $file) {
  238. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  239. if(file_exists($path)) unlink($path);
  240. $file->delete();
  241. }
  242. return true;
  243. }
  244. public function getAddresses() {
  245. $addresses = UserAddress::query([
  246. "user_id" => $this->id,
  247. ], [
  248. "sort" => "name"
  249. ]);
  250. return $addresses;
  251. }
  252. public function getInvoiceAddress() {
  253. $addresses = UserAddress::query([
  254. "user_id" => $this->id,
  255. ]);
  256. foreach($addresses as $address) {
  257. if($address->invoice) return $address;
  258. }
  259. // If only one address in account, just return it
  260. if(count($addresses) == 1) return array_shift($addresses);
  261. return false;
  262. }
  263. public function getCustomers() {
  264. if($this->level >= User::LEVEL_ADMIN) {
  265. return Customer::query([], ["sort" => "name"]);
  266. }
  267. if($this->level == User::LEVEL_CUSTOMER) {
  268. $links = UserCustomer::query(["user_id" => $this->id]);
  269. return Customer::query([
  270. "id" => array_column($links, "customer_id")
  271. ], ["sort" => "name"]);
  272. }
  273. return [];
  274. }
  275. public function getPendingTests() {
  276. return Test::query([
  277. "tester_id" => $this->id,
  278. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_PENDING_VERIFICATION],
  279. ], [
  280. "sort" => "id",
  281. "order" => "desc"
  282. ]);
  283. }
  284. public function getCompletedTests() {
  285. return Test::query([
  286. "tester_id" => $this->id,
  287. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  288. ], [
  289. "sort" => "id",
  290. "order" => "desc"
  291. ]);
  292. }
  293. public function getReferrals() {
  294. return User::query([
  295. "referrer_id" => $this->id
  296. ]);
  297. }
  298. public function getPreferredPaymentMethodText() {
  299. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  300. }
  301. }
  302. ?>