aboutsummaryrefslogtreecommitdiff
path: root/classes/User.php
blob: 525a15376cb578ee6583398a5aae6eb4c8e49733 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * Provides the user class, an interface to the user table in the database
 *
 * @author Camil Staps
 *
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * An interface to the user table in the database
 */
class User extends Model {
	/** {@inheritDoc} */
	public static
		$table = 'user',
		$fillable_columns = ['username', 'password'];

	/**
	 * Generate a random password
	 *
	 * @return string The password
	 */
	public static function generateRandomPassword() {
		return preg_replace('/[^\w]/', '',
			base64_encode(bin2hex(openssl_random_pseudo_bytes(4))));
	}

	/**
	 * Hash a password
	 *
	 * @param string $password          The password to be hashed
	 * @param int $cost                 The password cost
	 *
	 * @return string The hashed password
	 */
	public static function hash($password, $cost=null) {
		return password_hash(
			$password,
			Constants::password_algo,
			['cost' => is_null($cost) ? Constants::password_cost : $cost]
		);
	}

	/**
	 * {@inheritDoc}
	 * @param $key {@inheritDoc}
	 * @param $value {@inheritDoc}
	 */
	protected static function mutator($key, $value) {
		switch ($key) {
			case 'password':
				return self::hash($password);
				break;
			default:
				return parent::mutator($key, $value);
		}
	}

	/**
	 * Check if a user has administrator rights
	 *
	 * @return bool                     True iff the user has administrator rights
	 */
	public function isAdmin() {
		return in_array($this->id, Constants::user_admins);
	}

	/**
	 * Verify a password
	 *
	 * @param string $password          The password to verify
	 *
	 * @return bool                     True iff the password can be accepted
	 */
	public function verifyPassword($password) {
		if (!password_verify($password, $this->password)) {
			return false;
		}
		if (password_needs_rehash($this->password, Constants::password_algo,
				['cost' => Constants::password_cost])) {
			$this->password = $password;
		}
		return true;
	}
}