Passed
Push — devel-3.0 ( 543c48...5b8639 )
by Rubén
03:28
created

AccountManagerController::setViewData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 0
dl 0
loc 27
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\Web\Controllers;
26
27
use SP\Core\Acl\Acl;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\Http\JsonResponse;
31
use SP\Modules\Web\Controllers\Helpers\Grid\AccountGrid;
32
use SP\Modules\Web\Controllers\Traits\ItemTrait;
33
use SP\Modules\Web\Controllers\Traits\JsonTrait;
34
use SP\Modules\Web\Forms\AccountForm;
35
use SP\Mvc\View\Components\SelectItemAdapter;
36
use SP\Services\Account\AccountBulkRequest;
37
use SP\Services\Account\AccountHistoryService;
38
use SP\Services\Account\AccountSearchFilter;
39
use SP\Services\Account\AccountSearchService;
40
use SP\Services\Account\AccountService;
41
use SP\Services\Category\CategoryService;
42
use SP\Services\Client\ClientService;
43
use SP\Services\Tag\TagService;
44
use SP\Services\User\UserService;
45
use SP\Services\UserGroup\UserGroupService;
46
use SP\Util\Util;
47
48
/**
49
 * Class AccountManagerController
50
 *
51
 * @package SP\Modules\Web\Controllers
52
 */
53
final class AccountManagerController extends ControllerBase
54
{
55
    use JsonTrait, ItemTrait;
0 ignored issues
show
introduced by
The trait SP\Modules\Web\Controllers\Traits\ItemTrait requires some properties which are not provided by SP\Modules\Web\Controlle...ccountManagerController: $data, $key
Loading history...
56
57
    /**
58
     * @var AccountService
59
     */
60
    protected $accountService;
61
    /**
62
     * @var AccountSearchService
63
     */
64
    protected $accountSearchService;
65
66
    /**
67
     * @return bool
68
     * @throws \DI\DependencyException
69
     * @throws \DI\NotFoundException
70
     * @throws \SP\Core\Exceptions\ConstraintException
71
     * @throws \SP\Core\Exceptions\QueryException
72
     * @throws \SP\Core\Exceptions\SPException
73
     */
74
    public function searchAction()
75
    {
76
        if (!$this->acl->checkUserAccess(Acl::ACCOUNTMGR_SEARCH)) {
77
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
78
        }
79
80
        $this->view->addTemplate('datagrid-table', 'grid');
81
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
82
        $this->view->assign('data', $this->getSearchGrid());
83
84
        return $this->returnJsonResponseData(['html' => $this->render()]);
85
    }
86
87
    /**
88
     * getSearchGrid
89
     *
90
     * @return $this
91
     * @throws \DI\DependencyException
92
     * @throws \DI\NotFoundException
93
     * @throws \SP\Core\Exceptions\ConstraintException
94
     * @throws \SP\Core\Exceptions\QueryException
95
     * @throws \SP\Core\Exceptions\SPException
96
     */
97
    protected function getSearchGrid()
98
    {
99
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
100
101
        $accountGrid = $this->dic->get(AccountGrid::class);
102
103
        $filter = new AccountSearchFilter();
104
        $filter->setTxtSearch($itemSearchData->getSeachString());
105
        $filter->setLimitCount($itemSearchData->getLimitCount());
106
        $filter->setLimitStart($itemSearchData->getLimitStart());
107
        $filter->setStringFilters($this->accountSearchService->analyzeQueryFilters($itemSearchData->getSeachString()));
108
109
        return $accountGrid->updatePager(
110
            $accountGrid->getGrid(
111
                $this->accountService->getByFilter($filter)),
112
            $itemSearchData);
113
    }
114
115
    /**
116
     * Delete action
117
     *
118
     * @param $id
119
     *
120
     * @return bool
121
     */
122
    public function deleteAction($id = null)
123
    {
124
        try {
125
            if ($id === null) {
126
                $this->accountService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
127
128
                $this->deleteCustomFieldsForItem(Acl::ACCOUNT, $id);
129
130
                $this->eventDispatcher->notifyEvent('delete.account.selection',
131
                    new Event($this, EventMessage::factory()->addDescription(__u('Cuentas eliminadas')))
132
                );
133
134
                return $this->returnJsonResponseData(JsonResponse::JSON_SUCCESS, __u('Cuentas eliminadas'));
0 ignored issues
show
Bug introduced by
__u('Cuentas eliminadas') of type string is incompatible with the type integer expected by parameter $status of SP\Modules\Web\Controlle...eturnJsonResponseData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
                return $this->returnJsonResponseData(JsonResponse::JSON_SUCCESS, /** @scrutinizer ignore-type */ __u('Cuentas eliminadas'));
Loading history...
135
            }
136
137
            $accountDetails = $this->accountService->getById($id)->getAccountVData();
138
139
            $this->accountService->delete($id);
140
141
            $this->deleteCustomFieldsForItem(Acl::ACCOUNT, $id);
142
143
            $this->eventDispatcher->notifyEvent('delete.account',
144
                new Event($this, EventMessage::factory()
145
                    ->addDescription(__u('Cuenta eliminada'))
146
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
147
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName()))
148
            );
149
150
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Cuenta eliminada'));
151
        } catch (\Exception $e) {
152
            processException($e);
153
154
            return $this->returnJsonResponseException($e);
155
        }
156
    }
