. */ namespace SP\Domain\CustomField\Services; use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Crypt\CryptInterface; use SP\Domain\Crypt\Dtos\UpdateMasterPassRequest; use SP\Domain\CustomField\Models\CustomFieldData as CustomFieldDataModel; use SP\Domain\CustomField\Ports\CustomFieldCryptService; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\Task\Services\TaskFactory; use function SP\__; use function SP\__u; use function SP\processException; /** * Class CustomFieldCrypt */ final class CustomFieldCrypt extends Service implements CustomFieldCryptService { public function __construct( Application $application, private readonly CustomFieldDataService $customFieldService, private readonly CryptInterface $crypt ) { parent::__construct($application); } /** * Actualizar los datos encriptados con una nueva clave * * @throws ServiceException */ public function updateMasterPassword(UpdateMasterPassRequest $request): void { try { $this->processUpdateMasterPassword( $request, function (CustomFieldDataModel $customFieldData) use ($request) { return $this->crypt->decrypt( $customFieldData->getData(), $customFieldData->getKey(), $request->getCurrentMasterPass() ); } ); } catch (Exception $e) { $this->eventDispatcher->notify('exception', new Event($e)); throw ServiceException::error( __u('Error while updating the custom fields data'), null, $e->getCode(), $e ); } } /** * @param UpdateMasterPassRequest $request * @param callable $decryptor * @throws ServiceException */ private function processUpdateMasterPassword(UpdateMasterPassRequest $request, callable $decryptor): void { $customFieldsData = $this->customFieldService->getAllEncrypted(); if (count($customFieldsData) === 0) { $this->eventDispatcher->notify( 'update.masterPassword.customFieldsData', new Event( $this, EventMessage::factory() ->addDescription(__u('Update Master Password')) ->addDescription(__u('There aren\'t any data from custom fields')) ) ); return; } $this->eventDispatcher->notify( 'update.masterPassword.customFieldsData.start', new Event( $this, EventMessage::factory() ->addDescription(__u('Update Master Password')) ->addDescription(__u('Updating encrypted data')) ) ); if ($request->useTask()) { $task = $request->getTask(); TaskFactory::update( $task, TaskFactory::createMessage( $task->getTaskId(), __('Update Master Password') )->setMessage(__('Updating encrypted data')) ); } $errors = []; $success = []; foreach ($customFieldsData as $customFieldData) { try { $this->customFieldService->updateMasterPass( $customFieldData->mutate(['data' => $decryptor($customFieldData)]), $request->getNewMasterPass() ); $success[] = $customFieldData->getItemId(); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); $errors[] = $customFieldData->getItemId(); } } $this->eventDispatcher->notify( 'update.masterPassword.customFieldsData.end', new Event( $this, EventMessage::factory() ->addDescription(__u('Update Master Password')) ->addDetail(__u('Records updated'), implode(',', $success)) ->addDetail(__u('Records not updated'), implode(',', $errors)) ) ); } }