. */ namespace SP\Domain\Auth\Services; use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Auth\Ports\AuthTokenService; use SP\Domain\Auth\Ports\UpgradeAuthTokenService; use SP\Domain\Common\Services\Service; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Infrastructure\Database\DatabaseInterface; use function SP\__u; use function SP\processException; /** * Class UpgradeAuthToken */ final class UpgradeAuthToken extends Service implements UpgradeAuthTokenService { public function __construct( Application $application, protected readonly AuthTokenService $authTokenService, private readonly DatabaseInterface $database ) { parent::__construct($application); } /** * upgrade_300_18072901 * * @throws Exception */ public function upgradeV300B18072901(): void { $this->eventDispatcher->notify( 'upgrade.authToken.start', new Event( $this, EventMessage::factory() ->addDescription(__u('API authorizations update')) ->addDescription(__FUNCTION__) ) ); try { $this->transactionAware( function () { foreach ($this->authTokenService->getAll() as $item) { $itemData = clone $item; $itemData->setActionId($this->actionMapper($item->getActionId())); $this->authTokenService->updateRaw($itemData); $this->eventDispatcher->notify( 'upgrade.authToken.process', new Event( $this, EventMessage::factory() ->addDescription(__u('Authorization updated')) ->addDetail(__u('Authorization'), $item->getId()) ) ); } }, $this->database ); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); throw $e; } $this->eventDispatcher->notify( 'upgrade.authToken.end', new Event( $this, EventMessage::factory() ->addDescription(__u('API authorizations update')) ->addDescription(__FUNCTION__) ) ); } private function actionMapper(int $moduleId): int { return match ($moduleId) { 1 => AclActionsInterface::ACCOUNT_SEARCH, 100 => AclActionsInterface::ACCOUNT_VIEW, 104 => AclActionsInterface::ACCOUNT_VIEW_PASS, 103 => AclActionsInterface::ACCOUNT_DELETE, 101 => AclActionsInterface::ACCOUNT_CREATE, 615 => AclActionsInterface::CATEGORY_SEARCH, 610 => AclActionsInterface::CATEGORY_VIEW, 611 => AclActionsInterface::CATEGORY_CREATE, 612 => AclActionsInterface::CATEGORY_EDIT, 613 => AclActionsInterface::CATEGORY_DELETE, 625 => AclActionsInterface::CLIENT_SEARCH, 620 => AclActionsInterface::CLIENT_VIEW, 621 => AclActionsInterface::CLIENT_CREATE, 622 => AclActionsInterface::CLIENT_EDIT, 623 => AclActionsInterface::CLIENT_DELETE, 685 => AclActionsInterface::TAG_SEARCH, 681 => AclActionsInterface::TAG_VIEW, 680 => AclActionsInterface::TAG_CREATE, 682 => AclActionsInterface::TAG_EDIT, 683 => AclActionsInterface::TAG_DELETE, 1041 => AclActionsInterface::CONFIG_BACKUP_RUN, 1061 => AclActionsInterface::CONFIG_EXPORT_RUN, default => $moduleId, }; } }