Passed
Pull Request — master (#1541)
by
unknown
08:47
created

AccountService::getForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Services\Account;
26
27
use Defuse\Crypto\Exception\CryptoException;
28
use Exception;
29
use Psr\Container\ContainerExceptionInterface;
30
use Psr\Container\NotFoundExceptionInterface;
31
use SP\Core\Crypt\Crypt;
32
use SP\Core\Exceptions\ConstraintException;
33
use SP\Core\Exceptions\NoSuchPropertyException;
34
use SP\Core\Exceptions\QueryException;
35
use SP\Core\Exceptions\SPException;
36
use SP\DataModel\AccountData;
37
use SP\DataModel\AccountExtData;
38
use SP\DataModel\AccountHistoryData;
39
use SP\DataModel\AccountPassData;
40
use SP\DataModel\Dto\AccountDetailsResponse;
41
use SP\DataModel\Dto\AccountHistoryCreateDto;
42
use SP\DataModel\ItemPreset\AccountPermission;
43
use SP\DataModel\ItemPreset\AccountPrivate;
44
use SP\DataModel\ItemSearchData;
45
use SP\Repositories\Account\AccountRepository;
46
use SP\Repositories\Account\AccountToTagRepository;
47
use SP\Repositories\Account\AccountToUserGroupRepository;
48
use SP\Repositories\Account\AccountToUserRepository;
49
use SP\Repositories\NoSuchItemException;
50
use SP\Services\Config\ConfigService;
51
use SP\Services\ItemPreset\ItemPresetInterface;
52
use SP\Services\ItemPreset\ItemPresetService;
53
use SP\Services\Service;
54
use SP\Services\ServiceException;
55
use SP\Services\ServiceItemTrait;
56
use SP\Storage\Database\QueryResult;
57
use stdClass;
58
59
/**
60
 * Class AccountService
61
 *
62
 * @package SP\Services\Account
63
 */
64
final class AccountService extends Service implements AccountServiceInterface
65
{
66
    use ServiceItemTrait;
67
68
    /**
69
     * @var AccountRepository
70
     */
71
    protected $accountRepository;
72
    /**
73
     * @var AccountToUserGroupRepository
74
     */
75
    protected $accountToUserGroupRepository;
76
    /**
77
     * @var AccountToUserRepository
78
     */
79
    protected $accountToUserRepository;
80
    /**
81
     * @var AccountToTagRepository
82
     */
83
    protected $accountToTagRepository;
84
    /**
85
     * @var ItemPresetService
86
     */
87
    protected $itemPresetService;
88
89
    /**
90
     * @param AccountDetailsResponse $accountDetailsResponse
91
     *
92
     * @return AccountService
93
     * @throws QueryException
94
     * @throws ConstraintException
95
     */
96
    public function withUsersById(AccountDetailsResponse $accountDetailsResponse)
97
    {
98
        $accountDetailsResponse->setUsers($this->accountToUserRepository->getUsersByAccountId($accountDetailsResponse->getId())->getDataAsArray());
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param AccountDetailsResponse $accountDetailsResponse
105
     *
106
     * @return AccountService
107
     * @throws QueryException
108
     * @throws ConstraintException
109
     */
110
    public function withUserGroupsById(AccountDetailsResponse $accountDetailsResponse)
111
    {
112
        $accountDetailsResponse->setUserGroups($this->accountToUserGroupRepository->getUserGroupsByAccountId($accountDetailsResponse->getId())->getDataAsArray());
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param AccountDetailsResponse $accountDetailsResponse
119
     *
120
     * @return AccountService
121
     * @throws QueryException
122
     * @throws ConstraintException
123
     */
124
    public function withTagsById(AccountDetailsResponse $accountDetailsResponse)
125
    {
126
        $accountDetailsResponse->setTags($this->accountToTagRepository->getTagsByAccountId($accountDetailsResponse->getId())->getDataAsArray());
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param $id
133
     *
134
     * @return bool
135
     * @throws QueryException
136
     * @throws ConstraintException
137
     */
138
    public function incrementViewCounter($id)
139
    {
140
        return $this->accountRepository->incrementViewCounter($id);
141
    }
142
143
    /**
144
     * @param $id
145
     *
146
     * @return bool
147
     * @throws QueryException
148
     * @throws ConstraintException
149
     */
150
    public function incrementDecryptCounter($id)
151
    {
152
        return $this->accountRepository->incrementDecryptCounter($id);
153
    }
154
155
    /**
156
     * @param $id
157
     *
158
     * @return AccountPassData
159
     * @throws QueryException
160
     * @throws ConstraintException
161
     * @throws NoSuchItemException
162
     */
163
    public function getPasswordForId($id)
164
    {
165
        $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter();
166
167
        $result = $this->accountRepository->getPasswordForId($id, $queryFilter);
168
169
        if ($result->getNumRows() === 0) {
170
            throw new NoSuchItemException(__u('Account not found'));
171
        }
172
173
        return $result->getData();
174
    }
175
176
    /**
177
     * @param AccountRequest $accountRequest
178
     *
179
     * @return int
180
     * @throws QueryException
181
     * @throws SPException
182
     * @throws ConstraintException
183
     * @throws NoSuchPropertyException
184
     */
185
    public function create(AccountRequest $accountRequest)
186
    {
187
        $userData = $this->context->getUserData();
188
189
        $accountRequest->changePermissions = AccountAclService::getShowPermission(
190
            $userData,
191
            $this->context->getUserProfile());
192
193
        if (empty($accountRequest->userGroupId)
194
            || !$accountRequest->changePermissions
195
        ) {
196
            $accountRequest->userGroupId = $userData->getUserGroupId();
197
        }
198
199
        if (empty($accountRequest->userId)
200
            || !$accountRequest->changePermissions
201
        ) {
202
            $accountRequest->userId = $userData->getId();
203
        }
204
205
        if (empty($accountRequest->key)) {
206
            $pass = $this->getPasswordEncrypted($accountRequest->pass);
207
208
            $accountRequest->pass = $pass['pass'];
209
            $accountRequest->key = $pass['key'];
210
        }
211
212
        $this->setPresetPrivatePermissions($accountRequest);
213
214
        $accountRequest->id = $this->accountRepository->create($accountRequest);
215
216
        $this->addItems($accountRequest);
217
218
        $this->addPresetPermissions($accountRequest->id);
219
220
        return $accountRequest->id;
221
    }
222
223
    /**
224
     * Devolver los datos de la clave encriptados
225
     *
226
     * @param string $pass
227
     * @param string $masterPass Clave maestra a utilizar
228
     *
229
     * @return array
230
     * @throws ServiceException
231
     */
232
    public function getPasswordEncrypted($pass, $masterPass = null)
233
    {
234
        try {
235
            if ($masterPass === null) {
236
                $masterPass = $this->getMasterKeyFromContext();
237
            }
238
239
            if (empty($masterPass)) {
240
                throw new ServiceException(__u('Master password not set'));
241
            }
242
243
            $out['key'] = Crypt::makeSecuredKey($masterPass);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.
Loading history...
244
            $out['pass'] = Crypt::encrypt($pass, $out['key'], $masterPass);
245
246
            if (strlen($out['pass']) > 1000 || strlen($out['key']) > 1000) {
247
                throw new ServiceException(__u('Internal error'));
248
            }
249
250
            return $out;
251
        } catch (CryptoException $e) {
252
            throw new ServiceException(__u('Internal error'));
253
        }
254
    }
255
256
    /**
257
     * @param AccountRequest $accountRequest
258
     *
259
     * @throws QueryException
260
     * @throws ConstraintException
261
     * @throws NoSuchPropertyException
262
     * @throws NoSuchItemException
263
     */
264
    private function setPresetPrivatePermissions(AccountRequest $accountRequest)
265
    {
266
        $userData = $this->context->getUserData();
267
        $itemPreset = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PRIVATE);
268
269
        if ($itemPreset !== null
270
            && $itemPreset->getFixed()
271
        ) {
272
            $accountPrivate = $itemPreset->hydrate(AccountPrivate::class);
273
274
            $userId = $accountRequest->userId;
275
276
            if ($userId === null && $accountRequest->id > 0) {
0 ignored issues
show
introduced by
The condition $userId === null is always false.
Loading history...
277
                $userId = $this->getById($accountRequest->id)->getAccountVData()->getUserId();
278
            }
279
280
            if ($userData->getId() === $userId) {
281
                $accountRequest->isPrivate = (int)$accountPrivate->isPrivateUser();
282
            }
283
284
            if ($userData->getUserGroupId() === $accountRequest->userGroupId) {
285
                $accountRequest->isPrivateGroup = (int)$accountPrivate->isPrivateGroup();
286
            }
287
        }
288
289
        $itemPresetData = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION);
290
291
        if ($itemPresetData !== null
292
            && $itemPresetData->getFixed()
293
        ) {
294
            $accountPermission = $itemPresetData->hydrate(AccountPermission::class);
295
296
            $accountRequest->userGroupId = (int)$accountPermission->getMainUsergroupId();
297
        }
298
299
    }
300
301
    /**
302
     * @param int $id
303
     *
304
     * @return AccountDetailsResponse
305
     * @throws QueryException
306
     * @throws NoSuchItemException
307
     * @throws ConstraintException
308
     */
309
    public function getById($id)
310
    {
311
        $result = $this->accountRepository->getById($id);
312
313
        if ($result->getNumRows() === 0) {
314
            throw new NoSuchItemException(__u('The account doesn\'t exist'));
315
        }
316
317
        return new AccountDetailsResponse($id, $result->getData());
0 ignored issues
show
Bug introduced by
It seems like $result->getData() can also be of type null; however, parameter $accountVData of SP\DataModel\Dto\Account...Response::__construct() does only seem to accept SP\DataModel\AccountVData, maybe add an additional type check? ( Ignorable by Annotation )

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

317
        return new AccountDetailsResponse($id, /** @scrutinizer ignore-type */ $result->getData());
Loading history...
318
    }
319
320
    /**
321
     * Adds external items to the account
322
     *
323
     * @param AccountRequest $accountRequest
324
     */
325
    private function addItems(AccountRequest $accountRequest)
326
    {
327
        try {
328
329
            if ($accountRequest->changePermissions) {
330
                if (is_array($accountRequest->userGroupsView)
331
                    && !empty($accountRequest->userGroupsView)
332
                ) {
333
                    $this->accountToUserGroupRepository->addByType($accountRequest, false);
334
                }
335
336
                if (is_array($accountRequest->userGroupsEdit)
337
                    && !empty($accountRequest->userGroupsEdit)
338
                ) {
339
                    $this->accountToUserGroupRepository->addByType($accountRequest, true);
340
                }
341
342
                if (is_array($accountRequest->usersView)
343
                    && !empty($accountRequest->usersView)
344
                ) {
345
                    $this->accountToUserRepository->addByType($accountRequest, false);
346
                }
347
348
                if (is_array($accountRequest->usersEdit)
349
                    && !empty($accountRequest->usersEdit)
350
                ) {
351
                    $this->accountToUserRepository->addByType($accountRequest, true);
352
                }
353
            }
354
355
            if (is_array($accountRequest->tags)
356
                && !empty($accountRequest->tags)
357
            ) {
358
                $this->accountToTagRepository->add($accountRequest);
359
            }
360
        } catch (SPException $e) {
361
            logger($e->getMessage());
362
        }
363
    }
364
365
    /**
366
     * @param int $accountId
367
     *
368
     * @throws QueryException
369
     * @throws ConstraintException
370
     * @throws NoSuchPropertyException
371
     */
372
    private function addPresetPermissions(int $accountId)
373
    {
374
        $itemPresetData = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION);
375
376
        if ($itemPresetData !== null
377
            && $itemPresetData->getFixed()
378
        ) {
379
            $userData = $this->context->getUserData();
380
            $accountPermission = $itemPresetData->hydrate(AccountPermission::class);
381
382
            $accountRequest = new AccountRequest();
383
            $accountRequest->id = $accountId;
384
            $accountRequest->usersView = array_diff($accountPermission->getUsersView(), [$userData->getId()]);
385
            $accountRequest->usersEdit = array_diff($accountPermission->getUsersEdit(), [$userData->getId()]);
386
            $accountRequest->userGroupsView = array_diff($accountPermission->getUserGroupsView(), [$userData->getUserGroupId()]);
387
            $accountRequest->userGroupsEdit = array_diff($accountPermission->getUserGroupsEdit(), [$userData->getUserGroupId()]);
388
389
            if (!empty($accountRequest->usersView)) {
390
                $this->accountToUserRepository->addByType($accountRequest, false);
391
            }
392
393
            if (!empty($accountRequest->usersEdit)) {
394
                $this->accountToUserRepository->addByType($accountRequest, true);
395
            }
396
397
            if (!empty($accountRequest->userGroupsView)) {
398
                $this->accountToUserGroupRepository->addByType($accountRequest, false);
399
            }
400
401
            if (!empty($accountRequest->userGroupsEdit)) {
402
                $this->accountToUserGroupRepository->addByType($accountRequest, true);
403
            }
404
        }
405
    }
406
407
    /**
408
     * @param AccountHistoryData $data
409
     *
410
     * @return int
411
     * @throws QueryException
412
     * @throws ConstraintException
413
     */
414
    public function createFromHistory(AccountHistoryData $data)
415
    {
416
        $accountRequest = new AccountRequest();
417
        $accountRequest->name = $data->getName();
418
        $accountRequest->categoryId = $data->getCategoryId();
419
        $accountRequest->clientId = $data->getClientId();
420
        $accountRequest->url = $data->getUrl();
421
        $accountRequest->login = $data->getLogin();
422
        $accountRequest->pass = $data->getPass();
423
        $accountRequest->key = $data->getKey();
424
        $accountRequest->notes = $data->getNotes();
425
        $accountRequest->userId = $data->getUserId();
426
        $accountRequest->userGroupId = $data->getUserGroupId();
427
        $accountRequest->passDateChange = $data->getPassDateChange();
428
        $accountRequest->parentId = $data->getParentId();
429
        $accountRequest->isPrivate = $data->getIsPrivate();
430
        $accountRequest->isPrivateGroup = $data->getIsPrivateGroup();
431
432
        return $this->accountRepository->create($accountRequest);
433
    }
434
435
    /**
436
     * Updates external items for the account
437
     *
438
     * @param AccountRequest $accountRequest
439
     *
440
     * @throws Exception
441
     */
442
    public function update(AccountRequest $accountRequest)
443
    {
444
        $this->transactionAware(function () use ($accountRequest) {
445
            $userData = $this->context->getUserData();
446
            $userProfile = $this->context->getUserProfile();
447
448
            $accountRequest->changePermissions =
449
                AccountAclService::getShowPermission($userData, $userProfile);
450
451
            if ($accountRequest->changePermissions) {
452
                $account = $this->getById($accountRequest->id)->getAccountVData();
453
454
                $accountRequest->changeOwner = $accountRequest->userId > 0
455
                    && ($userData->getIsAdminApp()
456
                        || $userData->getIsAdminAcc()
457
                        || ($userProfile->isAccPermission()
458
                            && $userData->getId() === $account->getUserId()));
459
460
                $accountRequest->changeUserGroup = $accountRequest->userGroupId > 0
461
                    && ($userData->getIsAdminApp()
462
                        || $userData->getIsAdminAcc()
463
                        || ($userProfile->isAccPermission()
464
                            && ($userData->getUserGroupId() === $account->getUserGroupId())
465
                            || $userData->getId() === $account->getUserId()));
466
            }
467
468
            $this->addHistory($accountRequest->id);
469
470
            $this->setPresetPrivatePermissions($accountRequest);
471
472
            $this->accountRepository->update($accountRequest);
473
474
            $this->updateItems($accountRequest);
475
476
            $this->addPresetPermissions($accountRequest->id);
477
        });
478
    }
479
480
    /**
481
     * @param int  $accountId
482
     * @param bool $isDelete
483
     *
484
     * @return bool
485
     * @throws NoSuchItemException
486
     * @throws QueryException
487
     * @throws ServiceException
488
     * @throws ConstraintException
489
     */
490
    private function addHistory($accountId, $isDelete = false)
491
    {
492
        $accountHistoryRepository = $this->dic->get(AccountHistoryService::class);
493
        $configService = $this->dic->get(ConfigService::class);
494
495
        return $accountHistoryRepository->create(
496
            new AccountHistoryCreateDto(
497
                $accountId,
498
                !$isDelete,
499
                $isDelete,
500
                $configService->getByParam('masterPwd'))
501
        );
502
    }
503
504
    /**
505
     * Updates external items for the account
506
     *
507
     * @param AccountRequest $accountRequest
508
     *
509
     * @throws QueryException
510
     * @throws ConstraintException
511
     */
512
    private function updateItems(AccountRequest $accountRequest)
513
    {
514
        if ($accountRequest->changePermissions) {
515
            if ($accountRequest->userGroupsView !== null) {
516
                if (count($accountRequest->userGroupsView) > 0) {
517
                    $this->accountToUserGroupRepository->updateByType($accountRequest, false);
518
                } else {
519
                    $this->accountToUserGroupRepository->deleteTypeByAccountId($accountRequest->id, false);
520
                }
521
            }
522
523
            if ($accountRequest->userGroupsEdit !== null) {
524
                if (count($accountRequest->userGroupsEdit) > 0) {
525
                    $this->accountToUserGroupRepository->updateByType($accountRequest, true);
526
                } else {
527
                    $this->accountToUserGroupRepository->deleteTypeByAccountId($accountRequest->id, true);
528
                }
529
            }
530
531
            if ($accountRequest->usersView !== null) {
532
                if (count($accountRequest->usersView) > 0) {
533
                    $this->accountToUserRepository->updateByType($accountRequest, false);
534
                } else {
535
                    $this->accountToUserRepository->deleteTypeByAccountId($accountRequest->id, false);
536
                }
537
            }
538
539
            if ($accountRequest->usersEdit !== null) {
540
                if (count($accountRequest->usersEdit) > 0) {
541
                    $this->accountToUserRepository->updateByType($accountRequest, true);
542
                } else {
543
                    $this->accountToUserRepository->deleteTypeByAccountId($accountRequest->id, true);
544
                }
545
            }
546
        }
547
548
        if ($accountRequest->tags !== null) {
549
            if (count($accountRequest->tags) > 0) {
550
                $this->accountToTagRepository->update($accountRequest);
551
            } else {
552
                $this->accountToTagRepository->deleteByAccountId($accountRequest->id);
553
            }
554
        }
555
    }
556
557
    /**
558
     * Update accounts in bulk mode
559
     *
560
     * @param AccountBulkRequest $request
561
     *
562
     * @throws ServiceException
563
     */
564
    public function updateBulk(AccountBulkRequest $request)
565
    {
566
        $this->transactionAware(function () use ($request) {
567
            foreach ($request->getItemsId() as $itemId) {
568
                $accountRequest = $request->getAccountRequestForId($itemId);
569
570
                $this->addHistory($accountRequest->id);
571
572
                $this->accountRepository->updateBulk($accountRequest);
573
574
                $this->updateItems($accountRequest);
575
            }
576
        });
577
    }
578
579
    /**
580
     * @param AccountRequest $accountRequest
581
     *
582
     * @throws Exception
583
     */
584
    public function editPassword(AccountRequest $accountRequest)
585
    {
586
        $this->transactionAware(function () use ($accountRequest) {
587
            $this->addHistory($accountRequest->id);
588
589
            $pass = $this->getPasswordEncrypted($accountRequest->pass);
590
591
            $accountRequest->pass = $pass['pass'];
592
            $accountRequest->key = $pass['key'];
593
594
            $this->accountRepository->editPassword($accountRequest);
595
        });
596
    }
597
598
    /**
599
     * Updates an already encrypted password data from a master password changing action
600
     *
601
     * @param AccountPasswordRequest $accountRequest
602
     *
603
     * @return bool
604
     * @throws ConstraintException
605
     * @throws QueryException
606
     */
607
    public function updatePasswordMasterPass(AccountPasswordRequest $accountRequest)
608
    {
609
        return $this->accountRepository->updatePassword($accountRequest);
610
    }
611
612
    /**
613
     * @param $historyId
614
     * @param $accountId
615
     *
616
     * @throws Exception
617
     */
618
    public function editRestore($historyId, $accountId)
619
    {
620
        $this->transactionAware(function () use ($historyId, $accountId) {
621
            $this->addHistory($accountId);
622
623
            if (!$this->accountRepository->editRestore($historyId, $this->context->getUserData()->getId())) {
624
                throw new ServiceException(__u('Error on restoring the account'));
625
            }
626
        });
627
    }
628
629
    /**
630
     * @param $id
631
     *
632
     * @return AccountService
633
     * @throws ServiceException
634
     */
635
    public function delete($id)
636
    {
637
        $this->transactionAware(function () use ($id) {
638
            $this->addHistory($id, 1);
639
640
            if ($this->accountRepository->delete($id) === 0) {
641
                throw new NoSuchItemException(__u('Account not found'));
642
            }
643
        });
644
645
        return $this;
646
    }
647
648
    /**
649
     * @param array $ids
650
     *
651
     * @return AccountService
652
     * @throws SPException
653
     * @throws ServiceException
654
     */
655
    public function deleteByIdBatch(array $ids)
656
    {
657
        if ($this->accountRepository->deleteByIdBatch($ids) === 0) {
658
            throw new ServiceException(__u('Error while deleting the accounts'));
659
        }
660
661
        return $this;
662
    }
663
664
    /**
665
     * @param $accountId
666
     *
667
     * @return array
668
     * @throws QueryException
669
     * @throws ConstraintException
670
     */
671
    public function getForUser($accountId = null)
672
    {
673
        $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter();
674
675
        if (null !== $accountId) {
676
            $queryFilter->addFilter('Account.id <> ? AND (Account.parentId = 0 OR Account.parentId IS NULL)', [$accountId]);
677
        }
678
679
        return $this->accountRepository->getForUser($queryFilter)->getDataAsArray();
680
    }
681
682
    /**
683
     * @param $accountId
684
     *
685
     * @return array
686
     * @throws QueryException
687
     * @throws ConstraintException
688
     */
689
    public function getLinked($accountId)
690
    {
691
        $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter();
692
693
        $queryFilter->addFilter('Account.parentId = ?', [$accountId]);
694
695
        return $this->accountRepository->getLinked($queryFilter)->getDataAsArray();
696
    }
697
698
    /**
699
     * @param $id
700
     *
701
     * @return AccountPassData
702
     * @throws QueryException
703
     * @throws ConstraintException
704
     * @throws NoSuchItemException
705
     */
706
    public function getPasswordHistoryForId($id)
707
    {
708
        $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilterHistory();
709
        $queryFilter->addFilter('AccountHistory.id = ?', [$id]);
710
711
        $result = $this->accountRepository->getPasswordHistoryForId($queryFilter);
712
713
        if ($result->getNumRows() === 0) {
714
            throw new NoSuchItemException(__u('The account doesn\'t exist'));
715
        }
716
717
        return $result->getData();
718
    }
719
720
    /**
721
     * @return AccountData[]
722
     * @throws QueryException
723
     * @throws ConstraintException
724
     */
725
    public function getAllBasic()
726
    {
727
        return $this->accountRepository->getAll()->getDataAsArray();
728
    }
729
730
    /**
731
     * @param ItemSearchData $itemSearchData
732
     *
733
     * @return QueryResult
734
     * @throws QueryException
735
     * @throws ConstraintException
736
     */
737
    public function search(ItemSearchData $itemSearchData)
738
    {
739
        return $this->accountRepository->search($itemSearchData);
740
    }
741
742
    /**
743
     * Devolver el número total de cuentas
744
     *
745
     * @return stdClass
746
     * @throws QueryException
747
     * @throws ConstraintException
748
     */
749
    public function getTotalNumAccounts()
750
    {
751
        return $this->accountRepository->getTotalNumAccounts()->num;
752
    }
753
754
    /**
755
     * Obtener los datos de una cuenta.
756
     *
757
     * @param $id
758
     *
759
     * @return AccountExtData
760
     * @throws QueryException
761
     * @throws NoSuchItemException
762
     * @throws ConstraintException
763
     */
764
    public function getDataForLink($id)
765
    {
766
        $result = $this->accountRepository->getDataForLink($id);
767
768
        if ($result->getNumRows() === 0) {
769
            throw new NoSuchItemException(__u('The account doesn\'t exist'));
770
        }
771
772
        return $result->getData();
773
    }
774
775
    /**
776
     * Obtener los datos relativos a la clave de todas las cuentas.
777
     *
778
     * @return array Con los datos de la clave
779
     * @throws QueryException
780
     * @throws ConstraintException
781
     */
782
    public function getAccountsPassData()
783
    {
784
        return $this->accountRepository->getAccountsPassData();
785
    }
786
787
    /**
788
     * Obtener las cuentas de una búsqueda.
789
     *
790
     * @param AccountSearchFilter $accountSearchFilter
791
     *
792
     * @return QueryResult
793
     * @throws QueryException
794
     * @throws SPException
795
     * @throws ConstraintException
796
     */
797
    public function getByFilter(AccountSearchFilter $accountSearchFilter)
798
    {
799
        $accountFilterUser = $this->dic->get(AccountFilterUser::class);
800
801
        return $this->accountRepository->getByFilter(
802
            $accountSearchFilter,
803
            $accountFilterUser->getFilter($accountSearchFilter->getGlobalSearch())
804
        );
805
    }
806
807
    /**
808
     * @throws ContainerExceptionInterface
809
     * @throws NotFoundExceptionInterface
810
     */
811
    protected function initialize()
812
    {
813
        $this->accountRepository = $this->dic->get(AccountRepository::class);
814
        $this->accountToUserRepository = $this->dic->get(AccountToUserRepository::class);
815
        $this->accountToUserGroupRepository = $this->dic->get(AccountToUserGroupRepository::class);
816
        $this->accountToTagRepository = $this->dic->get(AccountToTagRepository::class);
817
        $this->itemPresetService = $this->dic->get(ItemPresetService::class);
818
    }
819
}