| Total Complexity | 81 |
| Total Lines | 685 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AccountService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AccountService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | final class AccountService extends Service implements AccountServiceInterface |
||
| 59 | { |
||
| 60 | use ServiceItemTrait; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var AccountRepository |
||
| 64 | */ |
||
| 65 | protected $accountRepository; |
||
| 66 | /** |
||
| 67 | * @var AccountToUserGroupRepository |
||
| 68 | */ |
||
| 69 | protected $accountToUserGroupRepository; |
||
| 70 | /** |
||
| 71 | * @var AccountToUserRepository |
||
| 72 | */ |
||
| 73 | protected $accountToUserRepository; |
||
| 74 | /** |
||
| 75 | * @var AccountToTagRepository |
||
| 76 | */ |
||
| 77 | protected $accountToTagRepository; |
||
| 78 | /** |
||
| 79 | * @var ItemPresetService |
||
| 80 | */ |
||
| 81 | protected $itemPresetService; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param AccountDetailsResponse $accountDetailsResponse |
||
| 85 | * |
||
| 86 | * @return AccountService |
||
| 87 | * @throws QueryException |
||
| 88 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 89 | */ |
||
| 90 | public function withUsersById(AccountDetailsResponse $accountDetailsResponse) |
||
| 91 | { |
||
| 92 | $accountDetailsResponse->setUsers($this->accountToUserRepository->getUsersByAccountId($accountDetailsResponse->getId())); |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param AccountDetailsResponse $accountDetailsResponse |
||
| 99 | * |
||
| 100 | * @return AccountService |
||
| 101 | * @throws QueryException |
||
| 102 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 103 | */ |
||
| 104 | public function withUserGroupsById(AccountDetailsResponse $accountDetailsResponse) |
||
| 105 | { |
||
| 106 | $accountDetailsResponse->setUserGroups($this->accountToUserGroupRepository->getUserGroupsByAccountId($accountDetailsResponse->getId())->getDataAsArray()); |
||
| 107 | |||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param AccountDetailsResponse $accountDetailsResponse |
||
| 113 | * |
||
| 114 | * @return AccountService |
||
| 115 | * @throws QueryException |
||
| 116 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 117 | */ |
||
| 118 | public function withTagsById(AccountDetailsResponse $accountDetailsResponse) |
||
| 119 | { |
||
| 120 | $accountDetailsResponse->setTags($this->accountToTagRepository->getTagsByAccountId($accountDetailsResponse->getId())->getDataAsArray()); |
||
| 121 | |||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param $id |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | * @throws QueryException |
||
| 130 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 131 | */ |
||
| 132 | public function incrementViewCounter($id) |
||
| 133 | { |
||
| 134 | return $this->accountRepository->incrementViewCounter($id); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param $id |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | * @throws QueryException |
||
| 142 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 143 | */ |
||
| 144 | public function incrementDecryptCounter($id) |
||
| 145 | { |
||
| 146 | return $this->accountRepository->incrementDecryptCounter($id); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $id |
||
| 151 | * |
||
| 152 | * @return \SP\DataModel\AccountPassData |
||
| 153 | * @throws QueryException |
||
| 154 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 155 | * @throws NoSuchItemException |
||
| 156 | */ |
||
| 157 | public function getPasswordForId($id) |
||
| 158 | { |
||
| 159 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter(); |
||
| 160 | |||
| 161 | $result = $this->accountRepository->getPasswordForId($id, $queryFilter); |
||
| 162 | |||
| 163 | if ($result->getNumRows() === 0) { |
||
| 164 | throw new NoSuchItemException(__u('Cuenta no encontrada')); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $result->getData(); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param AccountRequest $accountRequest |
||
| 172 | * |
||
| 173 | * @return int |
||
| 174 | * @throws QueryException |
||
| 175 | * @throws SPException |
||
| 176 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 177 | * @throws \SP\Core\Exceptions\NoSuchPropertyException |
||
| 178 | */ |
||
| 179 | public function create(AccountRequest $accountRequest) |
||
| 180 | { |
||
| 181 | $accountRequest->changePermissions = AccountAclService::getShowPermission($this->context->getUserData(), $this->context->getUserProfile()); |
||
| 182 | $accountRequest->userGroupId = $accountRequest->userGroupId ?: $this->context->getUserData()->getUserGroupId(); |
||
| 183 | |||
| 184 | if (empty($accountRequest->key)) { |
||
| 185 | $pass = $this->getPasswordEncrypted($accountRequest->pass); |
||
| 186 | |||
| 187 | $accountRequest->pass = $pass['pass']; |
||
| 188 | $accountRequest->key = $pass['key']; |
||
| 189 | } |
||
| 190 | |||
| 191 | $this->setPresetPrivate($accountRequest); |
||
| 192 | |||
| 193 | $accountRequest->id = $this->accountRepository->create($accountRequest); |
||
| 194 | |||
| 195 | $this->addItems($accountRequest); |
||
| 196 | |||
| 197 | $this->addPresetPermissions($accountRequest->id); |
||
| 198 | |||
| 199 | return $accountRequest->id; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Devolver los datos de la clave encriptados |
||
| 204 | * |
||
| 205 | * @param string $pass |
||
| 206 | * @param string $masterPass Clave maestra a utilizar |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | * @throws ServiceException |
||
| 210 | */ |
||
| 211 | public function getPasswordEncrypted($pass, $masterPass = null) |
||
| 212 | { |
||
| 213 | try { |
||
| 214 | if ($masterPass === null) { |
||
| 215 | $masterPass = $this->getMasterKeyFromContext(); |
||
| 216 | } |
||
| 217 | |||
| 218 | if (empty($masterPass)) { |
||
| 219 | throw new ServiceException(__u('Clave maestra no establecida')); |
||
| 220 | } |
||
| 221 | |||
| 222 | $out['key'] = Crypt::makeSecuredKey($masterPass); |
||
|
|
|||
| 223 | $out['pass'] = Crypt::encrypt($pass, $out['key'], $masterPass); |
||
| 224 | |||
| 225 | if (strlen($out['pass']) > 1000 || strlen($out['key']) > 1000) { |
||
| 226 | throw new ServiceException(__u('Error interno')); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $out; |
||
| 230 | } catch (CryptoException $e) { |
||
| 231 | throw new ServiceException(__u('Error interno')); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param AccountRequest $accountRequest |
||
| 237 | * |
||
| 238 | * @throws QueryException |
||
| 239 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 240 | * @throws \SP\Core\Exceptions\NoSuchPropertyException |
||
| 241 | * @throws NoSuchItemException |
||
| 242 | */ |
||
| 243 | private function setPresetPrivate(AccountRequest $accountRequest) |
||
| 244 | { |
||
| 245 | $userData = $this->context->getUserData(); |
||
| 246 | $itemPreset = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PRIVATE); |
||
| 247 | |||
| 248 | if ($itemPreset !== null |
||
| 249 | && $itemPreset->getFixed() |
||
| 250 | ) { |
||
| 251 | $accountPrivate = $itemPreset->hydrate(AccountPrivate::class); |
||
| 252 | |||
| 253 | $userId = $accountRequest->userId; |
||
| 254 | |||
| 255 | if ($userId === null && $accountRequest->id > 0) { |
||
| 256 | $userId = $this->getById($accountRequest->id)->getAccountVData()->getUserId(); |
||
| 257 | } |
||
| 258 | |||
| 259 | if ($userData->getId() === $userId) { |
||
| 260 | $accountRequest->isPrivate = (int)$accountPrivate->isPrivateUser(); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($userData->getUserGroupId() === $accountRequest->userGroupId) { |
||
| 264 | $accountRequest->isPrivateGroup = (int)$accountPrivate->isPrivateGroup(); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param int $id |
||
| 271 | * |
||
| 272 | * @return AccountDetailsResponse |
||
| 273 | * @throws QueryException |
||
| 274 | * @throws \SP\Repositories\NoSuchItemException |
||
| 275 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 276 | */ |
||
| 277 | public function getById($id) |
||
| 278 | { |
||
| 279 | $result = $this->accountRepository->getById($id); |
||
| 280 | |||
| 281 | if ($result->getNumRows() === 0) { |
||
| 282 | throw new NoSuchItemException(__u('La cuenta no existe')); |
||
| 283 | } |
||
| 284 | |||
| 285 | return new AccountDetailsResponse($id, $result->getData()); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds external items to the account |
||
| 290 | * |
||
| 291 | * @param AccountRequest $accountRequest |
||
| 292 | */ |
||
| 293 | private function addItems(AccountRequest $accountRequest) |
||
| 294 | { |
||
| 295 | try { |
||
| 296 | |||
| 297 | if ($accountRequest->changePermissions) { |
||
| 298 | if (is_array($accountRequest->userGroupsView) |
||
| 299 | && !empty($accountRequest->userGroupsView) |
||
| 300 | ) { |
||
| 301 | $this->accountToUserGroupRepository->add($accountRequest); |
||
| 302 | } |
||
| 303 | |||
| 304 | if (is_array($accountRequest->userGroupsEdit) |
||
| 305 | && !empty($accountRequest->userGroupsEdit) |
||
| 306 | ) { |
||
| 307 | $this->accountToUserGroupRepository->addEdit($accountRequest); |
||
| 308 | } |
||
| 309 | |||
| 310 | if (is_array($accountRequest->usersView) |
||
| 311 | && !empty($accountRequest->usersView) |
||
| 312 | ) { |
||
| 313 | $this->accountToUserRepository->add($accountRequest); |
||
| 314 | } |
||
| 315 | |||
| 316 | if (is_array($accountRequest->usersEdit) |
||
| 317 | && !empty($accountRequest->usersEdit) |
||
| 318 | ) { |
||
| 319 | $this->accountToUserRepository->addEdit($accountRequest); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | if (is_array($accountRequest->tags) |
||
| 324 | && !empty($accountRequest->tags) |
||
| 325 | ) { |
||
| 326 | $this->accountToTagRepository->add($accountRequest); |
||
| 327 | } |
||
| 328 | } catch (SPException $e) { |
||
| 329 | logger($e->getMessage()); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param int $accountId |
||
| 335 | * |
||
| 336 | * @throws QueryException |
||
| 337 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 338 | * @throws \SP\Core\Exceptions\NoSuchPropertyException |
||
| 339 | */ |
||
| 340 | private function addPresetPermissions(int $accountId) |
||
| 341 | { |
||
| 342 | $itemPresetData = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION); |
||
| 343 | |||
| 344 | if ($itemPresetData !== null |
||
| 345 | && $itemPresetData->getFixed() |
||
| 346 | ) { |
||
| 347 | $userData = $this->context->getUserData(); |
||
| 348 | $accountPermission = $itemPresetData->hydrate(AccountPermission::class); |
||
| 349 | |||
| 350 | $accountRequest = new AccountRequest(); |
||
| 351 | $accountRequest->id = $accountId; |
||
| 352 | $accountRequest->usersView = array_diff($accountPermission->getUsersView(), [$userData->getId()]); |
||
| 353 | $accountRequest->usersEdit = array_diff($accountPermission->getUsersEdit(), [$userData->getId()]); |
||
| 354 | $accountRequest->userGroupsView = array_diff($accountPermission->getUserGroupsView(), [$userData->getUserGroupId()]); |
||
| 355 | $accountRequest->userGroupsEdit = array_diff($accountPermission->getUserGroupsEdit(), [$userData->getUserGroupId()]); |
||
| 356 | |||
| 357 | if (!empty($accountRequest->usersView)) { |
||
| 358 | $this->accountToUserRepository->add($accountRequest); |
||
| 359 | } |
||
| 360 | |||
| 361 | if (!empty($accountRequest->usersEdit)) { |
||
| 362 | $this->accountToUserRepository->addEdit($accountRequest); |
||
| 363 | } |
||
| 364 | |||
| 365 | if (!empty($accountRequest->userGroupsView)) { |
||
| 366 | $this->accountToUserGroupRepository->add($accountRequest); |
||
| 367 | } |
||
| 368 | |||
| 369 | if (!empty($accountRequest->userGroupsEdit)) { |
||
| 370 | $this->accountToUserGroupRepository->addEdit($accountRequest); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param AccountHistoryData $data |
||
| 377 | * |
||
| 378 | * @return int |
||
| 379 | * @throws QueryException |
||
| 380 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 381 | */ |
||
| 382 | public function createFromHistory(AccountHistoryData $data) |
||
| 383 | { |
||
| 384 | $accountRequest = new AccountRequest(); |
||
| 385 | $accountRequest->name = $data->getName(); |
||
| 386 | $accountRequest->categoryId = $data->getCategoryId(); |
||
| 387 | $accountRequest->clientId = $data->getClientId(); |
||
| 388 | $accountRequest->url = $data->getUrl(); |
||
| 389 | $accountRequest->login = $data->getLogin(); |
||
| 390 | $accountRequest->pass = $data->getPass(); |
||
| 391 | $accountRequest->key = $data->getKey(); |
||
| 392 | $accountRequest->notes = $data->getNotes(); |
||
| 393 | $accountRequest->userId = $data->getUserId(); |
||
| 394 | $accountRequest->userGroupId = $data->getUserGroupId(); |
||
| 395 | $accountRequest->passDateChange = $data->getPassDateChange(); |
||
| 396 | $accountRequest->parentId = $data->getParentId(); |
||
| 397 | $accountRequest->isPrivate = $data->getIsPrivate(); |
||
| 398 | $accountRequest->isPrivateGroup = $data->getIsPrivateGroup(); |
||
| 399 | |||
| 400 | return $this->accountRepository->create($accountRequest); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Updates external items for the account |
||
| 405 | * |
||
| 406 | * @param AccountRequest $accountRequest |
||
| 407 | * |
||
| 408 | * @throws \Exception |
||
| 409 | */ |
||
| 410 | public function update(AccountRequest $accountRequest) |
||
| 411 | { |
||
| 412 | $this->transactionAware(function () use ($accountRequest) { |
||
| 413 | $accountRequest->changePermissions = AccountAclService::getShowPermission($this->context->getUserData(), $this->context->getUserProfile()); |
||
| 414 | |||
| 415 | // Cambiar el grupo principal si el usuario es Admin |
||
| 416 | $accountRequest->changeUserGroup = ($accountRequest->userGroupId > 0 |
||
| 417 | && ($this->context->getUserData()->getIsAdminApp() || $this->context->getUserData()->getIsAdminAcc())); |
||
| 418 | |||
| 419 | $this->addHistory($accountRequest->id); |
||
| 420 | |||
| 421 | $this->setPresetPrivate($accountRequest); |
||
| 422 | |||
| 423 | $this->accountRepository->update($accountRequest); |
||
| 424 | |||
| 425 | $this->updateItems($accountRequest); |
||
| 426 | |||
| 427 | $this->addPresetPermissions($accountRequest->id); |
||
| 428 | }); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param int $accountId |
||
| 433 | * @param bool $isDelete |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | * @throws NoSuchItemException |
||
| 437 | * @throws QueryException |
||
| 438 | * @throws ServiceException |
||
| 439 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 440 | */ |
||
| 441 | private function addHistory($accountId, $isDelete = false) |
||
| 442 | { |
||
| 443 | $accountHistoryRepository = $this->dic->get(AccountHistoryService::class); |
||
| 444 | $configService = $this->dic->get(ConfigService::class); |
||
| 445 | |||
| 446 | return $accountHistoryRepository->create( |
||
| 447 | new AccountHistoryCreateDto( |
||
| 448 | $accountId, |
||
| 449 | !$isDelete, |
||
| 450 | $isDelete, |
||
| 451 | $configService->getByParam('masterPwd')) |
||
| 452 | ); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Updates external items for the account |
||
| 457 | * |
||
| 458 | * @param AccountRequest $accountRequest |
||
| 459 | * |
||
| 460 | * @throws QueryException |
||
| 461 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 462 | */ |
||
| 463 | private function updateItems(AccountRequest $accountRequest) |
||
| 464 | { |
||
| 465 | if ($accountRequest->changePermissions) { |
||
| 466 | if ($accountRequest->updateUserGroupPermissions) { |
||
| 467 | if (!empty($accountRequest->userGroupsView)) { |
||
| 468 | $this->accountToUserGroupRepository->update($accountRequest); |
||
| 469 | } else { |
||
| 470 | $this->accountToUserGroupRepository->deleteByAccountId($accountRequest->id); |
||
| 471 | } |
||
| 472 | |||
| 473 | if (!empty($accountRequest->userGroupsEdit)) { |
||
| 474 | $this->accountToUserGroupRepository->updateEdit($accountRequest); |
||
| 475 | } else { |
||
| 476 | $this->accountToUserGroupRepository->deleteEditByAccountId($accountRequest->id); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | if ($accountRequest->updateUserPermissions) { |
||
| 481 | if (!empty($accountRequest->usersView)) { |
||
| 482 | $this->accountToUserRepository->update($accountRequest); |
||
| 483 | } else { |
||
| 484 | $this->accountToUserRepository->deleteByAccountId($accountRequest->id); |
||
| 485 | } |
||
| 486 | |||
| 487 | if (!empty($accountRequest->usersEdit)) { |
||
| 488 | $this->accountToUserRepository->updateEdit($accountRequest); |
||
| 489 | } else { |
||
| 490 | $this->accountToUserRepository->deleteEditByAccountId($accountRequest->id); |
||
| 491 | } |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | if ($accountRequest->updateTags) { |
||
| 496 | if (!empty($accountRequest->tags)) { |
||
| 497 | $this->accountToTagRepository->update($accountRequest); |
||
| 498 | } else { |
||
| 499 | $this->accountToTagRepository->deleteByAccountId($accountRequest->id); |
||
| 500 | } |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param AccountRequest $accountRequest |
||
| 506 | * |
||
| 507 | * @throws \Exception |
||
| 508 | */ |
||
| 509 | public function editPassword(AccountRequest $accountRequest) |
||
| 520 | }); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Updates an already encrypted password data from a master password changing action |
||
| 525 | * |
||
| 526 | * @param AccountPasswordRequest $accountRequest |
||
| 527 | * |
||
| 528 | * @return bool |
||
| 529 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 530 | * @throws QueryException |
||
| 531 | */ |
||
| 532 | public function updatePasswordMasterPass(AccountPasswordRequest $accountRequest) |
||
| 533 | { |
||
| 534 | return $this->accountRepository->updatePassword($accountRequest); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param $historyId |
||
| 539 | * @param $accountId |
||
| 540 | * |
||
| 541 | * @throws \Exception |
||
| 542 | */ |
||
| 543 | public function editRestore($historyId, $accountId) |
||
| 544 | { |
||
| 545 | $this->transactionAware(function () use ($historyId, $accountId) { |
||
| 546 | $this->addHistory($accountId); |
||
| 547 | |||
| 548 | if (!$this->accountRepository->editRestore($historyId, $this->context->getUserData()->getId())) { |
||
| 549 | throw new ServiceException(__u('Error al restaurar cuenta')); |
||
| 550 | } |
||
| 551 | }); |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param $id |
||
| 556 | * |
||
| 557 | * @return AccountService |
||
| 558 | * @throws ServiceException |
||
| 559 | */ |
||
| 560 | public function delete($id) |
||
| 561 | { |
||
| 562 | $this->transactionAware(function () use ($id) { |
||
| 563 | $this->addHistory($id, 1); |
||
| 564 | |||
| 565 | if ($this->accountRepository->delete($id) === 0) { |
||
| 566 | throw new NoSuchItemException(__u('Cuenta no encontrada')); |
||
| 567 | } |
||
| 568 | }); |
||
| 569 | |||
| 570 | return $this; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param array $ids |
||
| 575 | * |
||
| 576 | * @return AccountService |
||
| 577 | * @throws SPException |
||
| 578 | * @throws ServiceException |
||
| 579 | */ |
||
| 580 | public function deleteByIdBatch(array $ids) |
||
| 581 | { |
||
| 582 | if ($this->accountRepository->deleteByIdBatch($ids) === 0) { |
||
| 583 | throw new ServiceException(__u('Error al eliminar las cuentas')); |
||
| 584 | } |
||
| 585 | |||
| 586 | return $this; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param $accountId |
||
| 591 | * |
||
| 592 | * @return array |
||
| 593 | * @throws QueryException |
||
| 594 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 595 | */ |
||
| 596 | public function getForUser($accountId = null) |
||
| 597 | { |
||
| 598 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter(); |
||
| 599 | |||
| 600 | if (null !== $accountId) { |
||
| 601 | $queryFilter->addFilter('Account.id <> ? AND (Account.parentId = 0 OR Account.parentId IS NULL)', [$accountId]); |
||
| 602 | } |
||
| 603 | |||
| 604 | return $this->accountRepository->getForUser($queryFilter)->getDataAsArray(); |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param $accountId |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | * @throws QueryException |
||
| 612 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 613 | */ |
||
| 614 | public function getLinked($accountId) |
||
| 615 | { |
||
| 616 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter(); |
||
| 617 | |||
| 618 | $queryFilter->addFilter('Account.parentId = ?', [$accountId]); |
||
| 619 | |||
| 620 | return $this->accountRepository->getLinked($queryFilter)->getDataAsArray(); |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @param $id |
||
| 625 | * |
||
| 626 | * @return AccountPassData |
||
| 627 | * @throws QueryException |
||
| 628 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 629 | * @throws NoSuchItemException |
||
| 630 | */ |
||
| 631 | public function getPasswordHistoryForId($id) |
||
| 632 | { |
||
| 633 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilterHistory(); |
||
| 634 | $queryFilter->addFilter('AccountHistory.id = ?', [$id]); |
||
| 635 | |||
| 636 | $result = $this->accountRepository->getPasswordHistoryForId($queryFilter); |
||
| 637 | |||
| 638 | if ($result->getNumRows() === 0) { |
||
| 639 | throw new NoSuchItemException(__u('La cuenta no existe')); |
||
| 640 | } |
||
| 641 | |||
| 642 | return $result->getData(); |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return AccountData[] |
||
| 647 | * @throws QueryException |
||
| 648 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 649 | */ |
||
| 650 | public function getAllBasic() |
||
| 651 | { |
||
| 652 | return $this->accountRepository->getAll()->getDataAsArray(); |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param ItemSearchData $itemSearchData |
||
| 657 | * |
||
| 658 | * @return QueryResult |
||
| 659 | * @throws QueryException |
||
| 660 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 661 | */ |
||
| 662 | public function search(ItemSearchData $itemSearchData) |
||
| 663 | { |
||
| 664 | return $this->accountRepository->search($itemSearchData); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Devolver el número total de cuentas |
||
| 669 | * |
||
| 670 | * @return \stdClass |
||
| 671 | * @throws QueryException |
||
| 672 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 673 | */ |
||
| 674 | public function getTotalNumAccounts() |
||
| 675 | { |
||
| 676 | return $this->accountRepository->getTotalNumAccounts()->num; |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Obtener los datos de una cuenta. |
||
| 681 | * |
||
| 682 | * @param $id |
||
| 683 | * |
||
| 684 | * @return \SP\DataModel\AccountExtData |
||
| 685 | * @throws QueryException |
||
| 686 | * @throws \SP\Repositories\NoSuchItemException |
||
| 687 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 688 | */ |
||
| 689 | public function getDataForLink($id) |
||
| 690 | { |
||
| 691 | $result = $this->accountRepository->getDataForLink($id); |
||
| 692 | |||
| 693 | if ($result->getNumRows() === 0) { |
||
| 694 | throw new NoSuchItemException(__u('La cuenta no existe')); |
||
| 695 | } |
||
| 696 | |||
| 697 | return $result->getData(); |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Obtener los datos relativos a la clave de todas las cuentas. |
||
| 702 | * |
||
| 703 | * @return array Con los datos de la clave |
||
| 704 | * @throws QueryException |
||
| 705 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 706 | */ |
||
| 707 | public function getAccountsPassData() |
||
| 708 | { |
||
| 709 | return $this->accountRepository->getAccountsPassData(); |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Obtener las cuentas de una búsqueda. |
||
| 714 | * |
||
| 715 | * @param AccountSearchFilter $accountSearchFilter |
||
| 716 | * |
||
| 717 | * @return AccountSearchVData[] |
||
| 718 | * @throws QueryException |
||
| 719 | * @throws SPException |
||
| 720 | * @throws \SP\Core\Exceptions\ConstraintException |
||
| 721 | */ |
||
| 722 | public function getByFilter(AccountSearchFilter $accountSearchFilter) |
||
| 723 | { |
||
| 724 | $accountFilterUser = $this->dic->get(AccountFilterUser::class); |
||
| 725 | |||
| 726 | return $this->accountRepository->getByFilter( |
||
| 727 | $accountSearchFilter, |
||
| 728 | $accountFilterUser->getFilter($accountSearchFilter->getGlobalSearch()) |
||
| 729 | )->getDataAsArray(); |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @throws \Psr\Container\ContainerExceptionInterface |
||
| 734 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 735 | */ |
||
| 736 | protected function initialize() |
||
| 743 | } |
||
| 744 | } |