|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Authentication; |
|
13
|
|
|
|
|
14
|
|
|
use Linna\DataMapper\DomainObjectAbstract; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provide a basic user for authentication system. |
|
18
|
|
|
* This class is thinked to be used with PHP PDO, |
|
19
|
|
|
* but should be instantiated like a normal class. |
|
20
|
|
|
* |
|
21
|
|
|
* <pre><code class="php">$password = new Password(); |
|
22
|
|
|
* |
|
23
|
|
|
* $userId = 1; |
|
24
|
|
|
* |
|
25
|
|
|
* $pdos = $pdo->prepare('SELECT user_id AS objectId, |
|
26
|
|
|
* uuid, |
|
27
|
|
|
* name, |
|
28
|
|
|
* email, |
|
29
|
|
|
* description, |
|
30
|
|
|
* password, |
|
31
|
|
|
* active, |
|
32
|
|
|
* created, |
|
33
|
|
|
* last_update AS lastUpdate |
|
34
|
|
|
* FROM user WHERE user_id = :id'); |
|
35
|
|
|
* |
|
36
|
|
|
* $pdos->bindParam(':id', $userId, \PDO::PARAM_INT); |
|
37
|
|
|
* $pdos->execute(); |
|
38
|
|
|
* |
|
39
|
|
|
* //pdo return an User class instance |
|
40
|
|
|
* $user = $pdos->fetchObject('\Linna\Auth\User', [$password]); |
|
41
|
|
|
* |
|
42
|
|
|
* //var_dump result |
|
43
|
|
|
* //root |
|
44
|
|
|
* var_dump($user->name); |
|
45
|
|
|
* </code></pre> |
|
46
|
|
|
*/ |
|
47
|
|
|
class User extends DomainObjectAbstract |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var string Universal unique identifier. |
|
51
|
|
|
*/ |
|
52
|
|
|
public string $uuid = ''; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string User name. |
|
56
|
|
|
*/ |
|
57
|
|
|
public string $name = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string User description. |
|
61
|
|
|
*/ |
|
62
|
|
|
public ?string $description = ''; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string User e-mail. |
|
66
|
|
|
*/ |
|
67
|
|
|
public ?string $email = ''; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string User hashed password. |
|
71
|
|
|
*/ |
|
72
|
|
|
public string $password = ''; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var int It say if user is active or not. |
|
76
|
|
|
*/ |
|
77
|
|
|
public int $active = 0; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var Password Password class for manage password. |
|
81
|
|
|
*/ |
|
82
|
|
|
private Password $passwordUtility; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Class Constructor. |
|
86
|
|
|
* |
|
87
|
|
|
* <pre><code class="php">$password = new Password(); |
|
88
|
|
|
* |
|
89
|
|
|
* $user = new User($password); |
|
90
|
|
|
* </code></pre> |
|
91
|
|
|
* |
|
92
|
|
|
* @param Password $password Password class instance. |
|
93
|
|
|
*/ |
|
94
|
223 |
|
public function __construct(Password $password) |
|
95
|
|
|
{ |
|
96
|
223 |
|
$this->passwordUtility = $password; |
|
97
|
|
|
|
|
98
|
|
|
//set required type |
|
99
|
|
|
//do type conversion because PDO doesn't return any original type from db :(. |
|
100
|
223 |
|
\settype($this->id, 'integer'); |
|
101
|
223 |
|
\settype($this->active, 'integer'); |
|
102
|
223 |
|
\settype($this->email, 'string'); |
|
103
|
223 |
|
\settype($this->description, 'string'); |
|
104
|
223 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set new user password without do any check. |
|
108
|
|
|
* |
|
109
|
|
|
* <pre><code class="php">$user->setPassword('newPassword');</code></pre> |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $newPassword User new password. |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function setPassword(string $newPassword): void |
|
116
|
|
|
{ |
|
117
|
|
|
//hash provided password |
|
118
|
2 |
|
$this->password = $this->passwordUtility->hash($newPassword); |
|
119
|
2 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Change user password only after check old password. |
|
123
|
|
|
* |
|
124
|
|
|
* <pre><code class="php">$user->changePassword('newPassword', 'oldPassword');</code></pre> |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $newPassword User new password. |
|
127
|
|
|
* @param string $oldPassword User old password. |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function changePassword(string $newPassword, string $oldPassword): bool |
|
132
|
|
|
{ |
|
133
|
|
|
//verfy password match |
|
134
|
1 |
|
if ($this->passwordUtility->verify($oldPassword, $this->password)) { |
|
135
|
|
|
|
|
136
|
|
|
//if match set new password |
|
137
|
1 |
|
$this->password = $this->passwordUtility->hash($newPassword); |
|
138
|
|
|
|
|
139
|
1 |
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|