Passed
Push — devel-3.0 ( 330e85...5f7f30 )
by Rubén
03:30
created

AccountController::editPassAction()   A

Complexity

Conditions 2
Paths 11

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 11
nop 0
dl 0
loc 28
rs 9.6333
c 0
b 0
f 0
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\Api\Controllers;
26
27
use SP\Core\Acl\ActionsInterface;
28
use SP\Core\Crypt\Crypt;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventMessage;
31
use SP\Modules\Api\Controllers\Help\AccountHelp;
32
use SP\Mvc\Model\QueryCondition;
33
use SP\Services\Account\AccountRequest;
34
use SP\Services\Account\AccountSearchFilter;
35
use SP\Services\Account\AccountService;
36
use SP\Services\Api\ApiResponse;
37
38
/**
39
 * Class AccountController
40
 *
41
 * @package SP\Modules\Api\Controllers
42
 */
43
final class AccountController extends ControllerBase
44
{
45
    /**
46
     * @var AccountService
47
     */
48
    private $accountService;
49
50
    /**
51
     * viewAction
52
     */
53
    public function viewAction()
54
    {
55
        try {
56
            $this->setupApi(ActionsInterface::ACCOUNT_VIEW);
57
58
            $accountId = $this->apiService->getParamInt('id', true);
59
            $accountDetails = $this->accountService->getById($accountId)->getAccountVData();
60
61
            $this->accountService->incrementViewCounter($accountId);
62
63
            $this->eventDispatcher->notifyEvent('show.account',
64
                new Event($this, EventMessage::factory()
65
                    ->addDescription(__u('Cuenta visualizada'))
66
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
67
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName())
68
                    ->addDetail(__u('ID'), $accountDetails->getId()))
69
            );
70
71
            $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountId));
72
        } catch (\Exception $e) {
73
            $this->returnResponseException($e);
74
75
            processException($e);
76
        }
77
    }
78
79
    /**
80
     * viewPassAction
81
     */
82
    public function viewPassAction()
83
    {
84
        try {
85
            $this->setupApi(ActionsInterface::ACCOUNT_VIEW_PASS);
86
87
            $accountId = $this->apiService->getParamInt('id', true);
88
            $accountPassData = $this->accountService->getPasswordForId($accountId);
89
            $password = Crypt::decrypt($accountPassData->getPass(), $accountPassData->getKey(), $this->apiService->getMasterPass());
90
91
            $this->accountService->incrementDecryptCounter($accountId);
92
93
            $accountDetails = $this->accountService->getById($accountId)->getAccountVData();
94
95
            $this->eventDispatcher->notifyEvent('show.account.pass',
96
                new Event($this, EventMessage::factory()
97
                    ->addDescription(__u('Clave visualizada'))
98
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
99
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName())
100
                    ->addDetail(__u('ID'), $accountDetails->getId()))
101
            );
102
103
            $this->returnResponse(ApiResponse::makeSuccess(["password" => $password], $accountId));
104
        } catch (\Exception $e) {
105
            processException($e);
106
107
            $this->returnResponseException($e);
108
        }
109
    }
110
111
    /**
112
     * viewPassAction
113
     */
114
    public function editPassAction()
115
    {
116
        try {
117
            $this->setupApi(ActionsInterface::ACCOUNT_EDIT_PASS);
118
119
            $accountRequest = new AccountRequest();
120
            $accountRequest->id = $this->apiService->getParamInt('id', true);
121
            $accountRequest->pass = $this->apiService->getParamString('pass', true);
122
            $accountRequest->passDateChange = $this->apiService->getParamString('expireDate');
0 ignored issues
show
Documentation Bug introduced by
The property $passDateChange was declared of type integer, but $this->apiService->getParamString('expireDate') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
123
            $accountRequest->userEditId = $this->context->getUserData()->getId();
124
125
            $this->accountService->editPassword($accountRequest);
126
127
            $accountDetails = $this->accountService->getById($accountRequest->id)->getAccountVData();
128
129
            $this->eventDispatcher->notifyEvent('edit.account.pass',
130
                new Event($this, EventMessage::factory()
131
                    ->addDescription(__u('Clave actualizada'))
132
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
133
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName())
134
                    ->addDetail(__u('ID'), $accountDetails->getId()))
135
            );
136
137
            $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountRequest->id, __('Clave actualizada')));
