| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764 |
- <?php
- /**
- * Class User
- *
- * @property int $id
- * @property string $email
- * @property int $old_id
- * @property int $referrer_id
- * @property string $password
- * @property bool $activated
- * @property int $level
- * @property int $score
- * @property string $title
- * @property string $first_name
- * @property string $last_name
- * @property string $gender
- * @property string $date_of_birth
- * @property string $locale
- * @property bool $reported
- * @property string $reported_comment
- * @property bool $can_claim
- * @property string $phone
- * @property string $verify_token
- * @property string $reset_token
- * @property bool $terms_accepted
- * @property bool $newsletter_consent
- * @property int $preferred_payment_method
- * @property string $bank_name
- * @property string $bank_sort_code
- * @property string $bank_account_number
- * @property string $paypal_email
- * @property string $dts_registered
- * @property string $dts_last_seen
- * @property string $dts_verified
- * @property string $dts_deleted
- * @property int $verifier_id
- * @property string $verifier_notes
- */
- class User extends Model {
- protected static $table = "user";
- protected static $id_field = "id";
- 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"];
- // Constants
- const LEVEL_BANNED = 0;
- const LEVEL_REGISTERED = 1;
- const LEVEL_UNVERIFIED = 2;
- const LEVEL_DEACTIVATED = 4;
- const LEVEL_TESTER = 8;
- const LEVEL_VENUE = 16;
- const LEVEL_CUSTOMER = 32;
- const LEVEL_TESTER_MANAGER = 48;
- const LEVEL_ADMIN = 64;
- const LEVEL_SUPERADMIN = 128;
- const LEVELS = [
- 0 => "Banned",
- 1 => "Registered",
- 2 => "Unverified",
- 4 => "Deactivated",
- 8 => "Tester",
- 16 => "Venue",
- 32 => "Customer",
- 48 => "Tester Manager",
- 64 => "Admin",
- 128 => "Super Admin",
- ];
- const PAYMENT_METHOD_BANK_TRANSFER = 1;
- const PAYMENT_METHOD_PAYPAL = 2;
- const PAYMENT_METHODS = [
- 1 => "Bank Transfer",
- 2 => "PayPal",
- ];
- // Static methods
- /**
- * @return array
- */
- public static function titles(): array {
- return [
- "Miss" => MISS,
- "Mr" => MR,
- "Ms" => MS,
- "Dr" => DR
- ];
- }
- /**
- * @return array
- */
- public static function genders(): array {
- return [
- "Female" => FEMALE,
- "Male" => MALE,
- "Unspecified" => UNSPECIFIED
- ];
- }
- /**
- * @param string $password
- * @return bool
- */
- public static function validatePassword(string $password): bool {
- $lowercase = preg_match("/[a-z]/", $password);
- $uppercase = preg_match("/[A-Z]/", $password);
- $number = preg_match("/[0-9]/", $password);
- $special = preg_match("/[^0-9a-zA-Z]/", $password);
- $length = strlen($password) >= 6;
- return ($lowercase && $uppercase && $number && $special && $length);
- }
- // Object methods
- /**
- * @return bool
- */
- public function hasValidPaymentMethod(): bool {
- switch($this->preferred_payment_method) {
- case User::PAYMENT_METHOD_BANK_TRANSFER: {
- if(empty($this->bank_name)) return false;
- if(empty($this->bank_sort_code)) return false;
- if(empty($this->bank_account_number)) return false;
- break;
- }
- case User::PAYMENT_METHOD_PAYPAL: {
- if(empty($this->paypal_email)) return false;
- break;
- }
- }
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendActivationEmail() {
- $this->verify_token = bin2hex(random_bytes(16));
- $this->commit();
- // Content
- $name = htmlspecialchars($this->first_name);
- $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Thanks for registering!"));
- $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));
- $builder->addParagraph("<a href=\"$link\">$link</a>");
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress($this->email, $this->getDisplayName());
- $mail->setSubject(__("Verify your email address"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendWelcomeEmail() {
- // Content
- $name = htmlspecialchars($this->first_name);
- $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Thanks for verifying your email address!"));
- $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));
- $builder->addParagraph("<a href=\"$link\">$link</a>");
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress($this->email, $this->getDisplayName());
- $mail->setSubject(sprintf(__("Welcome to %s"), APP_NAME));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendIDPassEmail() {
- // Content
- $name = htmlspecialchars($this->first_name);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Your application has been successful!"));
- $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));
- $builder->addParagraph(__("When you're ready to try a Practice test, in your account and select the \"Practice\" option."));
- $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."));
- $builder->addParagraph(sprintf(__("2. Email us at %s and let us know that you're ready."), "contact@" . DOMAIN));
- $builder->addParagraph(__("3. We will allocate a test to you as soon as possible."));
- $builder->addParagraph(__("Remember to check your online account for when tests become available."));
- $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."));
- $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."));
- $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."));
- $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."));
- $builder->addParagraph(sprintf(__("Once again welcome to %s - Accept, Begin, Complete and get paid for up to 8 minutes work!"), APP_NAME));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress($this->email, $this->getDisplayName());
- $mail->setSubject(__("ID accepted and verified"));
- $mail->send();
- return true;
- }
- /**
- * @param string $reason
- * @return bool
- * @throws Exception
- */
- public function sendIDFailEmail($reason = "No reason given.") {
- // Content
- $name = htmlspecialchars($this->first_name);
- $reason = htmlspecialchars($reason);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Thank you for submitting your ID details."));
- $builder->addParagraph(__("Unfortunately we could not verify and accept your documents and/or image."));
- $builder->addParagraph(sprintf(__("The reason for this is: %s"), $reason));
- $builder->addParagraph(__("Please log in again, resubmit and we will try again."));
- $builder->addParagraph(__("We look forward to welcoming you onboard soon!"));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress($this->email, $this->getDisplayName());
- $mail->setSubject(__("We could not verify your ID details"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendPasswordResetEmail() {
- $this->reset_token = bin2hex(random_bytes(16));
- $this->commit();
- // Content
- $name = htmlspecialchars($this->first_name);
- $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("A reset password request has been received."));
- $builder->addParagraph(__("To reset your password, please tap on the following link:"));
- $builder->addParagraph("<a href=\"$link\">$link</a>");
- $builder->addParagraph(sprintf(__("If you %sdid not%s request for your password to be reset, please ignore this email."), '<strong>', '</strong>'));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress($this->email, $this->getDisplayName());
- $mail->setSubject(__("Password reset instructions"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendDocumentationEmail(): bool {
- // Content
- $name = htmlspecialchars($this->first_name);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Thanks for registering with us!"));
- $builder->addParagraph(__("The good news is that we have tests for you straight away."));
- $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."));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress($this->email, $this->getDisplayName());
- $mail->addAttachment("public/files/testing_procedure_arcades.pdf", "Testing Procedure - Arcades.pdf");
- $mail->addAttachment("public/files/testing_procedure_pubs.pdf", "Testing Procedure - Pubs.pdf");
- $mail->setSubject(__("Documentation"));
- return $mail->send();
- }
- /**
- * @return bool
- */
- public function canAdmin() {
- return $this->isAdmin();
- }
- /**
- * @return User|null
- * @throws Exception
- */
- public function getReferrer(): ?User {
- return is_null($this->referrer_id) ? null : User::get($this->referrer_id);
- }
- public function getDirectory() {
- $directory = ROOT_DIR . "/files/users/" . $this->id;
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- public function getLevelText() {
- return User::LEVELS[$this->level];
- }
- public function getLevelClass() {
- switch($this->level) {
- case User::LEVEL_BANNED: return "danger";
- case User::LEVEL_REGISTERED: return "secondary";
- case User::LEVEL_UNVERIFIED: return "warning";
- case User::LEVEL_DEACTIVATED: return "secondary";
- case User::LEVEL_TESTER: return "success";
- case User::LEVEL_VENUE: return "info";
- case User::LEVEL_CUSTOMER: return "info";
- case User::LEVEL_ADMIN: return "info";
- case User::LEVEL_SUPERADMIN: return "info";
- default:
- return "primary";
- }
- }
- public function isTesterManager(): bool {
- return $this->level >= User::LEVEL_TESTER_MANAGER;
- }
- public function isAdmin(): bool {
- return $this->level >= User::LEVEL_ADMIN;
- }
- public function isCustomer(): bool {
- return $this->level == User::LEVEL_CUSTOMER;
- }
- public function isTester(): bool {
- return $this->level == User::LEVEL_TESTER;
- }
- public function isDeactivated(): bool {
- return $this->level == User::LEVEL_DEACTIVATED;
- }
- public function getGenderText(): string {
- if($this->gender == "M") return MALE;
- if($this->gender == "F") return FEMALE;
- return "Unspecified";
- }
- /**
- * @return int|string
- * @throws Exception
- */
- public function getAge() {
- if(is_null($this->date_of_birth)) return __("N/A");
- $now = new DateTime("now");
- $dob = new DateTime($this->date_of_birth);
- return $dob->diff($now)->y;
- }
- public function getDisplayName() {
- if(empty($this->email)) return __("Deleted User");
- $parts = array_filter([$this->first_name, $this->last_name]);
- return implode(" ", $parts);
- }
- /**
- * @param $object
- * @return bool
- * @throws Exception
- */
- public function hasPermission($object): bool {
- if($this->canAccessLocations()) return true;
- $customers = $this->getCustomers();
- $customer_ids = array_column($customers, "id");
- if($object instanceof Customer) {
- return in_array($object->id, $customer_ids);
- }
- if($object instanceof Location) {
- return in_array($object->customer_id, $customer_ids);
- }
- if($object instanceof Project) {
- return in_array($object->customer_id, $customer_ids);
- }
- if($object instanceof Template) {
- return in_array($object->customer_id, $customer_ids);
- }
- return false;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function deactivate() {
- // Decline all pending tests
- $pending_tests = $this->getPendingTests();
- foreach($pending_tests as $test) {
- $test->decline();
- }
- // Set user level to Deactivated
- $this->level = User::LEVEL_DEACTIVATED;
- return $this->commit();
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function softDelete(): bool {
- // Delete identification documents
- $this->deleteIDFiles();
- // Delete addresses
- $this->deleteAddresses();
- // Decline all pending tests
- $pending_tests = $this->getPendingTests();
- foreach($pending_tests as $test) {
- $test->decline();
- }
- // Delete personal information
- $this->email = null;
- $this->title = null;
- $this->first_name = null;
- $this->last_name = null;
- $this->phone = null;
- $this->bank_name = null;
- $this->bank_sort_code = null;
- $this->bank_account_number = null;
- $this->paypal_email = null;
- // Set deleted time
- $this->dts_deleted = Util::getDateTime();
- return $this->commit();
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function deleteIDFiles(): bool {
- $files = UserFile::query([
- "user_id" => $this->id,
- "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
- ]);
- foreach($files as $file) {
- $path = $this->getDirectory() . "/" . $file->id . ".jpg";
- if(file_exists($path)) unlink($path);
- $file->delete();
- }
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function deleteAddresses(): bool {
- $addresses = $this->getAddresses();
- foreach($addresses as $address) {
- $address->delete();
- }
- return true;
- }
- /**
- * @return UserAddress[]
- * @throws Exception
- */
- public function getAddresses(): array {
- $addresses = UserAddress::query([
- "user_id" => $this->id,
- ], [
- "sort" => "name"
- ]);
- return $addresses;
- }
- /**
- * @return UserAddress|null
- * @throws Exception
- */
- public function getInvoiceAddress(): ?UserAddress {
- $addresses = UserAddress::query([
- "user_id" => $this->id,
- ]);
- foreach($addresses as $address) {
- if($address->invoice) return $address;
- }
- // If only one address in account, just return it
- if(count($addresses) == 1) return array_shift($addresses);
- return null;
- }
- /**
- * @return Customer[]
- * @throws Exception
- */
- public function getCustomers() {
- if($this->canAccessCustomers()) {
- return Customer::query([], ["sort" => "name"]);
- }
- if($this->isCustomer()) {
- $links = UserCustomer::query(["user_id" => $this->id]);
- return Customer::query([
- "id" => array_column($links, "customer_id")
- ], ["sort" => "name"]);
- }
- return [];
- }
- /**
- * @return Test[]
- * @throws Exception
- */
- public function getPendingTests(): array {
- return Test::query([
- "tester_id" => $this->id,
- "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_STARTED, Test::STATUS_PENDING_VERIFICATION],
- ], [
- "sort" => "dts_assigned",
- "order" => "desc"
- ]);
- }
- /**
- * @return Test[]
- * @throws Exception
- */
- public function getCompletedTests(): array {
- return Test::query([
- "tester_id" => $this->id,
- "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
- ], [
- "sort" => "dts_start",
- "order" => "desc"
- ]);
- }
- /**
- * @return User[]
- * @throws Exception
- */
- public function getReferrals(): array {
- return User::query([
- "referrer_id" => $this->id
- ]);
- }
- /**
- * @return string
- */
- public function getPreferredPaymentMethodText(): string {
- return User::PAYMENT_METHODS[$this->preferred_payment_method];
- }
- /**
- * @return string
- */
- public function getURL(): string {
- return "/account/users/{$this->id}";
- }
- public function isUnverified() {
- return $this->level == User::LEVEL_UNVERIFIED;
- }
- public function isRegistered() {
- return $this->level == User::LEVEL_REGISTERED;
- }
- public function isSuperAdmin(): bool {
- return $this->level == User::LEVEL_SUPERADMIN;
- }
- public function canAccessCustomers(): bool {
- return $this->level >= User::LEVEL_TESTER_MANAGER;
- }
- public function canAccessMapOverview(): bool {
- return $this->level >= User::LEVEL_TESTER_MANAGER;
- }
- public function canAccessUsers(): bool {
- return $this->level >= User::LEVEL_TESTER_MANAGER;
- }
- public function canAccessProjects(): bool {
- return $this->level >= User::LEVEL_TESTER_MANAGER;
- }
- public function canAccessTemplates(): bool {
- return $this->level >= User::LEVEL_ADMIN;
- }
- public function canAccessVenueTypes(): bool {
- return $this->level >= User::LEVEL_ADMIN;
- }
- public function canAccessInvoices(): bool {
- return $this->level >= User::LEVEL_ADMIN;
- }
- public function canAccessLog(): bool {
- return $this->level >= User::LEVEL_ADMIN;
- }
- public function canAccessLocations(): bool {
- return $this->level >= User::LEVEL_CUSTOMER;
- }
- public function canAccessReports(): bool {
- return $this->level >= User::LEVEL_CUSTOMER;
- }
- public function canAccessTestResults(): bool {
- return $this->level >= User::LEVEL_CUSTOMER;
- }
- public function canAccessTests(): bool {
- return $this->isTester() || $this->isTesterManager();
- }
- public function canAccessPractice(): bool {
- return $this->isTester() || $this->isAdmin() || $this->isUnverified() || $this->isRegistered();
- }
- public function canAccessAddresses(): bool {
- return $this->level == User::LEVEL_TESTER;
- }
- public function canAccessCompletedTests(): bool {
- return $this->level == User::LEVEL_TESTER;
- }
- public function canSubmitId(): bool {
- return $this->level == User::LEVEL_REGISTERED;
- }
- public function canDeactivate(): bool {
- return $this->level == User::LEVEL_TESTER;
- }
- public function canReactivate(): bool {
- return $this->level == User::LEVEL_DEACTIVATED;
- }
- public function canAccessAdminTestsList(): bool {
- return $this->isTesterManager();
- }
- public function canExportProjectCsv(): bool {
- return $this->isAdmin();
- }
- public function canVerifyTests(): bool {
- return $this->level >= User::LEVEL_TESTER_MANAGER;
- }
- public function canAccessTestResult(): bool {
- return $this->level >= User::LEVEL_VENUE;
- }
- public function canEditUsers(): bool {
- return $this->isTesterManager();
- }
- public function canSeeMissingData(): bool {
- return $this->level >= self::LEVEL_CUSTOMER;
- }
- }
|