factory = $factory; $this->config = $config; $this->acl = $acl; $this->user = $user; $this->metadata = $metadata; $this->entityManager = $entityManager; $this->jobSchedulerFactory = $jobSchedulerFactory; } public function process(Params $params, ServiceParams $serviceParams): ServiceResult { if ($this->config->get('exportDisabled') && !$this->user->isAdmin()) { throw new ForbiddenSilent("Export disabled for non-admin users."); } $entityType = $params->getEntityType(); if ($this->acl->getPermissionLevel('exportPermission') !== Table::LEVEL_YES) { throw new ForbiddenSilent("No 'export' permission."); } if (!$this->acl->check($entityType, Table::ACTION_READ)) { throw new ForbiddenSilent("No 'read' access."); } if ($this->metadata->get(['recordDefs', $entityType, 'exportDisabled'])) { throw new ForbiddenSilent("Export disabled for '{$entityType}'."); } if ($serviceParams->isIdle()) { if ($this->user->isPortal()) { throw new ForbiddenSilent("Idle export is not allowed for portal users."); } return $this->schedule($params); } $export = $this->factory->create(); $result = $export ->setParams($params) ->run(); return ServiceResult::createWithResult($result); } public function getStatusData(string $id): stdClass { /** @var ExportEntity|null $entity */ $entity = $this->entityManager->getEntity(ExportEntity::ENTITY_TYPE, $id); if (!$entity) { throw new NotFoundSilent(); } if ($entity->getCreatedBy()->getId() !== $this->user->getId()) { throw new ForbiddenSilent(); } return (object) [ 'status' => $entity->getStatus(), 'attachmentId' => $entity->getAttachmentId(), ]; } public function subscribeToNotificationOnSuccess(string $id): void { /** @var ExportEntity|null $entity */ $entity = $this->entityManager->getEntity(ExportEntity::ENTITY_TYPE, $id); if (!$entity) { throw new NotFoundSilent(); } if ($entity->getCreatedBy()->getId() !== $this->user->getId()) { throw new ForbiddenSilent(); } $entity->setNotifyOnFinish(); $this->entityManager->saveEntity($entity); } private function schedule(Params $params): ServiceResult { $entity = $this->entityManager->createEntity(ExportEntity::ENTITY_TYPE, [ 'params' => serialize($params), ]); $this->jobSchedulerFactory ->create() ->setClassName(Process::class) ->setData( JobData::create() ->withTargetId($entity->getId()) ->withTargetType($entity->getEntityType()) ) ->schedule(); return ServiceResult::createWithId($entity->getId()); } }