1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Identity; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Foundation\Security\Role; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* User Session. |
19
|
|
|
*/ |
20
|
|
|
class UserSession extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Refresh current session if necessary. |
24
|
|
|
* |
25
|
|
|
* @param int $user_id |
26
|
|
|
*/ |
27
|
|
|
public function refresh($user_id) |
28
|
|
|
{ |
29
|
|
|
if ($this->getId() == $user_id) { |
30
|
|
|
$this->initialize($this->userModel->getById($user_id)); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Update user session. |
36
|
|
|
* |
37
|
|
|
* @param array $user |
38
|
|
|
*/ |
39
|
|
|
public function initialize(array $user) |
40
|
|
|
{ |
41
|
|
|
foreach (['password', 'is_admin', 'is_project_admin', 'twofactor_secret'] as $column) { |
42
|
|
|
if (isset($user[$column])) { |
43
|
|
|
unset($user[$column]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$user['id'] = (int) $user['id']; |
48
|
|
|
$user['is_ldap_user'] = isset($user['is_ldap_user']) ? (bool) $user['is_ldap_user'] : false; |
49
|
|
|
$user['twofactor_activated'] = isset($user['twofactor_activated']) ? (bool) $user['twofactor_activated'] : false; |
50
|
|
|
|
51
|
|
|
$this->sessionStorage->user = $user; |
|
|
|
|
52
|
|
|
$this->sessionStorage->postAuthenticationValidated = false; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get user properties. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getAll() |
61
|
|
|
{ |
62
|
|
|
return $this->sessionStorage->user; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get user application role. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getRole() |
71
|
|
|
{ |
72
|
|
|
return $this->sessionStorage->user['role']; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return true if the user has validated the 2FA key. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isPostAuthenticationValidated() |
81
|
|
|
{ |
82
|
|
|
return isset($this->sessionStorage->postAuthenticationValidated) && $this->sessionStorage->postAuthenticationValidated === true; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Validate 2FA for the current session. |
87
|
|
|
*/ |
88
|
|
|
public function validatePostAuthentication() |
89
|
|
|
{ |
90
|
|
|
$this->sessionStorage->postAuthenticationValidated = true; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return true if the user has 2FA enabled. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function hasPostAuthentication() |
99
|
|
|
{ |
100
|
|
|
return isset($this->sessionStorage->user['twofactor_activated']) && $this->sessionStorage->user['twofactor_activated'] === true; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Disable 2FA for the current session. |
105
|
|
|
*/ |
106
|
|
|
public function disablePostAuthentication() |
107
|
|
|
{ |
108
|
|
|
$this->sessionStorage->user['twofactor_activated'] = false; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return true if the logged user is admin. |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isAdmin() |
117
|
|
|
{ |
118
|
|
|
return isset($this->sessionStorage->user['role']) && $this->sessionStorage->user['role'] === Role::APP_ADMIN; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the connected user id. |
123
|
|
|
* |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
public function getId() |
127
|
|
|
{ |
128
|
|
|
return isset($this->sessionStorage->user['id']) ? (int) $this->sessionStorage->user['id'] : 0; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get username. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getUsername() |
137
|
|
|
{ |
138
|
|
|
return isset($this->sessionStorage->user['username']) ? $this->sessionStorage->user['username'] : ''; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check is the user is connected. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isLogged() |
147
|
|
|
{ |
148
|
|
|
return isset($this->sessionStorage->user) && !empty($this->sessionStorage->user); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get project filters from the session. |
153
|
|
|
* |
154
|
|
|
* @param int $project_id |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getFilters($project_id) |
159
|
|
|
{ |
160
|
|
|
return !empty($this->sessionStorage->filters[$project_id]) ? $this->sessionStorage->filters[$project_id] : 'status:open'; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Save project filters in the session. |
165
|
|
|
* |
166
|
|
|
* @param int $project_id |
167
|
|
|
* @param string $filters |
168
|
|
|
*/ |
169
|
|
|
public function setFilters($project_id, $filters) |
170
|
|
|
{ |
171
|
|
|
$this->sessionStorage->filters[$project_id] = $filters; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get recent projects from the session. |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function getRecentProjects() |
180
|
|
|
{ |
181
|
|
|
return isset($this->sessionStorage->recentProjectIds) && !empty($this->sessionStorage->recentProjectIds) ? $this->sessionStorage->recentProjectIds : []; |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Save recent project in the session. |
186
|
|
|
* |
187
|
|
|
* @param int $project_id |
188
|
|
|
* @param string $filters |
|
|
|
|
189
|
|
|
*/ |
190
|
|
|
public function setRecentProject($project_id) |
191
|
|
|
{ |
192
|
|
|
if (!isset($this->sessionStorage->recentProjectIds)) { |
|
|
|
|
193
|
|
|
$this->sessionStorage->recentProjectIds = []; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
array_unshift($this->sessionStorage->recentProjectIds, $project_id); |
|
|
|
|
197
|
|
|
|
198
|
|
|
$this->sessionStorage->recentProjectIds = array_slice(array_unique($this->sessionStorage->recentProjectIds), 0, 5); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.