"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("" . sprintf(__("Hi %s"), $name) . "");
$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("$link");
$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("" . sprintf(__("Hi %s"), $name) . "");
$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("$link");
$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("" . sprintf(__("Hi %s"), $name) . "");
$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("" . sprintf(__("Hi %s"), $name) . "");
$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("" . sprintf(__("Hi %s"), $name) . "");
$builder->addLeadParagraph(__("A reset password request has been received."));
$builder->addParagraph(__("To reset your password, please tap on the following link:"));
$builder->addParagraph("$link");
$builder->addParagraph(sprintf(__("If you %sdid not%s request for your password to be reset, please ignore this email."), '', ''));
$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("" . sprintf(__("Hi %s"), $name) . "");
$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();
}
}