1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Authentication; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @todo it's too project specific... |
|
|
|
|
9
|
|
|
* @api |
10
|
|
|
*/ |
11
|
|
|
class UserVO implements JsonSerializable |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const PROPERTY_AVATAR = 'avatar'; |
15
|
|
|
const PROPERTY_EMAIL = 'email'; |
16
|
|
|
|
17
|
|
|
const ROLE_ADMIN = 'admin'; |
18
|
|
|
const ROLE_USER = 'user'; |
19
|
|
|
|
20
|
|
|
const AVATAR_1 = 'avatar1.png'; |
21
|
|
|
const AVATAR_2 = 'avatar2.png'; |
22
|
|
|
const AVATAR_3 = 'avatar3.png'; |
23
|
|
|
const AVATAR_4 = 'avatar4.png'; |
24
|
|
|
const AVATAR_5 = 'avatar5.png'; |
25
|
|
|
|
26
|
|
|
const DEFAULT_AVATAR = self::AVATAR_5; |
27
|
|
|
|
28
|
|
|
const ROLES = [ |
29
|
|
|
self::ROLE_ADMIN, |
30
|
|
|
self::ROLE_USER |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
const ROLE_MAP = [ |
34
|
|
|
self::ROLE_USER => [self::ROLE_ADMIN] |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
const AVATARS = [ |
38
|
|
|
self::AVATAR_1, |
39
|
|
|
self::AVATAR_2, |
40
|
|
|
self::AVATAR_3, |
41
|
|
|
self::AVATAR_4, |
42
|
|
|
self::AVATAR_5, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
public $id = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $username; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public $password_hash; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $one_time_secret; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @todo needed in VO? |
|
|
|
|
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
public $password; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
public $email; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string[] |
78
|
|
|
*/ |
79
|
|
|
public $roles = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
public $avatar; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
1 |
|
public function getId() : int |
90
|
|
|
{ |
91
|
1 |
|
return $this->id; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $role |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
3 |
|
public function hasRole(string $role) : bool |
99
|
|
|
{ |
100
|
3 |
|
if (in_array($role, $this->roles)) { |
101
|
3 |
|
return true; |
102
|
|
|
}; |
103
|
|
|
|
104
|
3 |
|
if (!empty(self::ROLE_MAP[$role])) { |
105
|
1 |
|
foreach (self::ROLE_MAP[$role] as $slaveRole) { |
106
|
1 |
|
if ($this->hasRole($slaveRole)) { |
107
|
1 |
|
return true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string[] |
117
|
|
|
*/ |
118
|
1 |
|
public function getRoles() : array |
119
|
|
|
{ |
120
|
1 |
|
return $this->roles; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
1 |
|
public function getPassword() :string |
127
|
|
|
{ |
128
|
1 |
|
return $this->password_hash; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
1 |
|
public function getSalt() |
135
|
|
|
{ |
136
|
1 |
|
return $this->username; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
1 |
|
public function getUsername() |
143
|
|
|
{ |
144
|
1 |
|
return $this->username; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
public function jsonSerialize() |
151
|
|
|
{ |
152
|
1 |
|
return $this->toArray(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
1 |
|
public function toArray() |
159
|
|
|
{ |
160
|
|
|
return [ |
161
|
1 |
|
'userId' => $this->id, |
162
|
1 |
|
'username' => $this->username, |
163
|
1 |
|
'roles' => $this->roles, |
164
|
1 |
|
'email' => $this->email, |
165
|
1 |
|
'avatar' => $this->avatar, |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.