diff --git a/src/Message/CacheMessage.php b/src/Message/CacheMessage.php index f4e6a1e7..70ad33ff 100644 --- a/src/Message/CacheMessage.php +++ b/src/Message/CacheMessage.php @@ -15,7 +15,7 @@ class CacheMessage private UuidInterface $objectEntityId; - public function __construct(UuidInterface $actionId) + public function __construct(UuidInterface $actionId, private readonly string $application) { $this->objectEntityId = $actionId; @@ -26,4 +26,10 @@ public function getObjectEntityId(): UuidInterface return $this->objectEntityId; }//end getObjectEntityId() + + public function getApplication(): ?string + { + return $this->application; + + }//end getApplication() }//end class diff --git a/src/Message/ValueMessage.php b/src/Message/ValueMessage.php index 965eee8a..defc6f81 100644 --- a/src/Message/ValueMessage.php +++ b/src/Message/ValueMessage.php @@ -29,7 +29,7 @@ class ValueMessage * * @param UuidInterface $valueId The id of the value to check./ */ - public function __construct(UuidInterface $valueId, ?UuidInterface $userId) + public function __construct(UuidInterface $valueId, ?UuidInterface $userId, private readonly string $application) { $this->valueId = $valueId; $this->userId = $userId; @@ -57,4 +57,10 @@ public function getUserId(): ?UuidInterface return $this->userId; }//end getUserId() + + public function getApplication(): string + { + return $this->application; + + }//end getApplication() }//end class diff --git a/src/MessageHandler/CacheMessageHandler.php b/src/MessageHandler/CacheMessageHandler.php index 8ee788bf..5dbf0625 100644 --- a/src/MessageHandler/CacheMessageHandler.php +++ b/src/MessageHandler/CacheMessageHandler.php @@ -48,6 +48,10 @@ public function __invoke(CacheMessage $message): void { $object = $this->repository->find($message->getObjectEntityId()); + if ($message->getApplication() !== null) { + $this->session->set('application', $message->getApplication()); + } + try { if ($object instanceof ObjectEntity) { $this->cacheService->cacheObject($object); diff --git a/src/MessageHandler/ValueMessageHandler.php b/src/MessageHandler/ValueMessageHandler.php index 9bf57e53..0f7cfd61 100644 --- a/src/MessageHandler/ValueMessageHandler.php +++ b/src/MessageHandler/ValueMessageHandler.php @@ -87,6 +87,10 @@ public function __invoke(ValueMessage $message): void $this->session->set('valueMessageUserId', $message->getUserId()->toString()); } + if ($message->getApplication() !== null) { + $this->session->set('application', $message->getApplication()); + } + try { if ($value instanceof Value === true) { $this->valueService->connectSubObjects($value); diff --git a/src/Service/CacheService.php b/src/Service/CacheService.php index 04148e4e..f0365ec3 100644 --- a/src/Service/CacheService.php +++ b/src/Service/CacheService.php @@ -444,6 +444,8 @@ private function removeDataFromCache(Collection $collection, string $type, array $filter['_self.schema.id']['$in'][] = $schemaRef; } + $filter['_limit'] = $collection->count($filter); + $objects = $collection->find($filter, [])->toArray(); } else { $objects = $collection->find()->toArray(); diff --git a/src/Service/ObjectEntityService.php b/src/Service/ObjectEntityService.php index 66e41785..4de0aaff 100644 --- a/src/Service/ObjectEntityService.php +++ b/src/Service/ObjectEntityService.php @@ -5,6 +5,7 @@ use App\Entity\ObjectEntity; use App\Entity\Organization; use App\Entity\User; +use App\Service\ApplicationService; use Doctrine\ORM\EntityManagerInterface; use Ramsey\Uuid\Uuid; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -63,7 +64,8 @@ class ObjectEntityService public function __construct( EntityManagerInterface $entityManager, Security $security, - SessionInterface $session + SessionInterface $session, + private readonly ApplicationService $applicationService ) { $this->entityManager = $entityManager; $this->security = $security; @@ -145,6 +147,8 @@ private function setOwner(ObjectEntity $object, ?User $user): ObjectEntity return $object; } + $application = $this->applicationService->getApplication(); + // Find the correct owner to set. if ($user !== null) { $owner = $user->getId()->toString(); @@ -175,9 +179,13 @@ private function setOrganization(ObjectEntity $object, ?User $user): ObjectEntit return $object; } + $application = $this->applicationService->getApplication(); + // Find the correct Organization to set. if ($user !== null && $user->getOrganization() !== null) { $organization = $user->getOrganization(); + } else if ($application !== null) { + $organization = $application->getOrganization(); } else { // Default to the Default Organization. $organization = $this->entityManager->getRepository('App:Organization')->findOneBy(['reference' => $this::DEFAULTS['organization']]); diff --git a/src/Subscriber/ActionSubscriber.php b/src/Subscriber/ActionSubscriber.php index 66f6a928..fa8455f2 100644 --- a/src/Subscriber/ActionSubscriber.php +++ b/src/Subscriber/ActionSubscriber.php @@ -193,7 +193,7 @@ public function handleAction(Action $action, ActionEvent $event): ActionEvent } else { $data = $event->getData(); unset($data['httpRequest']); - $this->messageBus->dispatch(new ActionMessage($action->getId(), $data, $currentCronJobThrow)); + $this->messageBus->dispatch(new ActionMessage($action->getId(), $data, $currentCronJobThrow, $this->session->get('application'))); } $this->handleActionIoFinish($action, $currentCronJobThrow); diff --git a/src/Subscriber/ObjectUriSubscriber.php b/src/Subscriber/ObjectUriSubscriber.php index 5e21a5e6..7179b58e 100644 --- a/src/Subscriber/ObjectUriSubscriber.php +++ b/src/Subscriber/ObjectUriSubscriber.php @@ -4,6 +4,8 @@ namespace CommonGateway\CoreBundle\Subscriber; use App\Entity\ObjectEntity; +use App\Exception\GatewayException; +use App\Service\ApplicationService; use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; use Doctrine\ORM\Events; use Doctrine\Persistence\Event\LifecycleEventArgs; @@ -40,7 +42,8 @@ class ObjectUriSubscriber implements EventSubscriberInterface */ public function __construct( ParameterBagInterface $parameterBag, - SessionInterface $session + SessionInterface $session, + private ApplicationService $applicationService ) { $this->parameterBag = $parameterBag; $this->session = $session; @@ -80,8 +83,19 @@ public function postPersist(LifecycleEventArgs $args): void $object = $args->getObject(); // if this subscriber only applies to certain entity types, if ($object instanceof ObjectEntity) { - if ($object->getUri() === null || str_contains($object->getUri(), $object->getSelf()) === false) { - $object->setUri(rtrim($this->parameterBag->get('app_url'), '/').$object->getSelf()); + try { + $application = $this->applicationService->getApplication(); + if ($object->getUri() === null + || str_contains($object->getUri(), $object->getSelf()) === false + ) { + $object->setUri('https://'.$application->getDomains()[0].$object->getSelf()); + } + } catch (GatewayException $exception) { + if ($object->getUri() === null + || str_contains($object->getUri(), $object->getSelf()) === false + ) { + $object->setUri(rtrim($this->parameterBag->get('app_url'), '/').$object->getSelf()); + } } return; diff --git a/src/Subscriber/ValueSubscriber.php b/src/Subscriber/ValueSubscriber.php index f7a89e2c..0a9e0209 100644 --- a/src/Subscriber/ValueSubscriber.php +++ b/src/Subscriber/ValueSubscriber.php @@ -96,7 +96,7 @@ public function postUpdate(LifecycleEventArgs $value): void $userId = Uuid::fromString($this->session->get('user')); } - $this->messageBus->dispatch(new ValueMessage($value->getObject()->getId(), $userId)); + $this->messageBus->dispatch(new ValueMessage($value->getObject()->getId(), $userId, $this->session->get('application'))); } catch (\Exception $exception) { $this->logger->error("Error when trying to create a ValueMessage for Value {$value->getObject()->getId()}: ".$exception->getMessage()); }