getActionsGrouppedForAccount()   C
last analyzed

Complexity

Conditions 13
Paths 48

Size

Total Lines 67
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 43
nc 48
nop 2
dl 0
loc 67
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helpers\Account;
26
27
use SP\Core\Acl\Acl;
28
use SP\Core\Acl\ActionsInterface;
29
use SP\Core\UI\ThemeIcons;
30
use SP\Html\DataGrid\Action\DataGridAction;
31
use SP\Html\DataGrid\Action\DataGridActionType;
32
use SP\Modules\Web\Controllers\Helpers\HelperBase;
33
use SP\Services\Account\AccountAcl;
34
use SP\Services\Account\AccountSearchItem;
35
36
/**
37
 * Class AccountIconsHelper
38
 *
39
 * @package SP\Modules\Web\Controllers\Helpers
40
 */
41
final class AccountActionsHelper extends HelperBase
42
{
43
    /**
44
     * @var ThemeIcons
45
     */
46
    protected $icons;
47
    /**
48
     * @var string
49
     */
50
    protected $sk;
51
52
    /**
53
     * @return DataGridAction
54
     */
55
    public function getViewAction()
56
    {
57
        $action = new DataGridAction();
58
        $action->setId(ActionsInterface::ACCOUNT_VIEW);
59
        $action->setType(DataGridActionType::VIEW_ITEM);
60
        $action->setName(__('Account Details'));
61
        $action->setTitle(__('Account Details'));
62
        $action->addClass('btn-action');
63
        $action->setIcon($this->icons->getIconView());
64
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowView');
65
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
66
        $action->addData('action-sk', $this->sk);
67
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
68
        $action->addAttribute('type', 'button');
69
70
        return $action;
71
    }
72
73
    /**
74
     * Set icons for view
75
     *
76
     * @param AccountAcl        $accountAcl
77
     * @param AccountActionsDto $accountActionsDto
78
     *
79
     * @return DataGridAction[]
80
     */
81
    public function getActionsForAccount(AccountAcl $accountAcl, AccountActionsDto $accountActionsDto)
82
    {
83
        $actions = [];
84
85
        $actionBack = $this->getBackAction();
86
87
        if ($accountActionsDto->isHistory()) {
88
            $actionBack->addData('item-id', $accountActionsDto->getAccountId());
89
            $actionBack->setName(__('View Current'));
90
            $actionBack->setTitle(__('View Current'));
91
        } else {
92
            $actionBack->setData([]);
93
            $actionBack->setClasses(['btn-back']);
94
        }
95
96
        $actions[] = $actionBack;
97
98
        if ($accountAcl->isShowEditPass()) {
99
            $actions[] = $this->getEditPassAction()->addData('item-id', $accountActionsDto->getAccountId());
100
        }
101
102
        if ($accountAcl->isShowEdit()) {
103
            $actions[] = $this->getEditAction()->addData('item-id', $accountActionsDto->getAccountId());
104
        }
105
106
        if ($accountAcl->getActionId() === ActionsInterface::ACCOUNT_VIEW
107
            && !$accountAcl->isShowEdit()
108
            && $this->configData->isMailRequestsEnabled()
109
        ) {
110
            $actions[] = $this->getRequestAction()->addData('item-id', $accountActionsDto->getAccountId());
111
        }
112
113
        if ($accountAcl->isShowRestore()) {
114
            $actionRestore = $this->getRestoreAction();
115
            $actionRestore->addData('item-id', $accountActionsDto->getAccountId());
116
            $actionRestore->addData('history-id', $accountActionsDto->getAccountHistoryId());
117
118
            $actions[] = $actionRestore;
119
        }
120
121
        if ($accountAcl->isShowSave()) {
122
            $actions[] = $this->getSaveAction()->addAttribute('form', 'frmAccount');
123
        }
124
125
        return $actions;
126
    }
127
128
    /**
129
     * @return DataGridAction
130
     */
131
    public function getBackAction()
132
    {
133
        $action = new DataGridAction();
134
        $action->setId('btnBack');
0 ignored issues
show
Bug introduced by
'btnBack' of type string is incompatible with the type integer expected by parameter $id of SP\Html\DataGrid\Action\...GridActionBase::setId(). ( Ignorable by Annotation )

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

134
        $action->setId(/** @scrutinizer ignore-type */ 'btnBack');
Loading history...
135
        $action->setName(__('Back'));
136
        $action->setTitle(__('Back'));
137
        $action->addClass('btn-action');
138
        $action->setIcon($this->icons->getIconBack());
139
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
140
        $action->addData('action-sk', $this->sk);
141
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
142
        $action->addAttribute('type', 'button');
143
144
        return $action;
145
    }
146
147
    /**
148
     * @return DataGridAction
149
     */
150
    public function getEditPassAction()
151
    {
152
        $action = new DataGridAction();
153
        $action->setId(ActionsInterface::ACCOUNT_EDIT_PASS);
154
        $action->setType(DataGridActionType::VIEW_ITEM);
155
        $action->setName(__('Edit Account Password'));
156
        $action->setTitle(__('Edit Account Password'));
157
        $action->addClass('btn-action');
158
        $action->setIcon($this->icons->getIconEditPass());
159
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowViewPass');
160
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_EDIT_PASS));
161
        $action->addData('action-sk', $this->sk);
