Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve detection of map provider with missing Api key #4760

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions app/Module/BingMaps.php
Expand Up @@ -19,11 +19,8 @@

namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -111,16 +108,6 @@ public function leafletJsTileLayers(): array
{
$api_key = $this->getPreference('api_key');

if ($api_key === '') {
$message = I18N::translate('This service requires an API key.');

if (Auth::isAdmin()) {
$message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
}

throw new HttpServerErrorException($message);
}

return [
(object) [
'bingMapsKey' => $api_key,
Expand Down
13 changes: 0 additions & 13 deletions app/Module/GoogleMaps.php
Expand Up @@ -19,15 +19,12 @@

namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use function e;
use function redirect;

/**
Expand Down Expand Up @@ -111,16 +108,6 @@ public function leafletJsTileLayers(): array
{
$api_key = $this->getPreference('api_key');

if ($api_key === '') {
$message = I18N::translate('This service requires an API key.');

if (Auth::isAdmin()) {
$message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
}

throw new HttpServerErrorException($message);
}

return [
(object) [
'GM_API_KEY' => $api_key,
Expand Down
13 changes: 0 additions & 13 deletions app/Module/HereMaps.php
Expand Up @@ -19,15 +19,12 @@

namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use function e;
use function redirect;

/**
Expand Down Expand Up @@ -70,16 +67,6 @@ public function getAdminAction(): ResponseInterface

$api_key = $this->getPreference('api_key');

if ($api_key === '') {
$message = I18N::translate('This service requires an API key.');

if (Auth::isAdmin()) {
$message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
}

throw new HttpServerErrorException($message);
}

return $this->viewResponse('modules/here-maps/config', [
'api_key' => $api_key,
'title' => $this->title(),
Expand Down
13 changes: 0 additions & 13 deletions app/Module/MapBox.php
Expand Up @@ -19,15 +19,12 @@

namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use function e;
use function redirect;

/**
Expand Down Expand Up @@ -70,16 +67,6 @@ public function getAdminAction(): ResponseInterface

$api_key = $this->getPreference('api_key');

if ($api_key === '') {
$message = I18N::translate('This service requires an API key.');

if (Auth::isAdmin()) {
$message = '<a href="' . e($this->getConfigLink()) . '">' . $message . '</a>';
}

throw new HttpServerErrorException($message);
}

return $this->viewResponse('modules/map-box/config', [
'api_key' => $api_key,
'title' => $this->title(),
Expand Down
7 changes: 7 additions & 0 deletions app/Module/ModuleMapProviderInterface.php
Expand Up @@ -30,4 +30,11 @@ interface ModuleMapProviderInterface extends ModuleInterface
* @return array<object>
*/
public function leafletJsTileLayers(): array;

/**
* Check if provider has valid api key
*
* @return bool
*/
public function hasApiKey(): bool;
}
27 changes: 27 additions & 0 deletions app/Module/ModuleMapProviderTrait.php
Expand Up @@ -19,11 +19,19 @@

namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\I18N;

use function e;

/**
* Trait ModuleMapProviderTrait - default implementation of ModuleMapProviderInterface
*/
trait ModuleMapProviderTrait
{
use ModuleConfigTrait;

/**
* Parameters to create a TileLayer in LeafletJs.
*
Expand All @@ -33,4 +41,23 @@ public function leafletJsTileLayers(): array
{
return [];
}

/**
* Check if Module contains the functions for a config page,
* If so then an api key is required so check if it is empty
*
* @return bool
* @throws HttpServerErrorException
*/
public function hasApiKey(): bool
{
$error = in_array("getAdminAction", get_class_methods($this)) && $this->getPreference('api_key') === '';
if ($error && Auth::isAdmin()) {
$message = I18N::translate('<a href="%s">The %s service requires an API key.', e($this->getConfigLink()), $this->title());

throw new HttpServerErrorException($message);
}

return !$error;
}
}
9 changes: 9 additions & 0 deletions app/Services/LeafletJsService.php
Expand Up @@ -23,6 +23,7 @@
use Fisharebest\Webtrees\Http\Exceptions\HttpServiceUnavailableException;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesMapProvidersPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Module\ModuleMapProviderInterface;

/**
Expand Down Expand Up @@ -51,6 +52,14 @@ public function config(): object

$map_providers = $this->module_service
->findByInterface(ModuleMapProviderInterface::class)
->filter(function ($item) {
$hasApiKey = $item->hasApiKey();
if (!$hasApiKey) {
Log::addErrorLog('Map provider "' . $item->title() . '" does not have an api key');
}

return $hasApiKey;
})
->map(static function (ModuleMapProviderInterface $map_provider) use ($default): object {
return (object) [
'children' => $map_provider->leafletJsTileLayers(),
Expand Down