user.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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", "reported", "reported_comment", "can_claim", "phone", "verify_token", "reset_token", "terms_accepted", "newsletter_consent", "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 isAdmin() {
  207. return ($this->level == User::LEVEL_ADMIN || $this->level == User::LEVEL_SUPERADMIN);
  208. }
  209. public function isCustomer() {
  210. return $this->level == User::LEVEL_CUSTOMER;
  211. }
  212. public function isTester() {
  213. return $this->level == User::LEVEL_TESTER;
  214. }
  215. public function isDeactivated() {
  216. return $this->level == User::LEVEL_DEACTIVATED;
  217. }
  218. public function getGenderText() {
  219. if($this->gender == "M") return "Male";
  220. if($this->gender == "F") return "Female";
  221. return "Unspecified";
  222. }
  223. public function getAge() {
  224. if(is_null($this->date_of_birth)) return "N/A";
  225. $now = new DateTime("now");
  226. $dob = new DateTime($this->date_of_birth);
  227. return $dob->diff($now)->y;
  228. }
  229. public function getDisplayName() {
  230. if(empty($this->email)) return "Deleted User";
  231. $parts = array_filter([$this->first_name, $this->last_name]);
  232. return implode(" ", $parts);
  233. }
  234. public function hasPermission($object) {
  235. if($this->level == User::LEVEL_SUPERADMIN) return true;
  236. if($this->level == User::LEVEL_ADMIN) return true;
  237. $customers = $this->getCustomers();
  238. $customer_ids = array_column($customers, "id");
  239. if(is_a($object, "Customer")) {
  240. return in_array($object->id, $customer_ids);
  241. }
  242. if(is_a($object, "Location")) {
  243. return in_array($object->customer_id, $customer_ids);
  244. }
  245. if(is_a($object, "Project")) {
  246. return in_array($object->customer_id, $customer_ids);
  247. }
  248. if(is_a($object, "Template")) {
  249. return in_array($object->customer_id, $customer_ids);
  250. }
  251. return false;
  252. }
  253. public function deactivate() {
  254. // Decline all pending tests
  255. $pending_tests = $this->getPendingTests();
  256. foreach($pending_tests as $test) {
  257. $test->decline();
  258. }
  259. // Set user level to Deactivated
  260. $this->level = User::LEVEL_DEACTIVATED;
  261. return $this->commit();
  262. }
  263. public function softDelete() {
  264. // Delete identification documents
  265. $this->deleteIDFiles();
  266. // Delete addresses
  267. $this->deleteAddresses();
  268. // Decline all pending tests
  269. $pending_tests = $this->getPendingTests();
  270. foreach($pending_tests as $test) {
  271. $test->decline();
  272. }
  273. // Delete personal information
  274. $this->email = null;
  275. $this->title = null;
  276. $this->first_name = null;
  277. $this->last_name = null;
  278. $this->phone = null;
  279. $this->bank_name = null;
  280. $this->bank_sort_code = null;
  281. $this->bank_account_number = null;
  282. $this->paypal_email = null;
  283. // Set deleted time
  284. $this->dts_deleted = Util::getDateTime();
  285. $success = $this->commit();
  286. return true;
  287. }
  288. public function deleteIDFiles() {
  289. $files = UserFile::query([
  290. "user_id" => $this->id,
  291. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  292. ]);
  293. foreach($files as $file) {
  294. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  295. if(file_exists($path)) unlink($path);
  296. $file->delete();
  297. }
  298. return true;
  299. }
  300. public function deleteAddresses() {
  301. $addresses = $this->getAddresses();
  302. foreach($addresses as $address) {
  303. $address->delete();
  304. }
  305. return true;
  306. }
  307. public function getAddresses() {
  308. $addresses = UserAddress::query([
  309. "user_id" => $this->id,
  310. ], [
  311. "sort" => "name"
  312. ]);
  313. return $addresses;
  314. }
  315. public function getInvoiceAddress() {
  316. $addresses = UserAddress::query([
  317. "user_id" => $this->id,
  318. ]);
  319. foreach($addresses as $address) {
  320. if($address->invoice) return $address;
  321. }
  322. // If only one address in account, just return it
  323. if(count($addresses) == 1) return array_shift($addresses);
  324. return false;
  325. }
  326. public function getCustomers() {
  327. if($this->level >= User::LEVEL_ADMIN) {
  328. return Customer::query([], ["sort" => "name"]);
  329. }
  330. if($this->level == User::LEVEL_CUSTOMER) {
  331. $links = UserCustomer::query(["user_id" => $this->id]);
  332. return Customer::query([
  333. "id" => array_column($links, "customer_id")
  334. ], ["sort" => "name"]);
  335. }
  336. return [];
  337. }
  338. public function getPendingTests() {
  339. return Test::query([
  340. "tester_id" => $this->id,
  341. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
  342. ], [
  343. "sort" => "id",
  344. "order" => "desc"
  345. ]);
  346. }
  347. public function getCompletedTests() {
  348. return Test::query([
  349. "tester_id" => $this->id,
  350. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  351. ], [
  352. "sort" => "id",
  353. "order" => "desc"
  354. ]);
  355. }
  356. public function getReferrals() {
  357. return User::query([
  358. "referrer_id" => $this->id
  359. ]);
  360. }
  361. public function getPreferredPaymentMethodText() {
  362. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  363. }
  364. }
  365. ?>