Skip to content

Commit

Permalink
Merge pull request #1647 from ConductionNL/fix/WOO-45/applicationServ…
Browse files Browse the repository at this point in the history
…ice-public-host

Add more if statements to prevent exceptions we can't catch somehow
  • Loading branch information
WilcoLouwerse committed May 7, 2024
2 parents 9813b74 + c5fe89f commit b3fb6a4
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions api/src/Service/ApplicationService.php
Expand Up @@ -6,6 +6,7 @@
use App\Exception\GatewayException;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Expand Down Expand Up @@ -37,15 +38,15 @@ public function __construct(
public function getApplication(): Application
{
// If application is already in the session
if ($this->session->has('application')) {
if (empty($this->session) === false && $this->session->has('application') === true) {
$application = $this->entityManager->getRepository('App:Application')->findOneBy(['id' => $this->session->get('application')]);
if ($application !== null) {
return $application;
}
}

// Find application using the publicKey
$public = ($this->request->headers->get('public') ?? $this->request->query->get('public'));
$public = $this->getHeaderOrQuery('public');
if (empty($public) === false) {
$application = $this->entityManager->getRepository('App:Application')->findOneBy(['public' => $public]);
if ($application !== null) {
Expand All @@ -55,7 +56,7 @@ public function getApplication(): Application
}

// Find application using the host/domain
$host = ($this->request->headers->get('host') ?? $this->request->query->get('host'));
$host = $this->getHeaderOrQuery('host');
if (empty($host) === false) {
$applications = $this->entityManager->getRepository('App:Application')->findByDomain($host);
if (count($applications) > 0) {
Expand All @@ -81,4 +82,29 @@ public function getApplication(): Application
'data' => $data ?? null, 'path' => $public ?? $host ?? 'Header', 'responseType' => Response::HTTP_FORBIDDEN,
]);
}


/**
* Tries to get a given key from the headers of the current request, else from the query params of the current request.
*
* @param string $key The key to get.
*
* @return bool|float|int|string|InputBag|null The value of the header or query or null if none was found.
*/
private function getHeaderOrQuery(string $key)
{
if (empty($this->request) === true) {
return null;
}

if (empty($this->request->headers) === false && $this->request->headers->has($key) === true) {
return $this->request->headers->get($key);
}

if (empty($this->request->query) === false && $this->request->query->has($key) === true) {
return $this->request->query->get($key);
}

return null;
}
}

0 comments on commit b3fb6a4

Please sign in to comment.