user.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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", "locale", "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 LEVEL_BANNED = 0;
  8. const LEVEL_REGISTERED = 1;
  9. const LEVEL_UNVERIFIED = 2;
  10. const LEVEL_DEACTIVATED = 4;
  11. const LEVEL_TESTER = 8;
  12. const LEVEL_VENUE = 16;
  13. const LEVEL_CUSTOMER = 32;
  14. const LEVEL_TESTER_MANAGER = 48;
  15. const LEVEL_ADMIN = 64;
  16. const LEVEL_SUPERADMIN = 128;
  17. const LEVELS = [
  18. 0 => "Banned",
  19. 1 => "Registered",
  20. 2 => "Unverified",
  21. 4 => "Deactivated",
  22. 8 => "Tester",
  23. 16 => "Venue",
  24. 32 => "Customer",
  25. 48 => "Tester Manager",
  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 titles() {
  37. return [
  38. "Miss" => MISS,
  39. "Mr" => MR,
  40. "Ms" => MS,
  41. "Dr" => DR
  42. ];
  43. }
  44. public static function genders() {
  45. return [
  46. "Female" => FEMALE,
  47. "Male" => MALE,
  48. "Unspecified" => UNSPECIFIED
  49. ];
  50. }
  51. public static function validatePassword($password) {
  52. $lowercase = preg_match("/[a-z]/", $password);
  53. $uppercase = preg_match("/[A-Z]/", $password);
  54. $number = preg_match("/[0-9]/", $password);
  55. $special = preg_match("/[^0-9a-zA-Z]/", $password);
  56. $length = strlen($password) >= 6;
  57. return ($lowercase && $uppercase && $number && $special && $length);
  58. }
  59. // Object methods
  60. public function hasValidPaymentMethod() {
  61. switch($this->preferred_payment_method) {
  62. case User::PAYMENT_METHOD_BANK_TRANSFER: {
  63. if(empty($this->bank_name)) return false;
  64. if(empty($this->bank_sort_code)) return false;
  65. if(empty($this->bank_account_number)) return false;
  66. break;
  67. }
  68. case User::PAYMENT_METHOD_PAYPAL: {
  69. if(empty($this->paypal_email)) return false;
  70. break;
  71. }
  72. }
  73. return true;
  74. }
  75. public function sendActivationEmail() {
  76. $this->verify_token = bin2hex(random_bytes(16));
  77. $this->commit();
  78. // Content
  79. $name = htmlspecialchars($this->first_name);
  80. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
  81. // Build
  82. $builder = new EmailBuilder;
  83. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  84. $builder->addLeadParagraph(__("Thanks for registering!"));
  85. $builder->addParagraph(sprintf(__("Welcome to %s! Before you can log in, you will need to verify your email address. You can do so by tapping the following link:"), APP_NAME));
  86. $builder->addParagraph("<a href=\"$link\">$link</a>");
  87. $content = $builder->build();
  88. // Send
  89. $mail = new Email;
  90. $mail->setHtmlContent($content);
  91. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  92. $mail->addAddress($this->email, $this->getDisplayName());
  93. $mail->setSubject(__("Verify your email address"));
  94. $mail->send();
  95. return true;
  96. }
  97. public function sendWelcomeEmail() {
  98. // Content
  99. $name = htmlspecialchars($this->first_name);
  100. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
  101. // Build
  102. $builder = new EmailBuilder;
  103. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  104. $builder->addLeadParagraph(__("Thanks for verifying your email address!"));
  105. $builder->addParagraph(sprintf(__("Welcome to %s! 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:"), APP_NAME));
  106. $builder->addParagraph("<a href=\"$link\">$link</a>");
  107. $content = $builder->build();
  108. // Send
  109. $mail = new Email;
  110. $mail->setHtmlContent($content);
  111. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  112. $mail->addAddress($this->email, $this->getDisplayName());
  113. $mail->setSubject(sprintf(__("Welcome to %s"), APP_NAME));
  114. $mail->send();
  115. return true;
  116. }
  117. public function sendIDPassEmail() {
  118. // Content
  119. $name = htmlspecialchars($this->first_name);
  120. // Build
  121. $builder = new EmailBuilder;
  122. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  123. $builder->addLeadParagraph(__("Your application has been successful!"));
  124. $builder->addParagraph(sprintf(__("Welcome to %s! 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."), APP_NAME));
  125. $builder->addParagraph(__("When you're ready to try a Practice test, in your account and select the \"Practice\" option."));
  126. $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."));
  127. $builder->addParagraph(sprintf(__("2. Email us at %s and let us know that you're ready."), "contact@" . DOMAIN));
  128. $builder->addParagraph(__("3. We will allocate a test to you as soon as possible."));
  129. $builder->addParagraph(__("Remember to check your online account for when tests become available."));
  130. $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."));
  131. $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."));
  132. $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."));
  133. $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."));
  134. $builder->addParagraph(sprintf(__("Once again welcome to %s - Accept, Begin, Complete and get paid for up to 8 minutes work!"), APP_NAME));
  135. $content = $builder->build();
  136. // Send
  137. $mail = new Email;
  138. $mail->setHtmlContent($content);
  139. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  140. $mail->addAddress($this->email, $this->getDisplayName());
  141. $mail->setSubject(__("ID accepted and verified"));
  142. $mail->send();
  143. return true;
  144. }
  145. public function sendIDFailEmail($reason = "No reason given.") {
  146. // Content
  147. $name = htmlspecialchars($this->first_name);
  148. $reason = htmlspecialchars($reason);
  149. // Build
  150. $builder = new EmailBuilder;
  151. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  152. $builder->addLeadParagraph(__("Thank you for submitting your ID details."));
  153. $builder->addParagraph(__("Unfortunately we could not verify and accept your documents and/or image."));
  154. $builder->addParagraph(sprintf(__("The reason for this is: %s"), $reason));
  155. $builder->addParagraph(__("Please log in again, resubmit and we will try again."));
  156. $builder->addParagraph(__("We look forward to welcoming you onboard soon!"));
  157. $content = $builder->build();
  158. // Send
  159. $mail = new Email;
  160. $mail->setHtmlContent($content);
  161. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  162. $mail->addAddress($this->email, $this->getDisplayName());
  163. $mail->setSubject(__("We could not verify your ID details"));
  164. $mail->send();
  165. return true;
  166. }
  167. public function sendPasswordResetEmail() {
  168. $this->reset_token = bin2hex(random_bytes(16));
  169. $this->commit();
  170. // Content
  171. $name = htmlspecialchars($this->first_name);
  172. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  173. // Build
  174. $builder = new EmailBuilder;
  175. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  176. $builder->addLeadParagraph(__("A reset password request has been received."));
  177. $builder->addParagraph(__("To reset your password, please tap on the following link:"));
  178. $builder->addParagraph("<a href=\"$link\">$link</a>");
  179. $builder->addParagraph(sprintf(__("If you %sdid not%s request for your password to be reset, please ignore this email."), '<strong>', '</strong>'));
  180. $content = $builder->build();
  181. // Send
  182. $mail = new Email;
  183. $mail->setHtmlContent($content);
  184. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  185. $mail->addAddress($this->email, $this->getDisplayName());
  186. $mail->setSubject(__("Password reset instructions"));
  187. $mail->send();
  188. return true;
  189. }
  190. public function canAdmin() {
  191. return $this->level >= User::LEVEL_ADMIN;
  192. }
  193. public function getReferrer() {
  194. return is_null($this->referrer_id) ? false : User::get($this->referrer_id);
  195. }
  196. public function getDirectory() {
  197. $directory = ROOT_DIR . "/files/users/" . $this->id;
  198. if(!file_exists($directory)) mkdir($directory, 0777, true);
  199. return $directory;
  200. }
  201. public function getLevelText() {
  202. return User::LEVELS[$this->level];
  203. }
  204. public function getLevelClass() {
  205. switch($this->level) {
  206. case User::LEVEL_BANNED: return "danger";
  207. case User::LEVEL_REGISTERED: return "secondary";
  208. case User::LEVEL_UNVERIFIED: return "warning";
  209. case User::LEVEL_DEACTIVATED: return "secondary";
  210. case User::LEVEL_TESTER: return "success";
  211. case User::LEVEL_VENUE: return "info";
  212. case User::LEVEL_CUSTOMER: return "info";
  213. case User::LEVEL_ADMIN: return "info";
  214. case User::LEVEL_SUPERADMIN: return "info";
  215. default:
  216. return "primary";
  217. }
  218. }
  219. public function isAdmin() {
  220. return ($this->level == User::LEVEL_ADMIN || $this->level == User::LEVEL_SUPERADMIN);
  221. }
  222. public function isCustomer() {
  223. return $this->level == User::LEVEL_CUSTOMER;
  224. }
  225. public function isTester() {
  226. return $this->level == User::LEVEL_TESTER;
  227. }
  228. public function isDeactivated() {
  229. return $this->level == User::LEVEL_DEACTIVATED;
  230. }
  231. public function getGenderText() {
  232. if($this->gender == "M") return MALE;
  233. if($this->gender == "F") return FEMALE;
  234. return "Unspecified";
  235. }
  236. public function getAge() {
  237. if(is_null($this->date_of_birth)) return __("N/A");
  238. $now = new DateTime("now");
  239. $dob = new DateTime($this->date_of_birth);
  240. return $dob->diff($now)->y;
  241. }
  242. public function getDisplayName() {
  243. if(empty($this->email)) return __("Deleted User");
  244. $parts = array_filter([$this->first_name, $this->last_name]);
  245. return implode(" ", $parts);
  246. }
  247. public function hasPermission($object) {
  248. if($this->isAdmin()) return true;
  249. $customers = $this->getCustomers();
  250. $customer_ids = array_column($customers, "id");
  251. if($object instanceof Customer) {
  252. return in_array($object->id, $customer_ids);
  253. }
  254. if($object instanceof Location) {
  255. return in_array($object->customer_id, $customer_ids);
  256. }
  257. if($object instanceof Project) {
  258. return in_array($object->customer_id, $customer_ids);
  259. }
  260. if($object instanceof Template) {
  261. return in_array($object->customer_id, $customer_ids);
  262. }
  263. return false;
  264. }
  265. public function deactivate() {
  266. // Decline all pending tests
  267. $pending_tests = $this->getPendingTests();
  268. foreach($pending_tests as $test) {
  269. $test->decline();
  270. }
  271. // Set user level to Deactivated
  272. $this->level = User::LEVEL_DEACTIVATED;
  273. return $this->commit();
  274. }
  275. public function softDelete() {
  276. // Delete identification documents
  277. $this->deleteIDFiles();
  278. // Delete addresses
  279. $this->deleteAddresses();
  280. // Decline all pending tests
  281. $pending_tests = $this->getPendingTests();
  282. foreach($pending_tests as $test) {
  283. $test->decline();
  284. }
  285. // Delete personal information
  286. $this->email = null;
  287. $this->title = null;
  288. $this->first_name = null;
  289. $this->last_name = null;
  290. $this->phone = null;
  291. $this->bank_name = null;
  292. $this->bank_sort_code = null;
  293. $this->bank_account_number = null;
  294. $this->paypal_email = null;
  295. // Set deleted time
  296. $this->dts_deleted = Util::getDateTime();
  297. $success = $this->commit();
  298. return true;
  299. }
  300. public function deleteIDFiles() {
  301. $files = UserFile::query([
  302. "user_id" => $this->id,
  303. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  304. ]);
  305. foreach($files as $file) {
  306. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  307. if(file_exists($path)) unlink($path);
  308. $file->delete();
  309. }
  310. return true;
  311. }
  312. public function deleteAddresses() {
  313. $addresses = $this->getAddresses();
  314. foreach($addresses as $address) {
  315. $address->delete();
  316. }
  317. return true;
  318. }
  319. public function getAddresses() {
  320. $addresses = UserAddress::query([
  321. "user_id" => $this->id,
  322. ], [
  323. "sort" => "name"
  324. ]);
  325. return $addresses;
  326. }
  327. public function getInvoiceAddress() {
  328. $addresses = UserAddress::query([
  329. "user_id" => $this->id,
  330. ]);
  331. foreach($addresses as $address) {
  332. if($address->invoice) return $address;
  333. }
  334. // If only one address in account, just return it
  335. if(count($addresses) == 1) return array_shift($addresses);
  336. return false;
  337. }
  338. /**
  339. * @return Customer[]
  340. */
  341. public function getCustomers() {
  342. if($this->level >= User::LEVEL_ADMIN) {
  343. return Customer::query([], ["sort" => "name"]);
  344. }
  345. if($this->level == User::LEVEL_CUSTOMER) {
  346. $links = UserCustomer::query(["user_id" => $this->id]);
  347. return Customer::query([
  348. "id" => array_column($links, "customer_id")
  349. ], ["sort" => "name"]);
  350. }
  351. return [];
  352. }
  353. public function getPendingTests() {
  354. return Test::query([
  355. "tester_id" => $this->id,
  356. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
  357. ], [
  358. "sort" => "dts_assigned",
  359. "order" => "desc"
  360. ]);
  361. }
  362. public function getCompletedTests() {
  363. return Test::query([
  364. "tester_id" => $this->id,
  365. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  366. ], [
  367. "sort" => "dts_start",
  368. "order" => "desc"
  369. ]);
  370. }
  371. public function getReferrals() {
  372. return User::query([
  373. "referrer_id" => $this->id
  374. ]);
  375. }
  376. public function getPreferredPaymentMethodText() {
  377. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  378. }
  379. public function getURL() {
  380. return "/account/users/{$this->id}";
  381. }
  382. }