user.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_TESTER_MANAGER = 48;
  17. const LEVEL_ADMIN = 64;
  18. const LEVEL_SUPERADMIN = 128;
  19. const LEVELS = [
  20. 0 => "Banned",
  21. 1 => "Registered",
  22. 2 => "Unverified",
  23. 4 => "Deactivated",
  24. 8 => "Tester",
  25. 16 => "Venue",
  26. 32 => "Customer",
  27. 48 => "Tester Manager",
  28. 64 => "Admin",
  29. 128 => "Super Admin",
  30. ];
  31. const PAYMENT_METHOD_BANK_TRANSFER = 1;
  32. const PAYMENT_METHOD_PAYPAL = 2;
  33. const PAYMENT_METHODS = [
  34. 1 => "Bank Transfer",
  35. 2 => "PayPal",
  36. ];
  37. // Static methods
  38. public static function validatePassword($password) {
  39. $lowercase = preg_match("/[a-z]/", $password);
  40. $uppercase = preg_match("/[A-Z]/", $password);
  41. $number = preg_match("/[0-9]/", $password);
  42. $special = preg_match("/[^0-9a-zA-Z]/", $password);
  43. $length = strlen($password) >= 6;
  44. return ($lowercase && $uppercase && $number && $special && $length);
  45. }
  46. // Object methods
  47. public function hasValidPaymentMethod() {
  48. switch($this->preferred_payment_method) {
  49. case User::PAYMENT_METHOD_BANK_TRANSFER: {
  50. if(empty($this->bank_name)) return false;
  51. if(empty($this->bank_sort_code)) return false;
  52. if(empty($this->bank_account_number)) return false;
  53. break;
  54. }
  55. case User::PAYMENT_METHOD_PAYPAL: {
  56. if(empty($this->paypal_email)) return false;
  57. break;
  58. }
  59. }
  60. return true;
  61. }
  62. public function sendActivationEmail() {
  63. $this->verify_token = bin2hex(random_bytes(16));
  64. $this->commit();
  65. // Content
  66. $name = htmlspecialchars($this->first_name);
  67. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
  68. // Build
  69. $builder = new EmailBuilder;
  70. $builder->addParagraph("<b>Hi $name</b>");
  71. $builder->addLeadParagraph("Thanks for registering!");
  72. $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:");
  73. $builder->addParagraph("<a href=\"$link\">$link</a>");
  74. $content = $builder->build();
  75. // Send
  76. $mail = new Email;
  77. $mail->setHtmlContent($content);
  78. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  79. $mail->addAddress($this->email, $this->getDisplayName());
  80. $mail->setSubject("Verify your email address");
  81. $mail->send();
  82. return true;
  83. }
  84. public function sendWelcomeEmail() {
  85. // Content
  86. $name = htmlspecialchars($this->first_name);
  87. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
  88. // Build
  89. $builder = new EmailBuilder;
  90. $builder->addParagraph("<b>Hi $name</b>");
  91. $builder->addLeadParagraph("Thanks for verifying your email address!");
  92. $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:");
  93. $builder->addParagraph("<a href=\"$link\">$link</a>");
  94. $content = $builder->build();
  95. // Send
  96. $mail = new Email;
  97. $mail->setHtmlContent($content);
  98. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  99. $mail->addAddress($this->email, $this->getDisplayName());
  100. $mail->setSubject("Welcome to Check Policy");
  101. $mail->send();
  102. return true;
  103. }
  104. public function sendIDPassEmail() {
  105. // Content
  106. $name = htmlspecialchars($this->first_name);
  107. // Build
  108. $builder = new EmailBuilder;
  109. $builder->addParagraph("<b>Hi $name</b>");
  110. $builder->addLeadParagraph("Your application has been successful!");
  111. $builder->addParagraph("Welcome to Check Policy! Remember to check your online account for when tests become available.");
  112. $builder->addParagraph("Keep an eye out for emails inviting you to accept a test.");
  113. $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.");
  114. $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.");
  115. $builder->addParagraph("Once again welcome to Check Policy - Accept, Begin, Complete and get paid for up to 8 minutes work!");
  116. $content = $builder->build();
  117. // Send
  118. $mail = new Email;
  119. $mail->setHtmlContent($content);
  120. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  121. $mail->addAddress($this->email, $this->getDisplayName());
  122. $mail->setSubject("ID accepted and verified");
  123. $mail->send();
  124. return true;
  125. }
  126. public function sendIDFailEmail($reason = "No reason given.") {
  127. // Content
  128. $name = htmlspecialchars($this->first_name);
  129. $reason = htmlspecialchars($reason);
  130. // Build
  131. $builder = new EmailBuilder;
  132. $builder->addParagraph("<b>Hi $name</b>");
  133. $builder->addLeadParagraph("Thank you for submitting your ID details.");
  134. $builder->addParagraph("Unfortunately we could not verify and accept your documents and/or image.");
  135. $builder->addParagraph("The reason for this is: $reason");
  136. $builder->addParagraph("Please log in again, resubmit and we will try again.");
  137. $builder->addParagraph("We look forward to welcoming you on-board soon!");
  138. $content = $builder->build();
  139. // Send
  140. $mail = new Email;
  141. $mail->setHtmlContent($content);
  142. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  143. $mail->addAddress($this->email, $this->getDisplayName());
  144. $mail->setSubject("We could not verify your ID details");
  145. $mail->send();
  146. return true;
  147. }
  148. public function sendPasswordResetEmail() {
  149. $this->reset_token = bin2hex(random_bytes(16));
  150. $this->commit();
  151. // Content
  152. $name = htmlspecialchars($this->first_name);
  153. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  154. // Build
  155. $builder = new EmailBuilder;
  156. $builder->addParagraph("<b>Hi $name</b>");
  157. $builder->addLeadParagraph("A reset password request has been received.");
  158. $builder->addParagraph("To reset your password, please tap on the following link:");
  159. $builder->addParagraph("<a href=\"$link\">$link</a>");
  160. $builder->addParagraph("If you <strong>did not</strong> request for your password to be reset, please ignore this email.");
  161. $content = $builder->build();
  162. // Send
  163. $mail = new Email;
  164. $mail->setHtmlContent($content);
  165. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  166. $mail->addAddress($this->email, $this->getDisplayName());
  167. $mail->setSubject("Password reset instructions");
  168. $mail->send();
  169. return true;
  170. }
  171. public function canAdmin() {
  172. return $this->level >= User::LEVEL_ADMIN;
  173. }
  174. public function getReferrer() {
  175. return is_null($this->referrer_id) ? false : User::get($this->referrer_id);
  176. }
  177. public function getDirectory() {
  178. $directory = ROOT_DIR . "/files/users/" . $this->id;
  179. if(!file_exists($directory)) mkdir($directory, 0777, true);
  180. return $directory;
  181. }
  182. public function getLevelText() {
  183. return User::LEVELS[$this->level];
  184. }
  185. public function getLevelClass() {
  186. switch($this->level) {
  187. case User::LEVEL_BANNED: return "danger";
  188. case User::LEVEL_REGISTERED: return "secondary";
  189. case User::LEVEL_UNVERIFIED: return "warning";
  190. case User::LEVEL_DEACTIVATED: return "secondary";
  191. case User::LEVEL_TESTER: return "success";
  192. case User::LEVEL_VENUE: return "info";
  193. case User::LEVEL_CUSTOMER: return "info";
  194. case User::LEVEL_ADMIN: return "info";
  195. case User::LEVEL_SUPERADMIN: return "info";
  196. default:
  197. return "primary";
  198. }
  199. }
  200. public function getGenderText() {
  201. if($this->gender == "M") return "Male";
  202. if($this->gender == "F") return "Female";
  203. return "Unspecified";
  204. }
  205. public function getAge() {
  206. if(is_null($this->date_of_birth)) return "N/A";
  207. $now = new DateTime("now");
  208. $dob = new DateTime($this->date_of_birth);
  209. return $dob->diff($now)->y;
  210. }
  211. public function getDisplayName() {
  212. $parts = array_filter([$this->first_name, $this->last_name]);
  213. return implode(" ", $parts);
  214. }
  215. public function hasPermission($object) {
  216. if($this->level == User::LEVEL_SUPERADMIN) return true;
  217. if($this->level == User::LEVEL_ADMIN) return true;
  218. $customers = $this->getCustomers();
  219. $customer_ids = array_column($customers, "id");
  220. if(is_a($object, "Customer")) {
  221. return in_array($object->id, $customer_ids);
  222. }
  223. if(is_a($object, "Location")) {
  224. return in_array($object->customer_id, $customer_ids);
  225. }
  226. if(is_a($object, "Project")) {
  227. return in_array($object->customer_id, $customer_ids);
  228. }
  229. if(is_a($object, "Template")) {
  230. return in_array($object->customer_id, $customer_ids);
  231. }
  232. return false;
  233. }
  234. public function deleteIDFiles() {
  235. $files = UserFile::query([
  236. "user_id" => $this->id,
  237. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  238. ]);
  239. foreach($files as $file) {
  240. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  241. if(file_exists($path)) unlink($path);
  242. $file->delete();
  243. }
  244. return true;
  245. }
  246. public function getAddresses() {
  247. $addresses = UserAddress::query([
  248. "user_id" => $this->id,
  249. ], [
  250. "sort" => "name"
  251. ]);
  252. return $addresses;
  253. }
  254. public function getInvoiceAddress() {
  255. $addresses = UserAddress::query([
  256. "user_id" => $this->id,
  257. ]);
  258. foreach($addresses as $address) {
  259. if($address->invoice) return $address;
  260. }
  261. // If only one address in account, just return it
  262. if(count($addresses) == 1) return array_shift($addresses);
  263. return false;
  264. }
  265. public function getCustomers() {
  266. if($this->level >= User::LEVEL_ADMIN) {
  267. return Customer::query([], ["sort" => "name"]);
  268. }
  269. if($this->level == User::LEVEL_CUSTOMER) {
  270. $links = UserCustomer::query(["user_id" => $this->id]);
  271. return Customer::query([
  272. "id" => array_column($links, "customer_id")
  273. ], ["sort" => "name"]);
  274. }
  275. return [];
  276. }
  277. public function getPendingTests() {
  278. return Test::query([
  279. "tester_id" => $this->id,
  280. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_PENDING_VERIFICATION],
  281. ], [
  282. "sort" => "id",
  283. "order" => "desc"
  284. ]);
  285. }
  286. public function getCompletedTests() {
  287. return Test::query([
  288. "tester_id" => $this->id,
  289. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  290. ], [
  291. "sort" => "id",
  292. "order" => "desc"
  293. ]);
  294. }
  295. public function getReferrals() {
  296. return User::query([
  297. "referrer_id" => $this->id
  298. ]);
  299. }
  300. public function getPreferredPaymentMethodText() {
  301. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  302. }
  303. }
  304. ?>