#4976 -- Alert users when base URL mismatches.

This commit is contained in:
Buster "Silver Eagle" Neece 2022-01-14 04:19:55 -06:00
parent fccb33b20b
commit 09d79b763f
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
5 changed files with 90 additions and 10 deletions

View File

@ -150,6 +150,10 @@ return function (CallableEventDispatcherInterface $dispatcher) {
);
// Other event subscribers from across the application.
$dispatcher->addCallableListener(
Event\GetNotifications::class,
App\Notification\Check\BaseUrlCheck::class
);
$dispatcher->addCallableListener(
Event\GetNotifications::class,
App\Notification\Check\ComposeVersionCheck::class

View File

@ -32,11 +32,12 @@ class PlayerAction
throw new StationNotFoundException();
}
$baseUrl = $request->getRouter()->getBaseUrl();
$np = $npApiGenerator->currentOrEmpty($station);
$np->resolveUrls($request->getRouter()->getBaseUrl());
$np->resolveUrls($baseUrl);
$defaultAlbumArtUri = $stationRepo->getDefaultAlbumArtUrl($station);
$baseUrl = $request->getRouter()->getBaseUrl();
$defaultAlbumArt = Router::resolveUri($baseUrl, $defaultAlbumArtUri, true);
$autoplay = !empty($request->getQueryParam('autoplay'));

View File

@ -35,16 +35,21 @@ class Router implements RouterInterface
$this->baseUrl = null;
}
public function getBaseUrl(bool $useRequest = true): UriInterface
public function getBaseUrl(?bool $useRequest = null): UriInterface
{
if (null === $this->baseUrl) {
$this->baseUrl = $this->buildBaseUrl();
if (null === $useRequest) {
if (null === $this->baseUrl) {
$settings = $this->settingsRepo->readSettings();
$this->baseUrl = $this->buildBaseUrl($settings->getPreferBrowserUrl());
}
return $this->baseUrl;
}
return $this->baseUrl;
return $this->buildBaseUrl($useRequest);
}
protected function buildBaseUrl(): UriInterface
protected function buildBaseUrl(bool $useRequest = true): UriInterface
{
$settings = $this->settingsRepo->readSettings();
@ -58,8 +63,7 @@ class Router implements RouterInterface
$useHttps = true;
}
$preferBrowserUrl = $settings->getPreferBrowserUrl();
if ($preferBrowserUrl || $baseUrl->getHost() === '') {
if ($useRequest || $baseUrl->getHost() === '') {
$ignoredHosts = ['web', 'nginx', 'localhost'];
if (!in_array($currentUri->getHost(), $ignoredHosts, true)) {
$baseUrl = (new Uri())

View File

@ -13,7 +13,7 @@ interface RouterInterface
public function withRequest(?ServerRequestInterface $request): self;
public function getBaseUrl(): UriInterface;
public function getBaseUrl(?bool $useRequest = null): UriInterface;
/**
* Simpler format for calling "named" routes with parameters.

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Notification\Check;
use App\Entity\Api\Notification;
use App\Entity\Repository\SettingsRepository;
use App\Enums\GlobalPermissions;
use App\Environment;
use App\Event\GetNotifications;
use App\Session\Flash;
use App\Utilities\Strings;
use App\Version;
class BaseUrlCheck
{
public function __construct(
protected SettingsRepository $settingsRepo,
protected Environment $environment
) {
}
public function __invoke(GetNotifications $event): void
{
// This notification is for full administrators only.
$request = $event->getRequest();
$router = $request->getRouter();
$acl = $request->getAcl();
if (!$acl->isAllowed(GlobalPermissions::Settings)) {
return;
}
$settings = $this->settingsRepo->readSettings();
// Base URL mismatch doesn't happen if this setting is enabled.
if ($settings->getPreferBrowserUrl()) {
return;
}
$baseUriWithRequest = $router->getBaseUrl(true);
$baseUriWithoutRequest = $router->getBaseUrl(false);
if ((string)$baseUriWithoutRequest !== (string)$baseUriWithRequest) {
// phpcs:disable Generic.Files.LineLength
$notificationBodyParts = [];
$notificationBodyParts[] = __(
'You may want to update your base URL to ensure it is correct.'
);
$notificationBodyParts[] = __(
'If you regularly use different URLs to access AzuraCast, you should enable the "Prefer Browser URL" setting.'
);
// phpcs:enable Generic.Files.LineLength
$notification = new Notification();
$notification->title = __(
'Your "Base URL" setting (%s) does not match the URL you are currently using (%s).',
Strings::truncateUrl((string)$baseUriWithoutRequest),
Strings::truncateUrl((string)$baseUriWithRequest)
);
$notification->body = implode(' ', $notificationBodyParts);
$notification->type = Flash::WARNING;
$notification->actionLabel = __('System Settings');
$notification->actionUrl = (string)$router->named('admin:settings:index');
$event->addNotification($notification);
}
}
}