summaryrefslogtreecommitdiff
path: root/frontend/Author.php
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/Author.php')
-rw-r--r--frontend/Author.php45
1 files changed, 45 insertions, 0 deletions
diff --git a/frontend/Author.php b/frontend/Author.php
new file mode 100644
index 0000000..669e733
--- /dev/null
+++ b/frontend/Author.php
@@ -0,0 +1,45 @@
+<?php
+class Author extends Model {
+ public static
+ $table = 'author',
+ $fillable_columns = ['name', 'email', 'password'];
+
+ public static function randomPass() {
+ return preg_replace('/[^\w]/', '',
+ base64_encode(bin2hex(openssl_random_pseudo_bytes(4))));
+ }
+
+ public function getPackageIds() {
+ return Package::searchIds($this->pdo, ['`author_id`=?'], [$this->id]);
+ }
+
+ public function getPackages() {
+ return Package::search($this->pdo, ['`author_id`=?'], [$this->id]);
+ }
+
+ public static function hash($password) {
+ return password_hash($password, PASSWORD_DEFAULT, ['cost' => 10]);
+ }
+
+ protected static function mutator($key, $value) {
+ switch ($key) {
+ case 'password':
+ return self::hash($value);
+ default:
+ return parent::mutator($key, $value);
+ }
+ }
+
+ public function isAdmin() {
+ return in_array($this->id, Constants::user_admins);
+ }
+
+ public function verifyPassword($password) {
+ if (!password_verify($password, $this->password))
+ return false;
+ if (password_needs_rehash(
+ $this->password, PASSWORD_DEFAULT, ['cost' => 10]))
+ $this->password = $password;
+ return true;
+ }
+}