"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
public static function validatePassword($password) {
$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
public function hasValidPaymentMethod() {
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;
}
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("Hi $name");
$builder->addLeadParagraph("Thanks for registering!");
$builder->addParagraph("Welcome to Check Policy! Before you can log in, you will need to verify your email address. You can do so by tapping the following link:");
$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;
}
public function sendWelcomeEmail() {
// Content
$name = htmlspecialchars($this->first_name);
$link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
// Build
$builder = new EmailBuilder;
$builder->addParagraph("Hi $name");
$builder->addLeadParagraph("Thanks for verifying your email address!");
$builder->addParagraph("Welcome to Check Policy! 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:");
$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("Welcome to Check Policy");
$mail->send();
return true;
}
public function sendIDPassEmail() {
// Content
$name = htmlspecialchars($this->first_name);
// Build
$builder = new EmailBuilder;
$builder->addParagraph("Hi $name");
$builder->addLeadParagraph("Your application has been successful!");
$builder->addParagraph("Welcome to Check Policy! 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.");
$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.");
$builder->addParagraph("After you click Accept, you will be given the option to Begin the test whenever you want. Just load up your account and click Begin.");
$builder->addParagraph("Once again welcome to Check Policy - Accept, Begin, Complete and get paid for up to 8 minutes work!");
$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;
}
public function sendIDFailEmail($reason = "No reason given.") {
// Content
$name = htmlspecialchars($this->first_name);
$reason = htmlspecialchars($reason);
// Build
$builder = new EmailBuilder;
$builder->addParagraph("Hi $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("The reason for this is: $reason");
$builder->addParagraph("Please log in again, resubmit and we will try again.");
$builder->addParagraph("We look forward to welcoming you on-board 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;
}
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("Hi $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("If you did not 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;
}
public function canAdmin() {
return $this->level >= User::LEVEL_ADMIN;
}
public function getReferrer() {
return is_null($this->referrer_id) ? false : 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 getGenderText() {
if($this->gender == "M") return "Male";
if($this->gender == "F") return "Female";
return "Unspecified";
}
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() {
$parts = array_filter([$this->first_name, $this->last_name]);
return implode(" ", $parts);
}
public function hasPermission($object) {
if($this->level == User::LEVEL_SUPERADMIN) return true;
if($this->level == User::LEVEL_ADMIN) return true;
$customers = $this->getCustomers();
$customer_ids = array_column($customers, "id");
if(is_a($object, "Customer")) {
return in_array($object->id, $customer_ids);
}
if(is_a($object, "Location")) {
return in_array($object->customer_id, $customer_ids);
}
if(is_a($object, "Project")) {
return in_array($object->customer_id, $customer_ids);
}
if(is_a($object, "Template")) {
return in_array($object->customer_id, $customer_ids);
}
return false;
}
public function deleteIDFiles() {
$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;
}
public function getAddresses() {
$addresses = UserAddress::query([
"user_id" => $this->id,
], [
"sort" => "name"
]);
return $addresses;
}
public function getInvoiceAddress() {
$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 false;
}
public function getCustomers() {
if($this->level >= User::LEVEL_ADMIN) {
return Customer::query([], ["sort" => "name"]);
}
if($this->level == User::LEVEL_CUSTOMER) {
$links = UserCustomer::query(["user_id" => $this->id]);
return Customer::query([
"id" => array_column($links, "customer_id")
], ["sort" => "name"]);
}
return [];
}
public function getPendingTests() {
return Test::query([
"tester_id" => $this->id,
"status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED, Test::STATUS_PENDING_VERIFICATION],
], [
"sort" => "id",
"order" => "desc"
]);
}
public function getCompletedTests() {
return Test::query([
"tester_id" => $this->id,
"status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED],
], [
"sort" => "id",
"order" => "desc"
]);
}
public function getReferrals() {
return User::query([
"referrer_id" => $this->id
]);
}
public function getPreferredPaymentMethodText() {
return User::PAYMENT_METHODS[$this->preferred_payment_method];
}
}
?>