138
        } catch (\Exception $e) {
139
            processException($e);
140
141
            $this->returnResponseException($e);
142
        }
143
    }
144
145
    /**
146
     * createAction
147
     */
148
    public function createAction()
149
    {
150
        try {
151
            $this->setupApi(ActionsInterface::ACCOUNT_CREATE);
152
153
            $accountRequest = new AccountRequest();
154
            $accountRequest->name = $this->apiService->getParamString('name', true);
155
            $accountRequest->clientId = $this->apiService->getParamInt('clientId', true);
156
            $accountRequest->categoryId = $this->apiService->getParamInt('categoryId', true);
157
            $accountRequest->login = $this->apiService->getParamString('login');
158
            $accountRequest->url = $this->apiService->getParamString('url');
159
            $accountRequest->notes = $this->apiService->getParamString('notes');
160
            $accountRequest->isPrivate = $this->apiService->getParamInt('private');
161
            $accountRequest->isPrivateGroup = $this->apiService->getParamInt('privateGroup');
162
            $accountRequest->passDateChange = $this->apiService->getParamInt('expireDate');
163
            $accountRequest->parentId = $this->apiService->getParamInt('parentId');
164
            $accountRequest->userId = $this->context->getUserData()->getId();
165
            $accountRequest->userGroupId = $this->context->getUserData()->getUserGroupId();
166
            $accountRequest->tags = array_map('intval', $this->apiService->getParamArray('tagsId', false, []));
167
168
            $pass = $this->accountService->getPasswordEncrypted($this->apiService->getParamRaw('pass', true), $this->apiService->getMasterPass());
169
            $accountRequest->pass = $pass['pass'];
170
            $accountRequest->key = $pass['key'];
171
172
            $accountId = $this->accountService->create($accountRequest);
173
174
            $accountDetails = $this->accountService->getById($accountId)->getAccountVData();
175
176
            $this->eventDispatcher->notifyEvent('create.account',
177
                new Event($this, EventMessage::factory()
178
                    ->addDescription(__u('Cuenta creada'))
179
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
180
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName())
181
                    ->addDetail(__u('ID'), $accountDetails->getId()))
182
            );
183
184
            $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountId, __('Cuenta creada')));
185
        } catch (\Exception $e) {
186
            processException($e);
187
188
            $this->returnResponseException($e);
189
        }
190
    }
191
192
    /**
193
     * editAction
194
     */
195
    public function editAction()
196
    {
197
        try {
198
            $this->setupApi(ActionsInterface::ACCOUNT_EDIT);
199
200
            $accountRequest = new AccountRequest();
201
            $accountRequest->id = $this->apiService->getParamInt('id', true);
202
            $accountRequest->name = $this->apiService->getParamString('name', true);
203
            $accountRequest->clientId = $this->apiService->getParamInt('clientId', true);
204
            $accountRequest->categoryId = $this->apiService->getParamInt('categoryId', true);
205
            $accountRequest->login = $this->apiService->getParamString('login');
206
            $accountRequest->url = $this->apiService->getParamString('url');
207
            $accountRequest->notes = $this->apiService->getParamString('notes');
208
            $accountRequest->isPrivate = $this->apiService->getParamInt('private');
209
            $accountRequest->isPrivateGroup = $this->apiService->getParamInt('privateGroup');
210
            $accountRequest->passDateChange = $this->apiService->getParamInt('expireDate');
211
            $accountRequest->parentId = $this->apiService->getParamInt('parentId');
212
            $accountRequest->userEditId = $this->context->getUserData()->getId();
213
214
            $tagsId = array_map('intval', $this->apiService->getParamArray('tagsId', false, []));
215
216
            if (!empty($tagsId)) {
217
                $accountRequest->updateTags = true;
218
                $accountRequest->tags = $tagsId;
219
            }
220
221
            $this->accountService->update($accountRequest);
222
223
            $accountDetails = $this->accountService->getById($accountRequest->id)->getAccountVData();
224
225
            $this->eventDispatcher->notifyEvent('edit.account',
226
                new Event($this, EventMessage::factory()
227
                    ->addDescription(__u('Cuenta actualizada'))
228
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
229
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName())
230
                    ->addDetail(__u('ID'), $accountDetails->getId()))
231
            );
232
233
            $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountRequest->id, __('Cuenta actualizada')));
