user.php 21 KB

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