|
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\Modules\Web\Controllers; |
|
26
|
|
|
|
|
27
|
|
|
defined('APP_ROOT') || die(); |
|
28
|
|
|
|
|
29
|
|
|
use DI\Container; |
|
30
|
|
|
use Psr\Container\ContainerInterface; |
|
31
|
|
|
use SP\Core\Crypt\Hash; |
|
32
|
|
|
use SP\Core\Exceptions\FileNotFoundException; |
|
33
|
|
|
use SP\Core\Exceptions\SessionTimeout; |
|
34
|
|
|
use SP\Core\Exceptions\SPException; |
|
35
|
|
|
use SP\DataModel\ProfileData; |
|
36
|
|
|
use SP\Modules\Web\Controllers\Helpers\LayoutHelper; |
|
37
|
|
|
use SP\Modules\Web\Controllers\Traits\WebControllerTrait; |
|
38
|
|
|
use SP\Mvc\View\Template; |
|
39
|
|
|
use SP\Providers\Auth\Browser\Browser; |
|
40
|
|
|
use SP\Services\Auth\AuthException; |
|
41
|
|
|
use SP\Services\User\UserLoginResponse; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Clase base para los controladores |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract class ControllerBase |
|
47
|
|
|
{ |
|
48
|
|
|
use WebControllerTrait; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constantes de errores |
|
52
|
|
|
*/ |
|
53
|
|
|
const ERR_UNAVAILABLE = 0; |
|
54
|
|
|
const ERR_ACCOUNT_NO_PERMISSION = 1; |
|
55
|
|
|
const ERR_PAGE_NO_PERMISSION = 2; |
|
56
|
|
|
const ERR_UPDATE_MPASS = 3; |
|
57
|
|
|
const ERR_OPERATION_NO_PERMISSION = 4; |
|
58
|
|
|
const ERR_EXCEPTION = 5; |
|
59
|
|
|
/** |
|
60
|
|
|
* @var Template Instancia del motor de plantillas a utilizar |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $view; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var UserLoginResponse |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $userData; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var ProfileData |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $userProfileData; |
|
71
|
|
|
/** |
|
72
|
|
|
* @var ContainerInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $dic; |
|
75
|
|
|
/** |
|
76
|
|
|
* @var bool |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $isAjax = false; |
|
79
|
|
|
/** |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $previousSk; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Constructor |
|
86
|
|
|
* |
|
87
|
|
|
* @param Container $container |
|
88
|
|
|
* @param $actionName |
|
89
|
|
|
* |
|
90
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
|
91
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
|
92
|
|
|
* @throws SessionTimeout |
|
93
|
|
|
*/ |
|
94
|
|
|
public final function __construct(Container $container, $actionName) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->dic = $container; |
|
97
|
|
|
$this->actionName = $actionName; |
|
98
|
|
|
|
|
99
|
|
|
$this->setUp($container); |
|
100
|
|
|
|
|
101
|
|
|
$this->view = $this->dic->get(Template::class); |
|
102
|
|
|
$this->view->setBase(strtolower($this->controllerName)); |
|
103
|
|
|
|
|
104
|
|
|
$this->isAjax = $this->request->isAjax(); |
|
105
|
|
|
$this->previousSk = $this->session->getSecurityKey(); |
|
106
|
|
|
|
|
107
|
|
|
$loggedIn = $this->session->isLoggedIn(); |
|
108
|
|
|
|
|
109
|
|
|
if ($loggedIn) { |
|
110
|
|
|
$this->userData = clone $this->session->getUserData(); |
|
111
|
|
|
$this->userProfileData = clone $this->session->getUserProfile(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->setViewVars($loggedIn); |
|
115
|
|
|
|
|
116
|
|
|
try { |
|
117
|
|
|
$this->initialize(); |
|
118
|
|
|
} catch (SessionTimeout $sessionTimeout) { |
|
119
|
|
|
$this->handleSessionTimeout(); |
|
120
|
|
|
|
|
121
|
|
|
throw $sessionTimeout; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set view variables |
|
127
|
|
|
* |
|
128
|
|
|
* @param bool $loggedIn |
|
129
|
|
|
*/ |
|
130
|
|
|
private function setViewVars($loggedIn = false) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->view->assign('timeStart', $this->request->getServer('REQUEST_TIME_FLOAT')); |
|
133
|
|
|
$this->view->assign('queryTimeStart', microtime()); |
|
134
|
|
|
|
|
135
|
|
|
if ($loggedIn) { |
|
136
|
|
|
$this->view->assign('ctx_userId', $this->userData->getId()); |
|
137
|
|
|
$this->view->assign('ctx_userGroupId', $this->userData->getUserGroupId()); |
|
138
|
|
|
$this->view->assign('ctx_userIsAdminApp', $this->userData->getIsAdminApp()); |
|
139
|
|
|
$this->view->assign('ctx_userIsAdminAcc', $this->userData->getIsAdminAcc()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->view->assign('isDemo', $this->configData->isDemoEnabled()); |
|
143
|
|
|
$this->view->assign('themeUri', $this->view->getTheme()->getThemeUri()); |
|
144
|
|
|
$this->view->assign('configData', $this->configData); |
|
145
|
|
|
$this->view->assign('sk', $loggedIn ? $this->session->generateSecurityKey($this->configData->getPasswordSalt()) : ''); |
|
146
|
|
|
|
|
147
|
|
|
// Pass the action name to the template as a variable |
|
148
|
|
|
$this->view->assign($this->actionName, true); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
protected abstract function initialize(); |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
|
|
private function handleSessionTimeout() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->sessionLogout( |
|
162
|
|
|
$this->request, |
|
163
|
|
|
$this->configData, |
|
164
|
|
|
function ($redirect) { |
|
165
|
|
|
$this->router->response() |
|
166
|
|
|
->redirect($redirect) |
|
167
|
|
|
->send(true); |
|
168
|
|
|
} |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Mostrar los datos de la plantilla |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function view() |
|
176
|
|
|
{ |
|
177
|
|
|
try { |
|
178
|
|
|
$this->router->response() |
|
179
|
|
|
->body($this->view->render()) |
|
180
|
|
|
->send(); |
|
181
|
|
|
} catch (FileNotFoundException $e) { |
|
182
|
|
|
processException($e); |
|
183
|
|
|
|
|
184
|
|
|
$this->router->response() |
|
185
|
|
|
->body(__($e->getMessage())) |
|
186
|
|
|
->send(true); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Renderizar los datos de la plantilla y devolverlos |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function render() |
|
194
|
|
|
{ |
|
195
|
|
|
try { |
|
196
|
|
|
return $this->view->render(); |
|
197
|
|
|
} catch (FileNotFoundException $e) { |
|
198
|
|
|
processException($e); |
|
199
|
|
|
|
|
200
|
|
|
return $e->getMessage(); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Upgrades a View to use a full page layout |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $page |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function upgradeView($page = null) |
|
210
|
|
|
{ |
|
211
|
|
|
$this->view->upgrade(); |
|
212
|
|
|
|
|
213
|
|
|
if ($this->view->isUpgraded() === false) { |
|
214
|
|
|
return; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$this->view->assign('contentPage', $page ?: strtolower($this->controllerName)); |
|
218
|
|
|
|
|
219
|
|
|
try { |
|
220
|
|
|
$this->dic->get(LayoutHelper::class)->getFullLayout('main', $this->acl); |
|
221
|
|
|
} catch (\Exception $e) { |
|
222
|
|
|
processException($e); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Obtener los datos para la vista de depuración |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function getDebug() |
|
230
|
|
|
{ |
|
231
|
|
|
global $memInit; |
|
232
|
|
|
|
|
233
|
|
|
$this->view->addTemplate('debug', 'common'); |
|
234
|
|
|
|
|
235
|
|
|
$this->view->assign('time', getElapsedTime($this->router->request()->server()->get('REQUEST_TIME_FLOAT'))); |
|
236
|
|
|
$this->view->assign('memInit', $memInit / 1000); |
|
237
|
|
|
$this->view->assign('memEnd', memory_get_usage() / 1000); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Comprobar si el usuario está logado. |
|
242
|
|
|
* |
|
243
|
|
|
* @param bool $requireAuthCompleted |
|
244
|
|
|
* |
|
245
|
|
|
* @throws AuthException |
|
246
|
|
|
* @throws \DI\DependencyException |
|
247
|
|
|
* @throws \DI\NotFoundException |
|
248
|
|
|
* @throws \SP\Core\Exceptions\SessionTimeout |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function checkLoggedIn($requireAuthCompleted = true) |
|
251
|
|
|
{ |
|
252
|
|
|
if ($this->session->isLoggedIn() === false |
|
253
|
|
|
|| $this->session->getAuthCompleted() !== $requireAuthCompleted |
|
254
|
|
|
) { |
|
255
|
|
|
throw new SessionTimeout(); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
if ($this->session->isLoggedIn() |
|
259
|
|
|
&& $this->session->getAuthCompleted() === $requireAuthCompleted |
|
260
|
|
|
&& $this->configData->isAuthBasicEnabled() |
|
261
|
|
|
) { |
|
262
|
|
|
$browser = $this->dic->get(Browser::class); |
|
263
|
|
|
|
|
264
|
|
|
// Comprobar si se ha identificado mediante el servidor web y el usuario coincide |
|
265
|
|
|
if ($browser->checkServerAuthUser($this->userData->getLogin()) === false |
|
266
|
|
|
&& $browser->checkServerAuthUser($this->userData->getSsoLogin()) === false |
|
267
|
|
|
) { |
|
268
|
|
|
throw new AuthException('Invalid browser auth'); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* prepareSignedUriOnView |
|
275
|
|
|
* |
|
276
|
|
|
* Prepares view's variables to pass in a signed URI |
|
277
|
|
|
*/ |
|
278
|
|
|
final protected function prepareSignedUriOnView() |
|
279
|
|
|
{ |
|
280
|
|
|
$from = $this->request->analyzeString('from'); |
|
281
|
|
|
|
|
282
|
|
|
if ($from) { |
|
283
|
|
|
try { |
|
284
|
|
|
$this->request->verifySignature($this->configData->getPasswordSalt()); |
|
285
|
|
|
|
|
286
|
|
|
$this->view->assign('from', $from); |
|
287
|
|
|
$this->view->assign('from_hash', Hash::signMessage($from, $this->configData->getPasswordSalt())); |
|
288
|
|
|
} catch (SPException $e) { |
|
289
|
|
|
processException($e); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Comprobar si está permitido el acceso al módulo/página. |
|
296
|
|
|
* |
|
297
|
|
|
* @param null $action La acción a comprobar |
|
|
|
|
|
|
298
|
|
|
* |
|
299
|
|
|
* @return bool |
|
300
|
|
|
*/ |
|
301
|
|
|
protected function checkAccess($action) |
|
302
|
|
|
{ |
|
303
|
|
|
return $this->userData->getIsAdminApp() || $this->acl->checkUserAccess($action); |
|
304
|
|
|
} |
|
305
|
|
|
} |