| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- 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", "phone", "verify_token", "reset_token", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
- // Constants
- const TITLES = ["Miss", "Mr", "Ms", "Dr"];
- const GENDERS = ["Female", "Male", "Unspecified"];
- 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_ADMIN = 64;
- const LEVEL_SUPERADMIN = 128;
- const LEVELS = [
- 0 => "Banned",
- 1 => "Registered",
- 2 => "Unverified",
- 4 => "Deactivated",
- 8 => "Tester",
- 16 => "Venue",
- 32 => "Customer",
- 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("<b>Hi $name</b>");
- $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("<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;
- }
- public function sendWelcomeEmail() {
- // Content
- $name = htmlspecialchars($this->first_name);
- $link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>Hi $name</b>");
- $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("<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("Welcome to Check Policy");
- $mail->send();
- return true;
- }
- public function sendIDPassEmail() {
- // Content
- $name = htmlspecialchars($this->first_name);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>Hi $name</b>");
- $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("<b>Hi $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("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("<b>Hi $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("If you <strong>did not</strong> 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];
- }
- }
- ?>
|