234
        } catch (\Exception $e) {
235
            processException($e);
236
237
            $this->returnResponseException($e);
238
        }
239
    }
240
241
    /**
242
     * searchAction
243
     */
244
    public function searchAction()
245
    {
246
        try {
247
            $this->setupApi(ActionsInterface::ACCOUNT_SEARCH);
248
249
            $accountSearchFilter = new AccountSearchFilter();
250
            $accountSearchFilter->setCleanTxtSearch($this->apiService->getParamString('text'));
251
            $accountSearchFilter->setCategoryId($this->apiService->getParamInt('categoryId'));
252
            $accountSearchFilter->setClientId($this->apiService->getParamInt('clientId'));
253
254
            $tagsId = array_map('intval', $this->apiService->getParamArray('tagsId', false, []));
255
256
            if (!empty($tagsId)) {
257
                $accountSearchFilter->setTagsId($tagsId);
258
            }
259
260
            $op = $this->apiService->getParamString('op');
261
262
            if ($op !== null) {
0 ignored issues
show
introduced by
The condition $op !== null is always true.
Loading history...
263
                switch ($op) {
264
                    case 'and':
265
                        $accountSearchFilter->setFilterOperator(QueryCondition::CONDITION_AND);
266
                        break;
267
                    case 'or':
268
                        $accountSearchFilter->setFilterOperator(QueryCondition::CONDITION_OR);
269
                        break;
270
                }
271
            }
272
273
            $accountSearchFilter->setLimitCount($this->apiService->getParamInt('count', false, 50));
274
            $accountSearchFilter->setSortOrder($this->apiService->getParamInt('order', false, AccountSearchFilter::SORT_DEFAULT));
275
276
            $this->returnResponse(ApiResponse::makeSuccess($this->accountService->getByFilter($accountSearchFilter)));
277
        } catch (\Exception $e) {
278
            processException($e);
279
280
            $this->returnResponseException($e);
281
        }
282
    }
283
284
    /**
285
     * deleteAction
286
     */
287
    public function deleteAction()
288
    {
289
        try {
290
            $this->setupApi(ActionsInterface::ACCOUNT_DELETE);
291
292
            $accountId = $this->apiService->getParamInt('id', true);
293
294
            $accountDetails = $this->accountService->getById($accountId)->getAccountVData();
295
296
            $this->accountService->delete($accountId);
297
298
            $this->eventDispatcher->notifyEvent('delete.account',
299
                new Event($this, EventMessage::factory()
300
                    ->addDescription(__u('Cuenta eliminada'))
301
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
302
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName())
303
                    ->addDetail(__u('ID'), $accountDetails->getId()))
304
            );
305
306
            $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountId, __('Cuenta eliminada')));
307
        } catch (\Exception $e) {
308
            processException($e);
309
310
            $this->returnResponseException($e);
311
        }
312
    }
313
314
    /**
315
     * @throws \SP\Core\Exceptions\InvalidClassException
316
     */
317
    protected function initialize()
318
    {
319
        $this->accountService = $this->dic->get(AccountService::class);
320
        $this->apiService->setHelpClass(AccountHelp::class);
321
    }
322
}