162
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_EDIT_PASS));
163
        $action->addAttribute('type', 'button');
164
165
        return $action;
166
    }
167
168
    /**
169
     * @return DataGridAction
170
     */
171
    public function getEditAction()
172
    {
173
        $action = new DataGridAction();
174
        $action->setId(ActionsInterface::ACCOUNT_EDIT);
175
        $action->setType(DataGridActionType::EDIT_ITEM);
176
        $action->setName(__('Edit Account'));
177
        $action->setTitle(__('Edit Account'));
178
        $action->addClass('btn-action');
179
        $action->setIcon($this->icons->getIconEdit());
180
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowEdit');
181
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_EDIT));
182
        $action->addData('action-sk', $this->sk);
183
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_EDIT));
184
        $action->addAttribute('type', 'button');
185
186
        return $action;
187
    }
188
189
    /**
190
     * @return DataGridAction
191
     */
192
    public function getRequestAction()
193
    {
194
        $action = new DataGridAction();
195
        $action->setId(ActionsInterface::ACCOUNT_REQUEST);
196
        $action->setName(__('Request Modification'));
197
        $action->setTitle(__('Request Modification'));
198
        $action->addClass('btn-action');
199
        $action->setIcon($this->icons->getIconEmail());
200
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowRequest');
201
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_REQUEST));
202
        $action->addData('action-sk', $this->sk);
203
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
204
        $action->addAttribute('type', 'submit');
205
206
        return $action;
207
    }
208
209
    /**
210
     * @return DataGridAction
211
     */
212
    public function getRestoreAction()
213
    {
214
        $action = new DataGridAction();
215
        $action->setId(ActionsInterface::ACCOUNT_EDIT_RESTORE);
216
        $action->setType(DataGridActionType::VIEW_ITEM);
217
        $action->setName(__('Restore account from this point'));
218
        $action->setTitle(__('Restore account from this point'));
219
        $action->addClass('btn-action');
220
        $action->setIcon($this->icons->getIconRestore());
221
        $action->addData('action-route', 'account/saveEditRestore');
222
        $action->addData('action-sk', $this->sk);
223
        $action->addData('onclick', 'account/saveEditRestore');
224
        $action->addAttribute('type', 'button');
225
226
        return $action;
227
    }
228
229
    /**
230
     * @return DataGridAction
231
     */
232
    public function getSaveAction()
233
    {
234
        $action = new DataGridAction();
235
        $action->setId(ActionsInterface::ACCOUNT);
236
        $action->setType(DataGridActionType::VIEW_ITEM);
237
        $action->setName(__('Save'));
238
        $action->setTitle(__('Save'));
239
        $action->addClass('btn-action');
240
        $action->setIcon($this->icons->getIconSave());
241
        $action->addData('action-route', 'account/save');
242
        $action->addData('action-sk', $this->sk);
243
        $action->addData('onclick', 'account/save');
244
        $action->addAttribute('type', 'submit');
245
246
        return $action;
247
    }
248
249
    /**
250
     * Set icons for view
251
     *
252
     * @param AccountAcl        $accountAcl
253
     * @param AccountActionsDto $accountActionsDto
254
     *
255
     * @return DataGridAction[]
256
     */
257
    public function getActionsGrouppedForAccount(AccountAcl $accountAcl, AccountActionsDto $accountActionsDto)
