user.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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", "terms_accepted", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "dts_deleted", "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! In order to get you a test as soon as possible, we need one final confirmation. Please log in to your account and have a quick read of the documents that explain what we need you to do during a test.");
  112. $builder->addParagraph("When you're ready to try a Practice test, in your account and select the \"Practice\" option.");
  113. $builder->addParagraph("1. As it is a Practice test, you can reset and start over as many times as you like because it doesn't count but lets you see what the real thing is like.");
  114. $builder->addParagraph("2. Email us at contact@check-policy.com and let us know that you're ready.");
  115. $builder->addParagraph("3. We will allocate a test to you as soon as possible.");
  116. $builder->addParagraph("Remember to check your online account for when tests become available.");
  117. $builder->addParagraph("Keep an eye out for emails inviting you to accept a test via the email or log in to your account where you can see your available tests.");
  118. $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 within the agreed period.");
  119. $builder->addParagraph("If you can't make it (it's too far or you'll be away), please Reject the test or email us with other suitable dates.");
  120. $builder->addParagraph("After you click Accept, you will be given the option to Begin the test whenever you want. Just log in to your account and click Begin on the test.");
  121. $builder->addParagraph("Once again welcome to Check Policy - Accept, Begin, Complete and get paid for up to 8 minutes work!");
  122. $content = $builder->build();
  123. // Send
  124. $mail = new Email;
  125. $mail->setHtmlContent($content);
  126. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  127. $mail->addAddress($this->email, $this->getDisplayName());
  128. $mail->setSubject("ID accepted and verified");
  129. $mail->send();
  130. return true;
  131. }
  132. public function sendIDFailEmail($reason = "No reason given.") {
  133. // Content
  134. $name = htmlspecialchars($this->first_name);
  135. $reason = htmlspecialchars($reason);
  136. // Build
  137. $builder = new EmailBuilder;
  138. $builder->addParagraph("<b>Hi $name</b>");
  139. $builder->addLeadParagraph("Thank you for submitting your ID details.");
  140. $builder->addParagraph("Unfortunately we could not verify and accept your documents and/or image.");
  141. $builder->addParagraph("The reason for this is: $reason");
  142. $builder->addParagraph("Please log in again, resubmit and we will try again.");
  143. $builder->addParagraph("We look forward to welcoming you on-board soon!");
  144. $content = $builder->build();
  145. // Send
  146. $mail = new Email;
  147. $mail->setHtmlContent($content);
  148. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  149. $mail->addAddress($this->email, $this->getDisplayName());
  150. $mail->setSubject("We could not verify your ID details");
  151. $mail->send();
  152. return true;
  153. }
  154. public function sendPasswordResetEmail() {
  155. $this->reset_token = bin2hex(random_bytes(16));
  156. $this->commit();
  157. // Content
  158. $name = htmlspecialchars($this->first_name);
  159. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  160. // Build
  161. $builder = new EmailBuilder;
  162. $builder->addParagraph("<b>Hi $name</b>");
  163. $builder->addLeadParagraph("A reset password request has been received.");
  164. $builder->addParagraph("To reset your password, please tap on the following link:");
  165. $builder->addParagraph("<a href=\"$link\">$link</a>");
  166. $builder->addParagraph("If you <strong>did not</strong> request for your password to be reset, please ignore this email.");
  167. $content = $builder->build();
  168. // Send
  169. $mail = new Email;
  170. $mail->setHtmlContent($content);
  171. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  172. $mail->addAddress($this->email, $this->getDisplayName());
  173. $mail->setSubject("Password reset instructions");
  174. $mail->send();
  175. return true;
  176. }
  177. public function canAdmin() {
  178. return $this->level >= User::LEVEL_ADMIN;
  179. }
  180. public function getReferrer() {
  181. return is_null($this->referrer_id) ? false : User::get($this->referrer_id);
  182. }
  183. public function getDirectory() {
  184. $directory = ROOT_DIR . "/files/users/" . $this->id;
  185. if(!file_exists($directory)) mkdir($directory, 0777, true);
  186. return $directory;
  187. }
  188. public function getLevelText() {
  189. return User::LEVELS[$this->level];
  190. }
  191. public function getLevelClass() {
  192. switch($this->level) {
  193. case User::LEVEL_BANNED: return "danger";
  194. case User::LEVEL_REGISTERED: return "secondary";
  195. case User::LEVEL_UNVERIFIED: return "warning";
  196. case User::LEVEL_DEACTIVATED: return "secondary";
  197. case User::LEVEL_TESTER: return "success";
  198. case User::LEVEL_VENUE: return "info";
  199. case User::LEVEL_CUSTOMER: return "info";
  200. case User::LEVEL_ADMIN: return "info";
  201. case User::LEVEL_SUPERADMIN: return "info";
  202. default:
  203. return "primary";
  204. }
  205. }
  206. public function getGenderText() {
  207. if($this->gender == "M") return "Male";
  208. if($this->gender == "F") return "Female";
  209. return "Unspecified";
  210. }
  211. public function getAge() {
  212. if(is_null($this->date_of_birth)) return "N/A";
  213. $now = new DateTime("now");
  214. $dob = new DateTime($this->date_of_birth);
  215. return $dob->diff($now)->y;
  216. }
  217. public function getDisplayName() {
  218. $parts = array_filter([$this->first_name, $this->last_name]);
  219. return implode(" ", $parts);
  220. }
  221. public function hasPermission($object) {
  222. if($this->level == User::LEVEL_SUPERADMIN) return true;
  223. if($this->level == User::LEVEL_ADMIN) return true;
  224. $customers = $this->getCustomers();
  225. $customer_ids = array_column($customers, "id");
  226. if(is_a($object, "Customer")) {
  227. return in_array($object->id, $customer_ids);
  228. }
  229. if(is_a($object, "Location")) {
  230. return in_array($object->customer_id, $customer_ids);
  231. }
  232. if(is_a($object, "Project")) {
  233. return in_array($object->customer_id, $customer_ids);
  234. }
  235. if(is_a($object, "Template")) {
  236. return in_array($object->customer_id, $customer_ids);
  237. }
  238. return false;
  239. }
  240. public function deactivate() {
  241. // Decline all pending tests
  242. $pending_tests = $this->getPendingTests();
  243. foreach($pending_tests as $test) {
  244. $test->decline();
  245. }
  246. // Set user level to Deactivated
  247. $this->level = User::LEVEL_DEACTIVATED;
  248. return $this->commit();
  249. }
  250. public function softDelete() {
  251. // Delete identification documents
  252. $this->deleteIDFiles();
  253. // Delete addresses
  254. $this->deleteAddresses();
  255. // Decline all pending tests
  256. $pending_tests = $this->getPendingTests();
  257. foreach($pending_tests as $test) {
  258. $test->decline();
  259. }
  260. // Delete personal information
  261. $this->email = null;
  262. $this->title = null;
  263. $this->first_name = null;
  264. $this->last_name = null;
  265. $this->phone = null;
  266. $this->bank_name = null;
  267. $this->bank_sort_code = null;
  268. $this->bank_account_number = null;
  269. $this->paypal_email = null;
  270. // Set deleted time
  271. $this->dts_deleted = Util::getDateTime();
  272. $success = $this->commit();
  273. return true;
  274. }
  275. public function deleteIDFiles() {
  276. $files = UserFile::query([
  277. "user_id" => $this->id,
  278. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  279. ]);
  280. foreach($files as $file) {
  281. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  282. if(file_exists($path)) unlink($path);
  283. $file->delete();
  284. }
  285. return true;
  286. }
  287. public function deleteAddresses() {
  288. $addresses = $this->getAddresses();
  289. foreach($addresses as $address) {
  290. $address->delete();
  291. }
  292. return true;
  293. }
  294. public function getAddresses() {
  295. $addresses = UserAddress::query([
  296. "user_id" => $this->id,
  297. ], [
  298. "sort" => "name"
  299. ]);
  300. return $addresses;
  301. }
  302. public function getInvoiceAddress() {
  303. $addresses = UserAddress::query([
  304. "user_id" => $this->id,
  305. ]);
  306. foreach($addresses as $address) {
  307. if($address->invoice) return $address;
  308. }
  309. // If only one address in account, just return it
  310. if(count($addresses) == 1) return array_shift($addresses);
  311. return false;
  312. }
  313. public function getCustomers() {
  314. if($this->level >= User::LEVEL_ADMIN) {
  315. return Customer::query([], ["sort" => "name"]);
  316. }
  317. if($this->level == User::LEVEL_CUSTOMER) {
  318. $links = UserCustomer::query(["user_id" => $this->id]);
  319. return Customer::query([
  320. "id" => array_column($links, "customer_id")
  321. ], ["sort" => "name"]);
  322. }
  323. return [];
  324. }
  325. public function getPendingTests() {
  326. return Test::query([
  327. "tester_id" => $this->id,
  328. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
  329. ], [
  330. "sort" => "id",
  331. "order" => "desc"
  332. ]);
  333. }
  334. public function getCompletedTests() {
  335. return Test::query([
  336. "tester_id" => $this->id,
  337. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  338. ], [
  339. "sort" => "id",
  340. "order" => "desc"
  341. ]);
  342. }
  343. public function getReferrals() {
  344. return User::query([
  345. "referrer_id" => $this->id
  346. ]);
  347. }
  348. public function getPreferredPaymentMethodText() {
  349. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  350. }
  351. }
  352. ?>