|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Core\Context; |
|
26
|
|
|
|
|
27
|
|
|
use SP\Config\ConfigData; |
|
28
|
|
|
use SP\DataModel\ProfileData; |
|
29
|
|
|
use SP\Services\User\UserLoginResponse; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class ApiContext |
|
33
|
|
|
* |
|
34
|
|
|
* @package SP\Core\Context |
|
35
|
|
|
*/ |
|
36
|
|
|
final class StatelessContext extends ContextBase |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Establecer la configuración |
|
40
|
|
|
* |
|
41
|
|
|
* @param ConfigData $config |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setConfig(ConfigData $config) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setContextKey('config', $config); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Establecer una variable de sesión |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key El nombre de la variable |
|
52
|
|
|
* @param mixed $value El valor de la variable |
|
53
|
|
|
* |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function setContextKey(string $key, $value) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
|
|
parent::setContextKey($key, $value); |
|
60
|
|
|
|
|
61
|
|
|
return $value; |
|
62
|
|
|
} catch (ContextException $e) { |
|
63
|
|
|
processException($e); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Establece los datos del usuario en la sesión. |
|
71
|
|
|
* |
|
72
|
|
|
* @param UserLoginResponse $userLoginResponse |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setUserData(UserLoginResponse $userLoginResponse = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->setContextKey('userData', $userLoginResponse); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Obtiene el objeto de perfil de usuario de la sesión. |
|
81
|
|
|
* |
|
82
|
|
|
* @return ProfileData |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getUserProfile() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->getContextKey('userProfile'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Devolver una variable de sesión |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $key |
|
93
|
|
|
* @param mixed $default |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getContextKey(string $key, $default = null) |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
|
|
return parent::getContextKey($key, $default); |
|
101
|
|
|
} catch (ContextException $e) { |
|
102
|
|
|
processException($e); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $default; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Establece el objeto de perfil de usuario en la sesión. |
|
110
|
|
|
* |
|
111
|
|
|
* @param ProfileData $ProfileData |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setUserProfile(ProfileData $ProfileData) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->setContextKey('userProfile', $ProfileData); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns if user is logged in |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function isLoggedIn() |
|
124
|
|
|
{ |
|
125
|
|
|
return !empty($this->getUserData()->getLogin()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Devuelve los datos del usuario en la sesión. |
|
130
|
|
|
* |
|
131
|
|
|
* @return UserLoginResponse |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getUserData() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->getContextKey('userData', new UserLoginResponse()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getSecurityKey() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->getContextKey('sk'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function generateSecurityKey() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->setSecurityKey(sha1(time() . $this->getConfig()->getPasswordSalt())); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param $sk |
|
156
|
|
|
* |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setSecurityKey($sk) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->setContextKey('sk', $sk); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Devolver la configuración |
|
166
|
|
|
* |
|
167
|
|
|
* @return ConfigData |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getConfig() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->getContextKey('config'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Establecer el lenguaje de la sesión |
|
176
|
|
|
* |
|
177
|
|
|
* @param $locale |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setLocale($locale) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->setContextKey('locale', $locale); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Devuelve el lenguaje de la sesión |
|
186
|
|
|
* |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getLocale() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->getContextKey('locale'); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Devuelve el estado de la aplicación |
|
196
|
|
|
* |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getAppStatus() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->getContextKey('status'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Establecer el estado de la aplicación |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $status |
|
208
|
|
|
*/ |
|
209
|
|
|
public function setAppStatus($status) |
|
210
|
|
|
{ |
|
211
|
|
|
$this->setContextKey('status', $status); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Reset del estado de la aplicación |
|
216
|
|
|
* |
|
217
|
|
|
* @return bool |
|
218
|
|
|
*/ |
|
219
|
|
|
public function resetAppStatus() |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->setContextKey('status', null); |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return void |
|
226
|
|
|
* @throws ContextException |
|
227
|
|
|
*/ |
|
228
|
|
|
public function initialize() |
|
229
|
|
|
{ |
|
230
|
|
|
$this->setContext(new ContextCollection()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Establecer la hora de carga de la configuración |
|
235
|
|
|
* |
|
236
|
|
|
* @param int $time |
|
237
|
|
|
*/ |
|
238
|
|
|
public function setConfigTime($time) |
|
239
|
|
|
{ |
|
240
|
|
|
$this->setContextKey('configTime', (int)$time); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Devolver la hora de carga de la configuración |
|
245
|
|
|
* |
|
246
|
|
|
* @return int |
|
247
|
|
|
*/ |
|
248
|
|
|
public function getConfigTime() |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->getContextKey('configTime'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return null |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getAccountsCache() |
|
257
|
|
|
{ |
|
258
|
|
|
return null; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Sets a temporary master password |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $password |
|
265
|
|
|
* |
|
266
|
|
|
* @throws ContextException |
|
267
|
|
|
*/ |
|
268
|
|
|
public function setTemporaryMasterPass(string $password) |
|
269
|
|
|
{ |
|
270
|
|
|
$this->setTrasientKey('_tempmasterpass', $password); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @param string $pluginName |
|
275
|
|
|
* @param string $key |
|
276
|
|
|
* @param mixed $value |
|
277
|
|
|
* |
|
278
|
|
|
* @return mixed |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setPluginKey(string $pluginName, string $key, $value) |
|
281
|
|
|
{ |
|
282
|
|
|
$ctxKey = $this->getContextKey('plugins'); |
|
283
|
|
|
|
|
284
|
|
|
$ctxKey[$pluginName][$key] = $value; |
|
285
|
|
|
|
|
286
|
|
|
return $value; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param string $pluginName |
|
291
|
|
|
* @param string $key |
|
292
|
|
|
* |
|
293
|
|
|
* @return mixed |
|
294
|
|
|
*/ |
|
295
|
|
|
public function getPluginKey(string $pluginName, string $key) |
|
296
|
|
|
{ |
|
297
|
|
|
$ctxKey = $this->getContextKey('plugins'); |
|
298
|
|
|
|
|
299
|
|
|
if (isset($ctxKey[$pluginName][$key])) { |
|
300
|
|
|
return $ctxKey[$pluginName][$key]; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
return null; |
|
304
|
|
|
} |
|
305
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.