258
    {
259
        $userData = $this->context->getUserData();
260
261
        $actions = [];
262
263
        if ($accountAcl->isShowDelete()) {
264
            $actions[] = $this->getDeleteAction()->addData('item-id', $accountActionsDto->getAccountId());
265
        }
266
267
        if ($accountActionsDto->isHistory() === false
268
            && $accountActionsDto->isLinked() === false
269
            && $this->configData->isPublinksEnabled()
270
            && $accountAcl->isShowLink()
271
            && $accountAcl->isShowViewPass()
272
        ) {
273
            $itemId = $accountActionsDto->getPublicLinkId();
274
275
            if ($itemId) {
276
                $actionRefresh = $this->getPublicLinkRefreshAction();
277
                $actionRefresh->addData('item-id', $itemId);
278
                $actionRefresh->addData('account-id', $accountActionsDto->getAccountId());
279
280
                $actions[] = $actionRefresh;
281
282
                if ($userData->getIsAdminApp()
283
                    || $userData->getId() === $accountActionsDto->getPublicLinkCreatorId()
284
                ) {
285
                    $actionDelete = $this->getPublicLinkDeleteAction();
286
                    $actionDelete->addData('item-id', $itemId);
287
                    $actionDelete->addData('account-id', $accountActionsDto->getAccountId());
288
289
                    $actions[] = $actionDelete;
290
                }
291
            } else {
292
                $action = $this->getPublicLinkAction();
293
                $action->addData('account-id', $accountActionsDto->getAccountId());
294
295
                $actions[] = $action;
296
            }
297
        }
298
299
        if ($accountAcl->isShowViewPass()) {
300
            if ($accountActionsDto->isHistory()) {
301
                $actionViewPass = $this->getViewPassHistoryAction()
302
                    ->addData('item-id', $accountActionsDto->getAccountHistoryId());
303
                $actionCopy = $this->getCopyPassHistoryAction()
304
                    ->addData('item-id', $accountActionsDto->getAccountHistoryId());
305
            } else {
306
                $actionViewPass = $this->getViewPassAction()
307
                    ->addData('item-id', $accountActionsDto->getAccountId());
308
                $actionCopy = $this->getCopyPassAction()
309
                    ->addData('item-id', $accountActionsDto->getAccountId());
310
            }
311
312
            $actionViewPass->addData('parent-id', $accountActionsDto->getAccountParentId());
313
            $actionCopy->addData('parent-id', $accountActionsDto->getAccountParentId());
314
315
            $actions[] = $actionViewPass;
316
            $actions[] = $actionCopy;
317
        }
318
319
        if ($accountAcl->isShowCopy()) {
320
            $actions[] = $this->getCopyAction()->addData('item-id', $accountActionsDto->getAccountId());
321
        }
322
323
        return $actions;
324
    }
325
326
    /**
327
     * @return DataGridAction
328
     */
329
    public function getDeleteAction()
330
    {
331
        $action = new DataGridAction();
332
        $action->setId(ActionsInterface::ACCOUNT_DELETE);
333
        $action->setType(DataGridActionType::DELETE_ITEM);
334
        $action->setName(__('Remove Account'));
335
        $action->setTitle(__('Remove Account'));
336
        $action->addClass('btn-action');
337
        $action->setIcon($this->icons->getIconDelete());
338
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowDelete');
339
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_DELETE));
340
        $action->addData('action-sk', $this->sk);
341
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_DELETE));
342
        $action->addAttribute('type', 'button');
343
344
        return $action;
345
    }
346
347
    /**
348
     * @return DataGridAction
349
     */
350
    public function getPublicLinkRefreshAction()
351
    {
352
        $action = new DataGridAction();
353
        $action->setId(ActionsInterface::PUBLICLINK_REFRESH);
354
        $action->setName(__('Update Public Link'));
355
        $action->setTitle(__('Update Public Link'));
356
        $action->addClass('btn-action');
357
        $action->setIcon($this->icons->getIconPublicLink());
358
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::PUBLICLINK_REFRESH));
359
        $action->addData('action-sk', $this->sk);
360
        $action->addData('onclick', 'link/refresh');
361
        $action->addData('action-next', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
362
        $action->addAttribute('type', 'button');
363
364
        return $action;
365
    }
366
367
    /**
368
     * @return DataGridAction
369
     */
370
    public function getPublicLinkDeleteAction()
371
    {
372
        $icon = clone $this->icons->getIconPublicLink();
373
        $icon->setIcon('link_off');
374
375
        $action = new DataGridAction();
376
        $action->setId(ActionsInterface::PUBLICLINK_DELETE);
377
        $action->setName(__('Delete Public Link'));
378
        $action->setTitle(__('Delete Public Link'));
379
        $action->addClass('btn-action');
380
        $action->setIcon($icon);
381
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::PUBLICLINK_DELETE));
382
        $action->addData('action-sk', $this->sk);
383
        $action->addData('onclick', 'link/delete');
384
        $action->addData('action-next', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
385
        $action->addAttribute('type', 'button');
386
387
        return $action;
388
    }
389
390
    /**
391
     * @return DataGridAction
392
     */
393
    public function getPublicLinkAction()
394
    {
395
        $action = new DataGridAction();
396
        $action->setId(ActionsInterface::PUBLICLINK_CREATE);
397
        $action->setName(__('Create Public Link'));
398
        $action->setTitle(__('Create Public Link'));
399
        $action->addClass('btn-action');
400
        $action->setIcon($this->icons->getIconPublicLink());
401
        $action->addData('action-route', 'publicLink/saveCreateFromAccount');
402
        $action->addData('action-sk', $this->sk);
403
        $action->addData('onclick', 'link/save');
404
        $action->addData('action-next', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW));
