Skip to content

Commit d3a833d

Browse files
authored
Merge pull request #616 from CommonGateway/feature/multitenancy-fixes
Fix multitenancy bugs
2 parents ee64502 + f027fbf commit d3a833d

9 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/Message/CacheMessage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CacheMessage
1515

1616
private UuidInterface $objectEntityId;
1717

18-
public function __construct(UuidInterface $actionId)
18+
public function __construct(UuidInterface $actionId, private readonly string $application)
1919
{
2020
$this->objectEntityId = $actionId;
2121

@@ -26,4 +26,10 @@ public function getObjectEntityId(): UuidInterface
2626
return $this->objectEntityId;
2727

2828
}//end getObjectEntityId()
29+
30+
public function getApplication(): ?string
31+
{
32+
return $this->application;
33+
34+
}//end getApplication()
2935
}//end class

src/Message/ValueMessage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ValueMessage
2929
*
3030
* @param UuidInterface $valueId The id of the value to check./
3131
*/
32-
public function __construct(UuidInterface $valueId, ?UuidInterface $userId)
32+
public function __construct(UuidInterface $valueId, ?UuidInterface $userId, private readonly string $application)
3333
{
3434
$this->valueId = $valueId;
3535
$this->userId = $userId;
@@ -57,4 +57,10 @@ public function getUserId(): ?UuidInterface
5757
return $this->userId;
5858

5959
}//end getUserId()
60+
61+
public function getApplication(): string
62+
{
63+
return $this->application;
64+
65+
}//end getApplication()
6066
}//end class

src/MessageHandler/CacheMessageHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function __invoke(CacheMessage $message): void
4848
{
4949
$object = $this->repository->find($message->getObjectEntityId());
5050

51+
if ($message->getApplication() !== null) {
52+
$this->session->set('application', $message->getApplication());
53+
}
54+
5155
try {
5256
if ($object instanceof ObjectEntity) {
5357
$this->cacheService->cacheObject($object);

src/MessageHandler/ValueMessageHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public function __invoke(ValueMessage $message): void
8787
$this->session->set('valueMessageUserId', $message->getUserId()->toString());
8888
}
8989

90+
if ($message->getApplication() !== null) {
91+
$this->session->set('application', $message->getApplication());
92+
}
93+
9094
try {
9195
if ($value instanceof Value === true) {
9296
$this->valueService->connectSubObjects($value);

src/Service/CacheService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ private function removeDataFromCache(Collection $collection, string $type, array
444444
$filter['_self.schema.id']['$in'][] = $schemaRef;
445445
}
446446

447+
$filter['_limit'] = $collection->count($filter);
448+
447449
$objects = $collection->find($filter, [])->toArray();
448450
} else {
449451
$objects = $collection->find()->toArray();

src/Service/ObjectEntityService.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Entity\ObjectEntity;
66
use App\Entity\Organization;
77
use App\Entity\User;
8+
use App\Service\ApplicationService;
89
use Doctrine\ORM\EntityManagerInterface;
910
use Ramsey\Uuid\Uuid;
1011
use Symfony\Component\HttpFoundation\Session\SessionInterface;
@@ -63,7 +64,8 @@ class ObjectEntityService
6364
public function __construct(
6465
EntityManagerInterface $entityManager,
6566
Security $security,
66-
SessionInterface $session
67+
SessionInterface $session,
68+
private readonly ApplicationService $applicationService
6769
) {
6870
$this->entityManager = $entityManager;
6971
$this->security = $security;
@@ -145,6 +147,8 @@ private function setOwner(ObjectEntity $object, ?User $user): ObjectEntity
145147
return $object;
146148
}
147149

150+
$application = $this->applicationService->getApplication();
151+
148152
// Find the correct owner to set.
149153
if ($user !== null) {
150154
$owner = $user->getId()->toString();
@@ -175,9 +179,13 @@ private function setOrganization(ObjectEntity $object, ?User $user): ObjectEntit
175179
return $object;
176180
}
177181

182+
$application = $this->applicationService->getApplication();
183+
178184
// Find the correct Organization to set.
179185
if ($user !== null && $user->getOrganization() !== null) {
180186
$organization = $user->getOrganization();
187+
} else if ($application !== null) {
188+
$organization = $application->getOrganization();
181189
} else {
182190
// Default to the Default Organization.
183191
$organization = $this->entityManager->getRepository('App:Organization')->findOneBy(['reference' => $this::DEFAULTS['organization']]);

src/Subscriber/ActionSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function handleAction(Action $action, ActionEvent $event): ActionEvent
193193
} else {
194194
$data = $event->getData();
195195
unset($data['httpRequest']);
196-
$this->messageBus->dispatch(new ActionMessage($action->getId(), $data, $currentCronJobThrow));
196+
$this->messageBus->dispatch(new ActionMessage($action->getId(), $data, $currentCronJobThrow, $this->session->get('application')));
197197
}
198198

199199
$this->handleActionIoFinish($action, $currentCronJobThrow);

src/Subscriber/ObjectUriSubscriber.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace CommonGateway\CoreBundle\Subscriber;
55

66
use App\Entity\ObjectEntity;
7+
use App\Exception\GatewayException;
8+
use App\Service\ApplicationService;
79
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
810
use Doctrine\ORM\Events;
911
use Doctrine\Persistence\Event\LifecycleEventArgs;
@@ -40,7 +42,8 @@ class ObjectUriSubscriber implements EventSubscriberInterface
4042
*/
4143
public function __construct(
4244
ParameterBagInterface $parameterBag,
43-
SessionInterface $session
45+
SessionInterface $session,
46+
private ApplicationService $applicationService
4447
) {
4548
$this->parameterBag = $parameterBag;
4649
$this->session = $session;
@@ -80,8 +83,19 @@ public function postPersist(LifecycleEventArgs $args): void
8083
$object = $args->getObject();
8184
// if this subscriber only applies to certain entity types,
8285
if ($object instanceof ObjectEntity) {
83-
if ($object->getUri() === null || str_contains($object->getUri(), $object->getSelf()) === false) {
84-
$object->setUri(rtrim($this->parameterBag->get('app_url'), '/').$object->getSelf());
86+
try {
87+
$application = $this->applicationService->getApplication();
88+
if ($object->getUri() === null
89+
|| str_contains($object->getUri(), $object->getSelf()) === false
90+
) {
91+
$object->setUri('https://'.$application->getDomains()[0].$object->getSelf());
92+
}
93+
} catch (GatewayException $exception) {
94+
if ($object->getUri() === null
95+
|| str_contains($object->getUri(), $object->getSelf()) === false
96+
) {
97+
$object->setUri(rtrim($this->parameterBag->get('app_url'), '/').$object->getSelf());
98+
}
8599
}
86100

87101
return;

src/Subscriber/ValueSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function postUpdate(LifecycleEventArgs $value): void
9696
$userId = Uuid::fromString($this->session->get('user'));
9797
}
9898

99-
$this->messageBus->dispatch(new ValueMessage($value->getObject()->getId(), $userId));
99+
$this->messageBus->dispatch(new ValueMessage($value->getObject()->getId(), $userId, $this->session->get('application')));
100100
} catch (\Exception $exception) {
101101
$this->logger->error("Error when trying to create a ValueMessage for Value {$value->getObject()->getId()}: ".$exception->getMessage());
102102
}

0 commit comments

Comments
 (0)