"Banned", 1 => "Registered", 2 => "Unverified", 4 => "Deactivated", 8 => "Tester", 16 => "Venue", 32 => "Customer", 64 => "Admin", 128 => "Super Admin", ]; 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 sendActivationEmail() { $this->verify_token = bin2hex(random_bytes(16)); $this->commit(); $mail = new Email(); $mail->loadHtmlTemplate("registration"); $mail->setValues([ "name" => $this->first_name, "link" => PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token ]); $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() { $mail = new Email(); $mail->loadHtmlTemplate("welcome"); $mail->setValues([ "name" => $this->first_name, "link" => PROTOCOL . "://" . DOMAIN . "/account/submit-id" ]); $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 sendPasswordResetEmail() { $this->reset_token = bin2hex(random_bytes(16)); $this->commit(); $mail = new Email(); $mail->loadHtmlTemplate("password_reset"); $mail->setValues([ "name" => $this->first_name, "link" => PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token ]); $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 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 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], ], [ "sort" => "id", ]); } public function getCompletedTests() { return Test::query([ "tester_id" => $this->id, "status" => [Test::STATUS_CANCELLED, Test::STATUS_COMPLETED], ], [ "sort" => "id", ]); } public function getPreferredPaymentMethodText() { return User::PAYMENT_METHODS[$this->preferred_payment_method]; } } ?>