user.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. <?php
  2. use Carbon\CarbonImmutable;
  3. /**
  4. * Class User
  5. *
  6. * @property int $id
  7. * @property string $email
  8. * @property int $old_id
  9. * @property int $referrer_id
  10. * @property string $password
  11. * @property bool $activated
  12. * @property bool $archived
  13. * @property int $level
  14. * @property int $score
  15. * @property string $title
  16. * @property string $first_name
  17. * @property string $last_name
  18. * @property string $gender
  19. * @property string $date_of_birth
  20. * @property string $locale
  21. * @property bool $reported
  22. * @property string $reported_comment
  23. * @property bool $can_claim
  24. * @property string $phone
  25. * @property string $verify_token
  26. * @property string $reset_token
  27. * @property bool $terms_accepted
  28. * @property bool $newsletter_consent
  29. * @property int $preferred_payment_method
  30. * @property string $bank_name
  31. * @property string $bank_sort_code
  32. * @property string $bank_account_number
  33. * @property string $paypal_email
  34. * @property string $dts_registered
  35. * @property string $dts_last_seen
  36. * @property string $dts_verified
  37. * @property string $dts_deleted
  38. * @property int $verifier_id
  39. * @property string $verifier_notes
  40. */
  41. class User extends Model {
  42. protected static $table = "user";
  43. protected static $id_field = "id";
  44. protected static $fillable_fields = ["email", "old_id", "referrer_id", "password", "activated", "archived", "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"];
  45. // Constants
  46. const LEVEL_BANNED = 0;
  47. const LEVEL_REGISTERED = 1;
  48. const LEVEL_UNVERIFIED = 2;
  49. const LEVEL_DEACTIVATED = 4;
  50. const LEVEL_TESTER = 8;
  51. const LEVEL_VENUE = 16;
  52. const LEVEL_CUSTOMER = 32;
  53. const LEVEL_TESTER_MANAGER = 48;
  54. const LEVEL_ADMIN = 64;
  55. const LEVEL_SUPERADMIN = 128;
  56. const LEVELS = [
  57. 0 => "Banned",
  58. 1 => "Registered",
  59. 2 => "Unverified",
  60. 4 => "Deactivated",
  61. 8 => "Tester",
  62. 16 => "Venue",
  63. 32 => "Customer",
  64. 48 => "Tester Manager",
  65. 64 => "Admin",
  66. 128 => "Super Admin",
  67. ];
  68. const PAYMENT_METHOD_BANK_TRANSFER = 1;
  69. const PAYMENT_METHOD_PAYPAL = 2;
  70. const PAYMENT_METHODS = [
  71. 1 => "Bank Transfer",
  72. 2 => "PayPal",
  73. ];
  74. // Static methods
  75. /**
  76. * @return array
  77. */
  78. public static function titles(): array {
  79. return [
  80. "Miss" => MISS,
  81. "Mr" => MR,
  82. "Ms" => MS,
  83. "Dr" => DR
  84. ];
  85. }
  86. /**
  87. * @return array
  88. */
  89. public static function genders(): array {
  90. return [
  91. "Female" => FEMALE,
  92. "Male" => MALE,
  93. "Unspecified" => UNSPECIFIED
  94. ];
  95. }
  96. /**
  97. * @param string $password
  98. * @return bool
  99. */
  100. public static function validatePassword(string $password): bool {
  101. $lowercase = preg_match("/[a-z]/", $password);
  102. $uppercase = preg_match("/[A-Z]/", $password);
  103. $number = preg_match("/[0-9]/", $password);
  104. $special = preg_match("/[^0-9a-zA-Z]/", $password);
  105. $length = strlen($password) >= 6;
  106. return ($lowercase && $uppercase && $number && $special && $length);
  107. }
  108. // Object methods
  109. /**
  110. * @return bool
  111. */
  112. public function hasValidPaymentMethod(): bool {
  113. switch($this->preferred_payment_method) {
  114. case User::PAYMENT_METHOD_BANK_TRANSFER: {
  115. if(empty($this->bank_name)) return false;
  116. if(empty($this->bank_sort_code)) return false;
  117. if(empty($this->bank_account_number)) return false;
  118. break;
  119. }
  120. case User::PAYMENT_METHOD_PAYPAL: {
  121. if(empty($this->paypal_email)) return false;
  122. break;
  123. }
  124. }
  125. return true;
  126. }
  127. /**
  128. * @return bool
  129. * @throws Exception
  130. */
  131. public function sendActivationEmail() {
  132. $this->verify_token = bin2hex(random_bytes(16));
  133. $this->commit();
  134. // Content
  135. $name = htmlspecialchars($this->first_name);
  136. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
  137. // Build
  138. $builder = new EmailBuilder;
  139. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  140. $builder->addLeadParagraph(__("Thanks for registering!"));
  141. $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));
  142. $builder->addParagraph("<a href=\"$link\">$link</a>");
  143. $content = $builder->build();
  144. // Send
  145. $mail = new Email;
  146. $mail->setHtmlContent($content);
  147. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  148. $mail->addAddress($this->email, $this->getDisplayName());
  149. $mail->setSubject(__("Verify your email address"));
  150. $mail->send();
  151. return true;
  152. }
  153. /**
  154. * @return bool
  155. * @throws Exception
  156. */
  157. public function sendWelcomeEmail() {
  158. // Content
  159. $name = htmlspecialchars($this->first_name);
  160. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
  161. // Build
  162. $builder = new EmailBuilder;
  163. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  164. $builder->addLeadParagraph(__("Thanks for verifying your email address!"));
  165. $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));
  166. $builder->addParagraph("<a href=\"$link\">$link</a>");
  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(sprintf(__("Welcome to %s"), APP_NAME));
  174. $mail->send();
  175. return true;
  176. }
  177. /**
  178. * @return bool
  179. * @throws Exception
  180. */
  181. public function sendIDPassEmail() {
  182. // Content
  183. $name = htmlspecialchars($this->first_name);
  184. // Build
  185. $builder = new EmailBuilder;
  186. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  187. $builder->addLeadParagraph(__("Your application has been successful!"));
  188. $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));
  189. $builder->addParagraph(__("When you're ready to try a Practice test, in your account and select the \"Practice\" option."));
  190. $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."));
  191. $builder->addParagraph(sprintf(__("2. Email us at %s and let us know that you're ready."), "contact@" . DOMAIN));
  192. $builder->addParagraph(__("3. We will allocate a test to you as soon as possible."));
  193. $builder->addParagraph(__("Remember to check your online account for when tests become available."));
  194. $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."));
  195. $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."));
  196. $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."));
  197. $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."));
  198. $builder->addParagraph(sprintf(__("Once again welcome to %s - Accept, Begin, Complete and get paid for up to 8 minutes work!"), APP_NAME));
  199. $content = $builder->build();
  200. // Send
  201. $mail = new Email;
  202. $mail->setHtmlContent($content);
  203. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  204. $mail->addAddress($this->email, $this->getDisplayName());
  205. $mail->setSubject(__("ID accepted and verified"));
  206. $mail->send();
  207. return true;
  208. }
  209. /**
  210. * @param string $reason
  211. * @return bool
  212. * @throws Exception
  213. */
  214. public function sendIDFailEmail($reason = "No reason given.") {
  215. // Content
  216. $name = htmlspecialchars($this->first_name);
  217. $reason = htmlspecialchars($reason);
  218. // Build
  219. $builder = new EmailBuilder;
  220. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  221. $builder->addLeadParagraph(__("Thank you for submitting your ID details."));
  222. $builder->addParagraph(__("Unfortunately we could not verify and accept your documents and/or image."));
  223. $builder->addParagraph(sprintf(__("The reason for this is: %s"), $reason));
  224. $builder->addParagraph(__("Please log in again, resubmit and we will try again."));
  225. $builder->addParagraph(__("We look forward to welcoming you onboard soon!"));
  226. $content = $builder->build();
  227. // Send
  228. $mail = new Email;
  229. $mail->setHtmlContent($content);
  230. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  231. $mail->addAddress($this->email, $this->getDisplayName());
  232. $mail->setSubject(__("We could not verify your ID details"));
  233. $mail->send();
  234. return true;
  235. }
  236. /**
  237. * @return bool
  238. * @throws Exception
  239. */
  240. public function sendPasswordResetEmail() {
  241. $this->reset_token = bin2hex(random_bytes(16));
  242. $this->commit();
  243. // Content
  244. $name = htmlspecialchars($this->first_name);
  245. $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
  246. // Build
  247. $builder = new EmailBuilder;
  248. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  249. $builder->addLeadParagraph(__("A reset password request has been received."));
  250. $builder->addParagraph(__("To reset your password, please tap on the following link:"));
  251. $builder->addParagraph("<a href=\"$link\">$link</a>");
  252. $builder->addParagraph(sprintf(__("If you %sdid not%s request for your password to be reset, please ignore this email."), '<strong>', '</strong>'));
  253. $content = $builder->build();
  254. // Send
  255. $mail = new Email;
  256. $mail->setHtmlContent($content);
  257. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  258. $mail->addAddress($this->email, $this->getDisplayName());
  259. $mail->setSubject(__("Password reset instructions"));
  260. $mail->send();
  261. return true;
  262. }
  263. /**
  264. * @return bool
  265. * @throws Exception
  266. */
  267. public function sendDocumentationEmail(): bool {
  268. // Content
  269. $name = htmlspecialchars($this->first_name);
  270. // Build
  271. $builder = new EmailBuilder;
  272. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  273. $builder->addLeadParagraph(__("Thanks for registering with us!"));
  274. $builder->addParagraph(__("The good news is that we have tests for you straight away."));
  275. $builder->addParagraph(__("We are testing if you are asked for ID in a pubs and also in amusement arcades before playing a slot machine. I have attached two documents with the test protocol for you to read and we will assign you the tests. Take a look at the protocol and let us know when you are ready."));
  276. $content = $builder->build();
  277. // Send
  278. $mail = new Email;
  279. $mail->setHtmlContent($content);
  280. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  281. $mail->addAddress($this->email, $this->getDisplayName());
  282. $mail->addAttachment("public/files/testing_procedure_arcades.pdf", "Testing Procedure - Arcades.pdf");
  283. $mail->addAttachment("public/files/testing_procedure_pubs.pdf", "Testing Procedure - Pubs.pdf");
  284. $mail->setSubject(__("Documentation"));
  285. return $mail->send();
  286. }
  287. /**
  288. * @return bool
  289. */
  290. public function canAdmin() {
  291. return $this->isAdmin();
  292. }
  293. /**
  294. * @return User|null
  295. * @throws Exception
  296. */
  297. public function getReferrer(): ?User {
  298. return is_null($this->referrer_id) ? null : User::get($this->referrer_id);
  299. }
  300. public function getDirectory() {
  301. $directory = ROOT_DIR . "/files/users/" . $this->id;
  302. if(!file_exists($directory)) mkdir($directory, 0777, true);
  303. return $directory;
  304. }
  305. public function getLevelText() {
  306. return User::LEVELS[$this->level];
  307. }
  308. public function getLevelClass() {
  309. switch($this->level) {
  310. case User::LEVEL_BANNED: return "danger";
  311. case User::LEVEL_REGISTERED: return "secondary";
  312. case User::LEVEL_UNVERIFIED: return "warning";
  313. case User::LEVEL_DEACTIVATED: return "secondary";
  314. case User::LEVEL_TESTER: return "success";
  315. case User::LEVEL_VENUE: return "info";
  316. case User::LEVEL_CUSTOMER: return "info";
  317. case User::LEVEL_ADMIN: return "info";
  318. case User::LEVEL_SUPERADMIN: return "info";
  319. default:
  320. return "primary";
  321. }
  322. }
  323. public function isTesterManager(): bool {
  324. return $this->level >= User::LEVEL_TESTER_MANAGER;
  325. }
  326. public function isAdmin(): bool {
  327. return $this->level >= User::LEVEL_ADMIN;
  328. }
  329. public function isCustomer(): bool {
  330. return $this->level == User::LEVEL_CUSTOMER;
  331. }
  332. public function isTester(): bool {
  333. return $this->level == User::LEVEL_TESTER;
  334. }
  335. public function isDeactivated(): bool {
  336. return $this->level == User::LEVEL_DEACTIVATED;
  337. }
  338. public function getGenderText(): string {
  339. if($this->gender == "M") return MALE;
  340. if($this->gender == "F") return FEMALE;
  341. return "Unspecified";
  342. }
  343. /**
  344. * @return int|string
  345. * @throws Exception
  346. */
  347. public function getAge() {
  348. if(is_null($this->date_of_birth)) return __("N/A");
  349. $now = new DateTime("now");
  350. $dob = new DateTime($this->date_of_birth);
  351. return $dob->diff($now)->y;
  352. }
  353. public function getDisplayName() {
  354. if(empty($this->email)) return __("Deleted User");
  355. $parts = array_filter([$this->first_name, $this->last_name]);
  356. return implode(" ", $parts);
  357. }
  358. /**
  359. * @param $object
  360. * @return bool
  361. * @throws Exception
  362. */
  363. public function hasPermission($object): bool {
  364. if($this->canAccessLocations()) return true;
  365. $customers = $this->getCustomers();
  366. $customer_ids = array_column($customers, "id");
  367. if($object instanceof Customer) {
  368. return in_array($object->id, $customer_ids);
  369. }
  370. if($object instanceof Location) {
  371. return in_array($object->customer_id, $customer_ids);
  372. }
  373. if($object instanceof Project) {
  374. return in_array($object->customer_id, $customer_ids);
  375. }
  376. if($object instanceof Template) {
  377. return in_array($object->customer_id, $customer_ids);
  378. }
  379. return false;
  380. }
  381. /**
  382. * @return bool
  383. * @throws Exception
  384. */
  385. public function deactivate() {
  386. // Decline all pending tests
  387. $pending_tests = $this->getPendingTests();
  388. foreach($pending_tests as $test) {
  389. $test->decline();
  390. }
  391. // Set user level to Deactivated
  392. $this->level = User::LEVEL_DEACTIVATED;
  393. return $this->commit();
  394. }
  395. /**
  396. * @return bool
  397. * @throws Exception
  398. */
  399. public function softDelete(): bool {
  400. // Delete identification documents
  401. $this->deleteIDFiles();
  402. // Delete addresses
  403. $this->deleteAddresses();
  404. // Decline all pending tests
  405. $pending_tests = $this->getPendingTests();
  406. foreach($pending_tests as $test) {
  407. $test->decline();
  408. }
  409. // Delete personal information
  410. $this->email = null;
  411. $this->title = null;
  412. $this->first_name = null;
  413. $this->last_name = null;
  414. $this->phone = null;
  415. $this->bank_name = null;
  416. $this->bank_sort_code = null;
  417. $this->bank_account_number = null;
  418. $this->paypal_email = null;
  419. // Set deleted time
  420. $this->dts_deleted = Util::getDateTime();
  421. return $this->commit();
  422. }
  423. /**
  424. * @return bool
  425. * @throws Exception
  426. */
  427. public function deleteIDFiles(): bool {
  428. $files = UserFile::query([
  429. "user_id" => $this->id,
  430. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  431. ]);
  432. foreach($files as $file) {
  433. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  434. if(file_exists($path)) unlink($path);
  435. $file->delete();
  436. }
  437. return true;
  438. }
  439. /**
  440. * @return bool
  441. * @throws Exception
  442. */
  443. public function deleteAddresses(): bool {
  444. $addresses = $this->getAddresses();
  445. foreach($addresses as $address) {
  446. $address->delete();
  447. }
  448. return true;
  449. }
  450. /**
  451. * @return UserAddress[]
  452. * @throws Exception
  453. */
  454. public function getAddresses(): array {
  455. $addresses = UserAddress::query([
  456. "user_id" => $this->id,
  457. ], [
  458. "sort" => "name"
  459. ]);
  460. return $addresses;
  461. }
  462. /**
  463. * @return UserAddress|null
  464. * @throws Exception
  465. */
  466. public function getInvoiceAddress(): ?UserAddress {
  467. $addresses = UserAddress::query([
  468. "user_id" => $this->id,
  469. ]);
  470. foreach($addresses as $address) {
  471. if($address->invoice) return $address;
  472. }
  473. // If only one address in account, just return it
  474. if(count($addresses) == 1) return array_shift($addresses);
  475. return null;
  476. }
  477. /**
  478. * @return Customer[]
  479. * @throws Exception
  480. */
  481. public function getCustomers() {
  482. if($this->canAccessCustomers()) {
  483. return Customer::query([], ["sort" => "name"]);
  484. }
  485. if($this->isCustomer()) {
  486. $links = UserCustomer::query(["user_id" => $this->id]);
  487. return Customer::query([
  488. "id" => array_column($links, "customer_id")
  489. ], ["sort" => "name"]);
  490. }
  491. return [];
  492. }
  493. /**
  494. * @return Test[]
  495. * @throws Exception
  496. */
  497. public function getPendingTests(): array {
  498. return Test::query([
  499. "tester_id" => $this->id,
  500. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
  501. ], [
  502. "sort" => "dts_assigned",
  503. "order" => "desc"
  504. ]);
  505. }
  506. /**
  507. * @return Test[]
  508. * @throws Exception
  509. */
  510. public function getCompletedTests(): array {
  511. return Test::query([
  512. "tester_id" => $this->id,
  513. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  514. ], [
  515. "sort" => "dts_start",
  516. "order" => "desc"
  517. ]);
  518. }
  519. /**
  520. * @return User[]
  521. * @throws Exception
  522. */
  523. public function getReferrals(): array {
  524. return User::query([
  525. "referrer_id" => $this->id
  526. ]);
  527. }
  528. /**
  529. * @return string
  530. */
  531. public function getPreferredPaymentMethodText(): string {
  532. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  533. }
  534. /**
  535. * @return string
  536. */
  537. public function getURL(): string {
  538. return "/account/users/{$this->id}";
  539. }
  540. public function isUnverified() {
  541. return $this->level <= User::LEVEL_UNVERIFIED;
  542. }
  543. public function isRegistered() {
  544. return $this->level == User::LEVEL_REGISTERED;
  545. }
  546. public function isSuperAdmin(): bool {
  547. return $this->level == User::LEVEL_SUPERADMIN;
  548. }
  549. public function canAccessCustomers(): bool {
  550. return $this->level >= User::LEVEL_TESTER_MANAGER;
  551. }
  552. public function canAccessMapOverview(): bool {
  553. return $this->level >= User::LEVEL_TESTER_MANAGER;
  554. }
  555. public function canAccessUsers(): bool {
  556. return $this->level >= User::LEVEL_TESTER_MANAGER;
  557. }
  558. public function canAccessProjects(): bool {
  559. return $this->level >= User::LEVEL_TESTER_MANAGER;
  560. }
  561. public function canAccessTemplates(): bool {
  562. return $this->level >= User::LEVEL_ADMIN;
  563. }
  564. public function canAccessVenueTypes(): bool {
  565. return $this->level >= User::LEVEL_ADMIN;
  566. }
  567. public function canAccessInvoices(): bool {
  568. return $this->level >= User::LEVEL_ADMIN;
  569. }
  570. public function canAccessLog(): bool {
  571. return $this->level >= User::LEVEL_ADMIN;
  572. }
  573. public function canAccessLocations(): bool {
  574. return $this->level >= User::LEVEL_CUSTOMER;
  575. }
  576. public function canAccessReports(): bool {
  577. return $this->level >= User::LEVEL_CUSTOMER;
  578. }
  579. public function canAccessTestResults(): bool {
  580. return $this->level >= User::LEVEL_CUSTOMER;
  581. }
  582. public function canAccessTests(): bool {
  583. return $this->isTester() || $this->isTesterManager();
  584. }
  585. public function canAccessPractice(): bool {
  586. return $this->isTester() || $this->isAdmin() || $this->isUnverified() || $this->isRegistered();
  587. }
  588. public function canAccessAddresses(): bool {
  589. return $this->level == User::LEVEL_TESTER;
  590. }
  591. public function canAccessCompletedTests(): bool {
  592. return $this->level == User::LEVEL_TESTER;
  593. }
  594. public function canSubmitId(): bool {
  595. return $this->level == User::LEVEL_REGISTERED;
  596. }
  597. public function canDeactivate(): bool {
  598. return $this->level == User::LEVEL_TESTER;
  599. }
  600. public function canReactivate(): bool {
  601. return $this->level == User::LEVEL_DEACTIVATED;
  602. }
  603. public function canAccessAdminTestsList(): bool {
  604. return $this->isTesterManager();
  605. }
  606. public function canExportProjectCsv(): bool {
  607. return $this->isAdmin();
  608. }
  609. public function canVerifyTests(): bool {
  610. return $this->level >= User::LEVEL_TESTER_MANAGER;
  611. }
  612. public function canAccessTestResult(): bool {
  613. return $this->level >= User::LEVEL_VENUE;
  614. }
  615. public function canEditUsers(): bool {
  616. return $this->isTesterManager();
  617. }
  618. public function canSeeMissingData(): bool {
  619. return $this->level >= self::LEVEL_CUSTOMER;
  620. }
  621. public function getDateTimeDateOfBirth(): ?CarbonImmutable {
  622. if(!$this->date_of_birth) return null;
  623. return CarbonImmutable::parse($this->date_of_birth);
  624. }
  625. public function isArchived(): bool {
  626. return $this->archived;
  627. }
  628. /**
  629. * @return CarbonImmutable|null
  630. * @throws Exception
  631. */
  632. public function getDateTimeLastTest(): ?CarbonImmutable {
  633. $test = $this->getCompletedTests()[0] ?? null;
  634. return $test ? $test->getDateTimeStart() : null;
  635. }
  636. }