405
        $action->addAttribute('type', 'button');
406
407
        return $action;
408
    }
409
410
    /**
411
     * @return DataGridAction
412
     */
413
    public function getViewPassHistoryAction()
414
    {
415
        $action = new DataGridAction();
416
        $action->setId(ActionsInterface::ACCOUNT_VIEW_PASS);
417
        $action->setType(DataGridActionType::VIEW_ITEM);
418
        $action->setName(__('View password'));
419
        $action->setTitle(__('View password'));
420
        $action->addClass('btn-action');
421
        $action->setIcon($this->icons->getIconViewPass());
422
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowViewPass');
423
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_HISTORY_VIEW_PASS));
424
        $action->addData('action-full', 1);
425
        $action->addData('action-sk', $this->sk);
426
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_HISTORY_VIEW_PASS));
427
        $action->addAttribute('type', 'button');
428
429
        return $action;
430
    }
431
432
    /**
433
     * @return DataGridAction
434
     */
435
    public function getCopyPassHistoryAction()
436
    {
437
        $action = new DataGridAction();
438
        $action->setId(ActionsInterface::ACCOUNT_VIEW_PASS);
439
        $action->setType(DataGridActionType::VIEW_ITEM);
440
        $action->setName(__('Copy Password to Clipboard'));
441
        $action->setTitle(__('Copy Password to Clipboard'));
442
        $action->addClass('btn-action');
443
        $action->addClass('clip-pass-button');
444
        $action->setIcon($this->icons->getIconClipboard());
445
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowCopyPass');
446
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_HISTORY_COPY_PASS));
447
        $action->addData('action-full', 0);
448
        $action->addData('action-sk', $this->sk);
449
        $action->addData('useclipboard', '1');
450
        $action->addAttribute('type', 'button');
451
452
        return $action;
453
    }
454
455
    /**
456
     * @return DataGridAction
457
     */
458
    public function getViewPassAction()
459
    {
460
        $action = new DataGridAction();
461
        $action->setId(ActionsInterface::ACCOUNT_VIEW_PASS);
462
        $action->setType(DataGridActionType::VIEW_ITEM);
463
        $action->setName(__('View password'));
464
        $action->setTitle(__('View password'));
465
        $action->addClass('btn-action');
466
        $action->setIcon($this->icons->getIconViewPass());
467
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowViewPass');
468
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW_PASS));
469
        $action->addData('action-full', 1);
470
        $action->addData('action-sk', $this->sk);
471
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW_PASS));
472
        $action->addAttribute('type', 'button');
473
474
        return $action;
475
    }
476
477
    /**
478
     * @return DataGridAction
479
     */
480
    public function getCopyPassAction()
481
    {
482
        $action = new DataGridAction();
483
        $action->setId(ActionsInterface::ACCOUNT_VIEW_PASS);
484
        $action->setType(DataGridActionType::VIEW_ITEM);
485
        $action->setName(__('Copy Password to Clipboard'));
486
        $action->setTitle(__('Copy Password to Clipboard'));
487
        $action->addClass('btn-action');
488
        $action->addClass('clip-pass-button');
489
        $action->setIcon($this->icons->getIconClipboard());
490
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowCopyPass');
491
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_COPY_PASS));
492
        $action->addData('action-full', 0);
493
        $action->addData('action-sk', $this->sk);
494
        $action->addData('useclipboard', '1');
495
        $action->addAttribute('type', 'button');
496
497
        return $action;
498
    }
499
500
    /**
501
     * @return DataGridAction
502
     */
503
    public function getCopyAction()
504
    {
505
        $action = new DataGridAction();
506
        $action->setId(ActionsInterface::ACCOUNT_COPY);
507
        $action->setType(DataGridActionType::MENUBAR_ITEM);
508
        $action->setName(__('Copy Account'));
509
        $action->setTitle(__('Copy Account'));
510
        $action->addClass('btn-action');
511
        $action->setIcon($this->icons->getIconCopy());
512
        $action->setRuntimeFilter(AccountSearchItem::class, 'isShowCopy');
513
        $action->addData('action-route', Acl::getActionRoute(ActionsInterface::ACCOUNT_COPY));
514
        $action->addData('action-sk', $this->sk);
515
        $action->addData('onclick', Acl::getActionRoute(ActionsInterface::ACCOUNT_COPY));
516
        $action->addAttribute('type', 'button');
517
518
        return $action;
519
    }
520
521
    /**
522
     * Initialize class
523
     */
524
    protected function initialize()
525
    {
526
        $this->sk = $this->view->get('sk');
527
        $this->icons = $this->view->getTheme()->getIcons();
528
    }
529
}