summaryrefslogtreecommitdiff
path: root/frontend/Author.php
blob: 669e733cd2b00aa0b15d8d47ce230cc611500406 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
	}
}