157
158
    /**
159
     * saveBulkEditAction
160
     *
161
     * @return bool
162
     */
163
    public function saveBulkEditAction()
164
    {
165
        try {
166
            $form = new AccountForm($this->dic);
167
            $form->validate(Acl::ACCOUNTMGR_BULK_EDIT);
168
169
            $request = new AccountBulkRequest(
170
                Util::itemsIdAdapter($this->request->analyzeString('itemsId')),
171
                $form->getItemData());
172
            $request->setDeleteHistory($this->request->analyzeBool('delete_history', false));
173
174
            if ($request->isDeleteHistory()) {
175
                $accountHistoryService = $this->dic->get(AccountHistoryService::class);
176
                $accountHistoryService->deleteByAccountIdBatch($request->getItemsId());
177
            }
178
179
            $this->accountService->updateBulk($request);
180
181
//            $this->updateCustomFieldsForItem(Acl::ACCOUNT, $id, $this->request);
182
183
            $this->eventDispatcher->notifyEvent('edit.account.bulk',
184
                new Event($this, EventMessage::factory()
185
                    ->addDescription(__u('Cuentas actualizadas')))
186
            );
187
188
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Cuentas actualizadas'));
189
        } catch (\Exception $e) {
190
            processException($e);
191
192
            return $this->returnJsonResponseException($e);
193
        }
194
    }
195
196
    /**
197
     * bulkEditAction
198
     *
199
     * @return bool
200
     */
201
    public function bulkEditAction()
202
    {
203
        if (!$this->acl->checkUserAccess(Acl::ACCOUNTMGR)) {
204
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
205
        }
206
207
        $this->view->assign('header', __('Actualización Masiva'));
208
        $this->view->assign('isView', false);
209
        $this->view->assign('route', 'accountManager/saveBulkEdit');
210
        $this->view->assign('itemsId', $this->getItemsIdFromRequest($this->request));
211
212
        try {
213
            $this->setViewData();
214
215
            $this->eventDispatcher->notifyEvent('show.account.bulkEdit', new Event($this));
216
217
            return $this->returnJsonResponseData(['html' => $this->render()]);
218
        } catch (\Exception $e) {
219
            processException($e);
220
221
            return $this->returnJsonResponseException($e);
222
        }
223
    }
224
225
    /**
226
     * Sets view data
227
     */
228
    protected function setViewData()
229
    {
230
        $this->view->addTemplate('account_bulkedit', 'itemshow');
231
232
        $this->view->assign('sk', $this->session->generateSecurityKey());
233
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::ITEMS_MANAGE));
234
235
        $clients = SelectItemAdapter::factory(ClientService::getItemsBasic())->getItemsFromModel();
236
        $categories = SelectItemAdapter::factory(CategoryService::getItemsBasic())->getItemsFromModel();
237
        $tags = SelectItemAdapter::factory(TagService::getItemsBasic())->getItemsFromModel();
238
239
        $users = SelectItemAdapter::factory(UserService::getItemsBasic())->getItemsFromModel();
240
        $userGroups = SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel();
241
242
        $this->view->assign('users', $users);
243
        $this->view->assign('userGroups', $userGroups);
244
245
        $this->view->assign('clients', $clients);
246
        $this->view->assign('categories', $categories);
247
        $this->view->assign('tags', $tags);
248
249
        if ($this->view->isView === true) {
0 ignored issues
show
introduced by
The condition $this->view->isView === true is always false.
Loading history...
Bug Best Practice introduced by
The property isView does not exist on SP\Mvc\View\Template. Since you implemented __get, consider adding a @property annotation.
Loading history...
250
            $this->view->assign('disabled', 'disabled');
251
            $this->view->assign('readonly', 'readonly');
252
        } else {
253
            $this->view->assign('disabled');
254
            $this->view->assign('readonly');
255
        }
256
    }
257
258
    /**
259
     * Initialize class
260
     *
261
     * @throws \DI\DependencyException
262
     * @throws \DI\NotFoundException
263
     * @throws \SP\Services\Auth\AuthException
264
     */
265
    protected function initialize()
266
    {
267
        $this->checkLoggedIn();
268
269
        $this->accountService = $this->dic->get(AccountService::class);
270
        $this->accountSearchService = $this->dic->get(AccountSearchService::class);
271
    }
272
}