1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sysPass |
4
|
|
|
* |
5
|
|
|
* @author nuxsmin |
6
|
|
|
* @link https://syspass.org |
7
|
|
|
* @copyright 2012-2019, 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
|
|
|
use DI\DependencyException; |
28
|
|
|
use DI\NotFoundException; |
29
|
|
|
use Exception; |
30
|
|
|
use SP\Core\Acl\Acl; |
31
|
|
|
use SP\Core\Events\Event; |
32
|
|
|
use SP\Core\Events\EventMessage; |
33
|
|
|
use SP\Core\Exceptions\ConstraintException; |
34
|
|
|
use SP\Core\Exceptions\QueryException; |
35
|
|
|
use SP\Core\Exceptions\SessionTimeout; |
36
|
|
|
use SP\Core\Exceptions\SPException; |
37
|
|
|
use SP\Core\Exceptions\ValidationException; |
38
|
|
|
use SP\DataModel\AuthTokenData; |
39
|
|
|
use SP\Http\JsonResponse; |
40
|
|
|
use SP\Modules\Web\Controllers\Helpers\Grid\AuthTokenGrid; |
41
|
|
|
use SP\Modules\Web\Controllers\Traits\ItemTrait; |
42
|
|
|
use SP\Modules\Web\Controllers\Traits\JsonTrait; |
43
|
|
|
use SP\Modules\Web\Forms\AuthTokenForm; |
44
|
|
|
use SP\Mvc\Controller\CrudControllerInterface; |
45
|
|
|
use SP\Mvc\View\Components\SelectItemAdapter; |
46
|
|
|
use SP\Services\Auth\AuthException; |
47
|
|
|
use SP\Services\AuthToken\AuthTokenService; |
48
|
|
|
use SP\Services\ServiceException; |
49
|
|
|
use SP\Services\User\UserService; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Class AuthTokenController |
53
|
|
|
* |
54
|
|
|
* @package SP\Modules\Web\Controllers |
55
|
|
|
*/ |
56
|
|
|
final class AuthTokenController extends ControllerBase implements CrudControllerInterface |
57
|
|
|
{ |
58
|
|
|
use JsonTrait, ItemTrait; |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var AuthTokenService |
62
|
|
|
*/ |
63
|
|
|
protected $authTokenService; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var UserService |
67
|
|
|
*/ |
68
|
|
|
protected $userService; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Search action |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
* @throws DependencyException |
75
|
|
|
* @throws NotFoundException |
76
|
|
|
* @throws ConstraintException |
77
|
|
|
* @throws QueryException |
78
|
|
|
* @throws SPException |
79
|
|
|
*/ |
80
|
|
|
public function searchAction() |
81
|
|
|
{ |
82
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
83
|
|
|
|
84
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_SEARCH)) { |
85
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->view->addTemplate('datagrid-table', 'grid'); |
89
|
|
|
$this->view->assign('index', $this->request->analyzeInt('activetab', 0)); |
90
|
|
|
$this->view->assign('data', $this->getSearchGrid()); |
91
|
|
|
|
92
|
|
|
return $this->returnJsonResponseData(['html' => $this->render()]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* getSearchGrid |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
* @throws DependencyException |
100
|
|
|
* @throws NotFoundException |
101
|
|
|
* @throws ConstraintException |
102
|
|
|
* @throws QueryException |
103
|
|
|
*/ |
104
|
|
|
protected function getSearchGrid() |
105
|
|
|
{ |
106
|
|
|
$itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request); |
107
|
|
|
|
108
|
|
|
$authTokenGrid = $this->dic->get(AuthTokenGrid::class); |
109
|
|
|
|
110
|
|
|
return $authTokenGrid->updatePager( |
111
|
|
|
$authTokenGrid->getGrid($this->authTokenService->search($itemSearchData)), |
112
|
|
|
$itemSearchData |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create action |
118
|
|
|
*/ |
119
|
|
|
public function createAction() |
120
|
|
|
{ |
121
|
|
|
try { |
122
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
123
|
|
|
|
124
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_CREATE)) { |
125
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->view->assign('header', __('New Authorization')); |
129
|
|
|
$this->view->assign('isView', false); |
130
|
|
|
$this->view->assign('route', 'authToken/saveCreate'); |
131
|
|
|
|
132
|
|
|
$this->setViewData(); |
133
|
|
|
|
134
|
|
|
$this->eventDispatcher->notifyEvent('show.authToken.create', new Event($this)); |
135
|
|
|
|
136
|
|
|
return $this->returnJsonResponseData(['html' => $this->render()]); |
137
|
|
|
} catch (Exception $e) { |
138
|
|
|
processException($e); |
139
|
|
|
|
140
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
141
|
|
|
|
142
|
|
|
return $this->returnJsonResponseException($e); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets view data for displaying auth token's data |
148
|
|
|
* |
149
|
|
|
* @param $authTokenId |
150
|
|
|
* |
151
|
|
|
* @throws ConstraintException |
152
|
|
|
* @throws QueryException |
153
|
|
|
* @throws ServiceException |
154
|
|
|
*/ |
155
|
|
|
protected function setViewData($authTokenId = null) |
156
|
|
|
{ |
157
|
|
|
$this->view->addTemplate('auth_token', 'itemshow'); |
158
|
|
|
|
159
|
|
|
$authToken = $authTokenId ? $this->authTokenService->getById($authTokenId) : new AuthTokenData(); |
160
|
|
|
$this->view->assign('authToken', $authToken); |
161
|
|
|
|
162
|
|
|
if($this->acl->checkUserAccess(Acl::AUTHTOKEN_ONLY_USER) && !$this->session->getUserData()->getIsAdminApp()) { |
163
|
|
|
$tokenUserId = $this->session->getUserData()->getId(); |
164
|
|
|
$selectItems = [$this->userService->getById($tokenUserId)]; |
165
|
|
|
} else { |
166
|
|
|
$selectItems = UserService::getItemsBasic(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->view->assign('users', SelectItemAdapter::factory($selectItems)->getItemsFromModelSelected([$authToken->getUserId()])); |
170
|
|
|
$this->view->assign('actions', SelectItemAdapter::factory(AuthTokenService::getTokenActions())->getItemsFromArraySelected([$authToken->getActionId()])); |
171
|
|
|
|
172
|
|
|
$this->view->assign('nextAction', Acl::getActionRoute(Acl::ACCESS_MANAGE)); |
173
|
|
|
|
174
|
|
|
if ($this->view->isView === true) { |
|
|
|
|
175
|
|
|
$this->view->assign('disabled', 'disabled'); |
176
|
|
|
$this->view->assign('readonly', 'readonly'); |
177
|
|
|
} else { |
178
|
|
|
$this->view->assign('disabled', false); |
179
|
|
|
$this->view->assign('readonly', false); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->view->assign('customFields', $this->getCustomFieldsForItem(Acl::AUTHTOKEN, $authTokenId)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Edit action |
187
|
|
|
* |
188
|
|
|
* @param $id |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function editAction($id) |
193
|
|
|
{ |
194
|
|
|
try { |
195
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
196
|
|
|
$tokenUserId = $this->authTokenService->getById($id)->getUserId(); |
197
|
|
|
|
198
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_EDIT) || !$this->authTokenOnlyUser($tokenUserId)) { |
199
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->view->assign('header', __('Edit Authorization')); |
203
|
|
|
$this->view->assign('isView', false); |
204
|
|
|
$this->view->assign('route', 'authToken/saveEdit/' . $id); |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
$this->setViewData($id); |
208
|
|
|
|
209
|
|
|
$this->eventDispatcher->notifyEvent('show.authToken.edit', new Event($this)); |
210
|
|
|
|
211
|
|
|
return $this->returnJsonResponseData(['html' => $this->render()]); |
212
|
|
|
} catch (Exception $e) { |
213
|
|
|
processException($e); |
214
|
|
|
|
215
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
216
|
|
|
|
217
|
|
|
return $this->returnJsonResponseException($e); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Delete action |
223
|
|
|
* |
224
|
|
|
* @param $id |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function deleteAction($id = null) |
229
|
|
|
{ |
230
|
|
|
try { |
231
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
232
|
|
|
$tokenUserId = $this->authTokenService->getById($id)->getUserId(); |
233
|
|
|
|
234
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_DELETE) || !$this->authTokenOnlyUser($tokenUserId)) { |
235
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($id === null) { |
239
|
|
|
$this->authTokenService->deleteByIdBatch($this->getItemsIdFromRequest($this->request)); |
|
|
|
|
240
|
|
|
|
241
|
|
|
$this->deleteCustomFieldsForItem(Acl::AUTHTOKEN, $id); |
242
|
|
|
|
243
|
|
|
$this->eventDispatcher->notifyEvent('delete.authToken.selection', |
244
|
|
|
new Event($this, |
245
|
|
|
EventMessage::factory() |
246
|
|
|
->addDescription(__u('Authorizations deleted'))) |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Authorizations deleted')); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$this->authTokenService->delete($id); |
253
|
|
|
|
254
|
|
|
$this->deleteCustomFieldsForItem(Acl::AUTHTOKEN, $id); |
255
|
|
|
|
256
|
|
|
$this->eventDispatcher->notifyEvent('delete.authToken', |
257
|
|
|
new Event($this, |
258
|
|
|
EventMessage::factory() |
259
|
|
|
->addDescription(__u('Authorization deleted')) |
260
|
|
|
->addDetail(__u('Authorization'), $id)) |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Authorization deleted')); |
264
|
|
|
} catch (Exception $e) { |
265
|
|
|
processException($e); |
266
|
|
|
|
267
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
268
|
|
|
|
269
|
|
|
return $this->returnJsonResponseException($e); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Saves create action |
275
|
|
|
*/ |
276
|
|
|
public function saveCreateAction() |
277
|
|
|
{ |
278
|
|
|
try { |
279
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
280
|
|
|
$tokenUserId = $this->session->getUserData()->getId(); |
281
|
|
|
|
282
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_CREATE) || (!$this->session->getUserData()->getIsAdminApp() && $tokenUserId !== $this->request->analyzeInt('users'))) { |
283
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
$form = new AuthTokenForm($this->dic); |
288
|
|
|
$form->validate(Acl::AUTHTOKEN_CREATE); |
289
|
|
|
$form->validate(Acl::AUTHTOKEN_ONLY_USER); |
290
|
|
|
|
291
|
|
|
$apiTokenData = $form->getItemData(); |
292
|
|
|
|
293
|
|
|
$id = $this->authTokenService->create($apiTokenData); |
294
|
|
|
|
295
|
|
|
$this->addCustomFieldsForItem(Acl::AUTHTOKEN, $id, $this->request); |
296
|
|
|
|
297
|
|
|
$this->eventDispatcher->notifyEvent('create.authToken', new Event($this)); |
298
|
|
|
|
299
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Authorization added')); |
300
|
|
|
} catch (ValidationException $e) { |
301
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, $e->getMessage()); |
302
|
|
|
} catch (Exception $e) { |
303
|
|
|
processException($e); |
304
|
|
|
|
305
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
306
|
|
|
|
307
|
|
|
return $this->returnJsonResponseException($e); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Saves edit action |
313
|
|
|
* |
314
|
|
|
* @param $id |
315
|
|
|
* |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
public function saveEditAction($id) |
319
|
|
|
{ |
320
|
|
|
try { |
321
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
322
|
|
|
$tokenUserId = $this->authTokenService->getById($id)->getUserId(); |
323
|
|
|
|
324
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_EDIT) || !$this->authTokenOnlyUser($tokenUserId)) { |
325
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$form = new AuthTokenForm($this->dic, $id); |
329
|
|
|
$form->validate(Acl::AUTHTOKEN_EDIT); |
330
|
|
|
|
331
|
|
|
if ($form->isRefresh()) { |
332
|
|
|
$this->authTokenService->refreshAndUpdate($form->getItemData()); |
333
|
|
|
|
334
|
|
|
$this->eventDispatcher->notifyEvent('refresh.authToken', |
335
|
|
|
new Event($this, |
336
|
|
|
EventMessage::factory() |
337
|
|
|
->addDescription(__u('Authorization updated')) |
338
|
|
|
->addDetail(__u('Authorization'), $id)) |
339
|
|
|
); |
340
|
|
|
} else { |
341
|
|
|
$this->authTokenService->update($form->getItemData()); |
342
|
|
|
|
343
|
|
|
$this->eventDispatcher->notifyEvent('edit.authToken', |
344
|
|
|
new Event($this, |
345
|
|
|
EventMessage::factory() |
346
|
|
|
->addDescription(__u('Authorization updated')) |
347
|
|
|
->addDetail(__u('Authorization'), $id)) |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$this->updateCustomFieldsForItem(Acl::AUTHTOKEN, $id, $this->request); |
352
|
|
|
|
353
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Authorization updated')); |
354
|
|
|
} catch (ValidationException $e) { |
355
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, $e->getMessage()); |
356
|
|
|
} catch (Exception $e) { |
357
|
|
|
processException($e); |
358
|
|
|
|
359
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
360
|
|
|
|
361
|
|
|
return $this->returnJsonResponseException($e); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* View action |
367
|
|
|
* |
368
|
|
|
* @param $id |
369
|
|
|
* |
370
|
|
|
* @return bool |
371
|
|
|
*/ |
372
|
|
|
public function viewAction($id) |
373
|
|
|
{ |
374
|
|
|
try { |
375
|
|
|
$this->checkSecurityToken($this->previousSk, $this->request); |
376
|
|
|
$tokenUserId = $this->authTokenService->getById($id)->getUserId(); |
377
|
|
|
|
378
|
|
|
if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_VIEW) || !$this->authTokenOnlyUser($tokenUserId)) { |
379
|
|
|
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$this->view->assign('header', __('View Authorization')); |
383
|
|
|
$this->view->assign('isView', true); |
384
|
|
|
|
385
|
|
|
|
386
|
|
|
$this->setViewData($id); |
387
|
|
|
|
388
|
|
|
$this->eventDispatcher->notifyEvent('show.authToken', |
389
|
|
|
new Event($this, EventMessage::factory() |
390
|
|
|
->addDescription(__u('Authorization viewed')) |
391
|
|
|
->addDetail(__u('Authorization'), $id)) |
392
|
|
|
); |
393
|
|
|
|
394
|
|
|
return $this->returnJsonResponseData(['html' => $this->render()]); |
395
|
|
|
} catch (Exception $e) { |
396
|
|
|
processException($e); |
397
|
|
|
|
398
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
399
|
|
|
|
400
|
|
|
return $this->returnJsonResponseException($e); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Initialize class |
406
|
|
|
* |
407
|
|
|
* @throws AuthException |
408
|
|
|
* @throws DependencyException |
409
|
|
|
* @throws NotFoundException |
410
|
|
|
* @throws SessionTimeout |
411
|
|
|
*/ |
412
|
|
|
protected function initialize() |
413
|
|
|
{ |
414
|
|
|
$this->checkLoggedIn(); |
415
|
|
|
|
416
|
|
|
$this->authTokenService = $this->dic->get(AuthTokenService::class); |
417
|
|
|
$this->userService = $this->dic->get(UserService::class); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
protected function authTokenOnlyUser($tokenUserId) { |
421
|
|
|
return $this->session->getUserData()->getIsAdminApp() || ($this->acl->checkUserAccess(Acl::AUTHTOKEN_ONLY_USER) && $tokenUserId == $this->session->getUserData()->getId()); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|