user.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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->level == User::LEVEL_SUPERADMIN) return true;
  249. if($this->level == User::LEVEL_ADMIN) return true;
  250. $customers = $this->getCustomers();
  251. $customer_ids = array_column($customers, "id");
  252. if(is_a($object, "Customer")) {
  253. return in_array($object->id, $customer_ids);
  254. }
  255. if(is_a($object, "Location")) {
  256. return in_array($object->customer_id, $customer_ids);
  257. }
  258. if(is_a($object, "Project")) {
  259. return in_array($object->customer_id, $customer_ids);
  260. }
  261. if(is_a($object, "Template")) {
  262. return in_array($object->customer_id, $customer_ids);
  263. }
  264. return false;
  265. }
  266. public function deactivate() {
  267. // Decline all pending tests
  268. $pending_tests = $this->getPendingTests();
  269. foreach($pending_tests as $test) {
  270. $test->decline();
  271. }
  272. // Set user level to Deactivated
  273. $this->level = User::LEVEL_DEACTIVATED;
  274. return $this->commit();
  275. }
  276. public function softDelete() {
  277. // Delete identification documents
  278. $this->deleteIDFiles();
  279. // Delete addresses
  280. $this->deleteAddresses();
  281. // Decline all pending tests
  282. $pending_tests = $this->getPendingTests();
  283. foreach($pending_tests as $test) {
  284. $test->decline();
  285. }
  286. // Delete personal information
  287. $this->email = null;
  288. $this->title = null;
  289. $this->first_name = null;
  290. $this->last_name = null;
  291. $this->phone = null;
  292. $this->bank_name = null;
  293. $this->bank_sort_code = null;
  294. $this->bank_account_number = null;
  295. $this->paypal_email = null;
  296. // Set deleted time
  297. $this->dts_deleted = Util::getDateTime();
  298. $success = $this->commit();
  299. return true;
  300. }
  301. public function deleteIDFiles() {
  302. $files = UserFile::query([
  303. "user_id" => $this->id,
  304. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  305. ]);
  306. foreach($files as $file) {
  307. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  308. if(file_exists($path)) unlink($path);
  309. $file->delete();
  310. }
  311. return true;
  312. }
  313. public function deleteAddresses() {
  314. $addresses = $this->getAddresses();
  315. foreach($addresses as $address) {
  316. $address->delete();
  317. }
  318. return true;
  319. }
  320. public function getAddresses() {
  321. $addresses = UserAddress::query([
  322. "user_id" => $this->id,
  323. ], [
  324. "sort" => "name"
  325. ]);
  326. return $addresses;
  327. }
  328. public function getInvoiceAddress() {
  329. $addresses = UserAddress::query([
  330. "user_id" => $this->id,
  331. ]);
  332. foreach($addresses as $address) {
  333. if($address->invoice) return $address;
  334. }
  335. // If only one address in account, just return it
  336. if(count($addresses) == 1) return array_shift($addresses);
  337. return false;
  338. }
  339. public function getCustomers() {
  340. if($this->level >= User::LEVEL_ADMIN) {
  341. return Customer::query([], ["sort" => "name"]);
  342. }
  343. if($this->level == User::LEVEL_CUSTOMER) {
  344. $links = UserCustomer::query(["user_id" => $this->id]);
  345. return Customer::query([
  346. "id" => array_column($links, "customer_id")
  347. ], ["sort" => "name"]);
  348. }
  349. return [];
  350. }
  351. public function getPendingTests() {
  352. return Test::query([
  353. "tester_id" => $this->id,
  354. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
  355. ], [
  356. "sort" => "dts_assigned",
  357. "order" => "desc"
  358. ]);
  359. }
  360. public function getCompletedTests() {
  361. return Test::query([
  362. "tester_id" => $this->id,
  363. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  364. ], [
  365. "sort" => "dts_start",
  366. "order" => "desc"
  367. ]);
  368. }
  369. public function getReferrals() {
  370. return User::query([
  371. "referrer_id" => $this->id
  372. ]);
  373. }
  374. public function getPreferredPaymentMethodText() {
  375. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  376. }
  377. public function getURL() {
  378. return "/account/users/{$this->id}";
  379. }
  380. }