user.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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->addAttachment("public/files/completing_the_questionnaire.pdf", "Completing the Questionnaire.pdf");
  302. $mail->setSubject(__("Documentation"));
  303. return $mail->send();
  304. }
  305. /**
  306. * @return bool
  307. */
  308. public function canAdmin() {
  309. return $this->isAdmin();
  310. }
  311. /**
  312. * @return User|null
  313. * @throws Exception
  314. */
  315. public function getReferrer(): ?User {
  316. return is_null($this->referrer_id) ? null : User::get($this->referrer_id);
  317. }
  318. public function getDirectory() {
  319. $directory = ROOT_DIR . "/files/users/" . $this->id;
  320. if(!file_exists($directory)) mkdir($directory, 0777, true);
  321. return $directory;
  322. }
  323. public function getLevelText() {
  324. return User::LEVELS[$this->level];
  325. }
  326. public function getLevelClass() {
  327. switch($this->level) {
  328. case User::LEVEL_BANNED: return "danger";
  329. case User::LEVEL_REGISTERED: return "secondary";
  330. case User::LEVEL_UNVERIFIED: return "warning";
  331. case User::LEVEL_DEACTIVATED: return "secondary";
  332. case User::LEVEL_TESTER: return "success";
  333. case User::LEVEL_VENUE: return "info";
  334. case User::LEVEL_CUSTOMER: return "info";
  335. case User::LEVEL_ADMIN: return "info";
  336. case User::LEVEL_SUPERADMIN: return "info";
  337. default:
  338. return "primary";
  339. }
  340. }
  341. public function isTesterManager(): bool {
  342. return $this->level >= User::LEVEL_TESTER_MANAGER;
  343. }
  344. public function isAdmin(): bool {
  345. return $this->level >= User::LEVEL_ADMIN;
  346. }
  347. public function isCustomer(): bool {
  348. return $this->level == User::LEVEL_CUSTOMER;
  349. }
  350. public function isTester(): bool {
  351. return $this->level == User::LEVEL_TESTER;
  352. }
  353. public function isDeactivated(): bool {
  354. return $this->level == User::LEVEL_DEACTIVATED;
  355. }
  356. public function getGenderText(): string {
  357. if($this->gender == "M") return MALE;
  358. if($this->gender == "F") return FEMALE;
  359. return "Unspecified";
  360. }
  361. /**
  362. * @return int|string
  363. * @throws Exception
  364. */
  365. public function getAge() {
  366. if(is_null($this->date_of_birth)) return __("N/A");
  367. $now = new DateTime("now");
  368. $dob = new DateTime($this->date_of_birth);
  369. return $dob->diff($now)->y;
  370. }
  371. public function getDisplayName() {
  372. if(empty($this->email)) return __("Deleted User");
  373. $parts = array_filter([$this->first_name, $this->last_name]);
  374. return implode(" ", $parts);
  375. }
  376. /**
  377. * @param $object
  378. * @return bool
  379. * @throws Exception
  380. */
  381. public function hasPermission($object): bool {
  382. if($this->canAccessLocations()) return true;
  383. $customers = $this->getCustomers();
  384. $customer_ids = array_column($customers, "id");
  385. if($object instanceof Customer) {
  386. return in_array($object->id, $customer_ids);
  387. }
  388. if($object instanceof Location) {
  389. return in_array($object->customer_id, $customer_ids);
  390. }
  391. if($object instanceof Project) {
  392. return in_array($object->customer_id, $customer_ids);
  393. }
  394. if($object instanceof Template) {
  395. return in_array($object->customer_id, $customer_ids);
  396. }
  397. return false;
  398. }
  399. /**
  400. * @return bool
  401. * @throws Exception
  402. */
  403. public function deactivate() {
  404. // Decline all pending tests
  405. $pending_tests = $this->getPendingTests();
  406. foreach($pending_tests as $test) {
  407. $test->decline();
  408. }
  409. // Set user level to Deactivated
  410. $this->level = User::LEVEL_DEACTIVATED;
  411. return $this->commit();
  412. }
  413. /**
  414. * @return bool
  415. * @throws Exception
  416. */
  417. public function softDelete(): bool {
  418. // Delete identification documents
  419. $this->deleteIDFiles();
  420. // Delete addresses
  421. $this->deleteAddresses();
  422. // Decline all pending tests
  423. $pending_tests = $this->getPendingTests();
  424. foreach($pending_tests as $test) {
  425. $test->decline();
  426. }
  427. // Delete personal information
  428. $this->email = null;
  429. $this->title = null;
  430. $this->first_name = null;
  431. $this->last_name = null;
  432. $this->phone = null;
  433. $this->bank_name = null;
  434. $this->bank_sort_code = null;
  435. $this->bank_account_number = null;
  436. $this->paypal_email = null;
  437. // Set deleted time
  438. $this->dts_deleted = Util::getDateTime();
  439. return $this->commit();
  440. }
  441. /**
  442. * @return bool
  443. * @throws Exception
  444. */
  445. public function deleteIDFiles(): bool {
  446. $files = UserFile::query([
  447. "user_id" => $this->id,
  448. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  449. ]);
  450. foreach($files as $file) {
  451. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  452. if(file_exists($path)) unlink($path);
  453. $file->delete();
  454. }
  455. return true;
  456. }
  457. /**
  458. * @return bool
  459. * @throws Exception
  460. */
  461. public function deleteAddresses(): bool {
  462. $addresses = $this->getAddresses();
  463. foreach($addresses as $address) {
  464. $address->delete();
  465. }
  466. return true;
  467. }
  468. /**
  469. * @return UserAddress[]
  470. * @throws Exception
  471. */
  472. public function getAddresses(): array {
  473. $addresses = UserAddress::query([
  474. "user_id" => $this->id,
  475. ], [
  476. "sort" => "name"
  477. ]);
  478. return $addresses;
  479. }
  480. /**
  481. * @return UserAddress|null
  482. * @throws Exception
  483. */
  484. public function getInvoiceAddress(): ?UserAddress {
  485. $addresses = UserAddress::query([
  486. "user_id" => $this->id,
  487. ]);
  488. foreach($addresses as $address) {
  489. if($address->invoice) return $address;
  490. }
  491. // If only one address in account, just return it
  492. if(count($addresses) == 1) return array_shift($addresses);
  493. return null;
  494. }
  495. /**
  496. * @return Customer[]
  497. * @throws Exception
  498. */
  499. public function getCustomers(array $nvp = []) {
  500. if($this->canAccessCustomers()) {
  501. return Customer::query($nvp, ["sort" => "name"]);
  502. }
  503. if($this->isCustomer()) {
  504. $links = UserCustomer::query(["user_id" => $this->id]);
  505. $nvp = array_merge($nvp, [
  506. "id" => array_column($links, "customer_id")
  507. ]);
  508. return Customer::query($nvp, ["sort" => "name"]);
  509. }
  510. return [];
  511. }
  512. /**
  513. * @return Test[]
  514. * @throws Exception
  515. */
  516. public function getPendingTests(): array {
  517. return Test::query([
  518. "tester_id" => $this->id,
  519. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
  520. ], [
  521. "sort" => "dts_assigned",
  522. "order" => "desc"
  523. ]);
  524. }
  525. /**
  526. * @return Test[]
  527. * @throws Exception
  528. */
  529. public function getCompletedTests(): array {
  530. return Test::query([
  531. "tester_id" => $this->id,
  532. "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
  533. ], [
  534. "sort" => "dts_start",
  535. "order" => "desc"
  536. ]);
  537. }
  538. /**
  539. * @return User[]
  540. * @throws Exception
  541. */
  542. public function getReferrals(): array {
  543. return User::query([
  544. "referrer_id" => $this->id
  545. ]);
  546. }
  547. /**
  548. * @return string
  549. */
  550. public function getPreferredPaymentMethodText(): string {
  551. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  552. }
  553. /**
  554. * @return string
  555. */
  556. public function getURL(): string {
  557. return "/account/users/{$this->id}";
  558. }
  559. public function isUnverified() {
  560. return $this->level <= User::LEVEL_UNVERIFIED;
  561. }
  562. public function isRegistered() {
  563. return $this->level == User::LEVEL_REGISTERED;
  564. }
  565. public function isSuperAdmin(): bool {
  566. return $this->level == User::LEVEL_SUPERADMIN;
  567. }
  568. public function canAccessCustomers(): bool {
  569. return $this->level >= User::LEVEL_TESTER_MANAGER;
  570. }
  571. public function canAccessMapOverview(): bool {
  572. return $this->level >= User::LEVEL_TESTER_MANAGER;
  573. }
  574. public function canAccessUsers(): bool {
  575. return $this->level >= User::LEVEL_TESTER_MANAGER;
  576. }
  577. public function canAccessProjects(): bool {
  578. return $this->level >= User::LEVEL_TESTER_MANAGER;
  579. }
  580. public function canAccessTemplates(): bool {
  581. return $this->level >= User::LEVEL_ADMIN;
  582. }
  583. public function canAccessVenueTypes(): bool {
  584. return $this->level >= User::LEVEL_ADMIN;
  585. }
  586. public function canAccessInvoices(): bool {
  587. return $this->level >= User::LEVEL_ADMIN;
  588. }
  589. public function canAccessLog(): bool {
  590. return $this->level >= User::LEVEL_ADMIN;
  591. }
  592. public function canAccessLocations(): bool {
  593. return $this->level >= User::LEVEL_CUSTOMER;
  594. }
  595. public function canAccessReports(): bool {
  596. return $this->level >= User::LEVEL_CUSTOMER;
  597. }
  598. public function canAccessTestResults(): bool {
  599. return $this->level >= User::LEVEL_CUSTOMER;
  600. }
  601. public function canAccessTests(): bool {
  602. return $this->isTester() || $this->isTesterManager();
  603. }
  604. public function canAccessPractice(): bool {
  605. return $this->isTester() || $this->isAdmin() || $this->isUnverified() || $this->isRegistered();
  606. }
  607. public function canAccessAddresses(): bool {
  608. return $this->level == User::LEVEL_TESTER;
  609. }
  610. public function canAccessCompletedTests(): bool {
  611. return $this->level == User::LEVEL_TESTER;
  612. }
  613. public function canSubmitId(): bool {
  614. return $this->level == User::LEVEL_REGISTERED;
  615. }
  616. public function canDeactivate(): bool {
  617. return $this->level == User::LEVEL_TESTER;
  618. }
  619. public function canReactivate(): bool {
  620. return $this->level == User::LEVEL_DEACTIVATED;
  621. }
  622. public function canAccessAdminTestsList(): bool {
  623. return $this->isTesterManager();
  624. }
  625. public function canExportProjectCsv(): bool {
  626. return $this->isAdmin();
  627. }
  628. public function canVerifyTests(): bool {
  629. return $this->level >= User::LEVEL_TESTER_MANAGER;
  630. }
  631. public function canAccessTestResult(): bool {
  632. return $this->level >= User::LEVEL_VENUE;
  633. }
  634. public function canEditUsers(): bool {
  635. return $this->isTesterManager();
  636. }
  637. public function canSeeMissingData(): bool {
  638. return $this->level >= self::LEVEL_CUSTOMER;
  639. }
  640. public function getDateTimeDateOfBirth(): ?CarbonImmutable {
  641. if(!$this->date_of_birth) return null;
  642. return CarbonImmutable::parse($this->date_of_birth);
  643. }
  644. public function isArchived(): bool {
  645. return $this->archived;
  646. }
  647. /**
  648. * @return CarbonImmutable|null
  649. * @throws Exception
  650. */
  651. public function getDateTimeLastTest(): ?CarbonImmutable {
  652. $test = $this->getCompletedTests()[0] ?? null;
  653. return $test ? $test->getDateTimeStart() : null;
  654. }
  655. public function formatDateTime(?DateTimeInterface $dt): string {
  656. if(!$dt) {
  657. return "N/A";
  658. }
  659. $carbon = CarbonImmutable::parse($dt);
  660. return $carbon->setTimezone(TIMEZONE)->format("d/m/Y H:i");
  661. }
  662. public function formatDate(?DateTimeInterface $dt): string {
  663. if(!$dt) {
  664. return "(null)";
  665. }
  666. $carbon = CarbonImmutable::parse($dt);
  667. return $carbon->setTimezone(TIMEZONE)->format("d/m/Y");
  668. }
  669. }