Skip to content

Commit

Permalink
Check for layers which might need an API key, according to server con…
Browse files Browse the repository at this point in the history
…figuration
  • Loading branch information
Gustry committed Jun 4, 2024
1 parent 2b2c08e commit 6f5de0a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
23 changes: 20 additions & 3 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
duplicated_rule_key_legend,
project_invalid_pk,
project_safeguards_checks,
project_tos_layers,
project_trust_layer_metadata,
simplify_provider_side,
trailing_layer_group_name,
Expand Down Expand Up @@ -3062,6 +3063,7 @@ def check_project(
SourceLayer,
)
server_metadata = self.dlg.server_combo.currentData(ServerComboData.JsonMetadata.value)
lizmap_cloud = is_lizmap_cloud(server_metadata)
beginner_mode = QgsSettings().value(Settings.key(Settings.BeginnerMode), True, bool)
severities = Severities()

Expand All @@ -3072,7 +3074,7 @@ def check_project(
self.dlg.html_help.setHtml(
checks.html(
severity=severities.blocking if beginner_mode else severities.important,
lizmap_cloud=is_lizmap_cloud(server_metadata)
lizmap_cloud=lizmap_cloud
)
)

Expand Down Expand Up @@ -3300,7 +3302,7 @@ def check_project(

if check_server:

if is_lizmap_cloud(server_metadata):
if lizmap_cloud:
error, message = check_project_ssl_postgis(self.project)
for layer in error:
self.dlg.check_results.add_error(
Expand Down Expand Up @@ -3342,6 +3344,21 @@ def check_project(
for result in results:
self.dlg.check_results.add_error(result)

tos_checks = server_metadata.get("qgis_server_info").get("external_providers_tos_checks")
if tos_checks is not None:
google = tos_checks.get('google', False)
bing = tos_checks.get('bing', False)
if google or bing:
for layer in project_tos_layers(self.project, google, bing):
self.dlg.check_results.add_error(
Error(
layer.name,
checks.LayerMissingApiKey,
source_type=SourceLayer(layer.name, layer.layer_id),
),
lizmap_cloud=lizmap_cloud,
)

if lwc_version >= LwcVersions.Lizmap_3_7:
results = duplicated_layer_with_filter_legend(self.project)
if results:
Expand Down Expand Up @@ -3482,7 +3499,7 @@ def check_project(
msg = tr('You are using the "{mode}" mode.').format(mode=mode)
self.dlg.label_check_resume.setText(msg)

self.dlg.auto_fix_tooltip(is_lizmap_cloud(server_metadata))
self.dlg.auto_fix_tooltip(lizmap_cloud)
self.dlg.label_autofix.setVisible(self.dlg.has_auto_fix())
self.dlg.push_visit_settings.setVisible(self.dlg.has_auto_fix())

Expand Down
13 changes: 13 additions & 0 deletions lizmap/project_checker_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ def project_invalid_pk(project: QgsProject) -> Tuple[List[SourceLayer], List[Sou
return autogenerated_keys, int8, varchar


def project_tos_layers(project: QgsProject, google: bool, bing: bool) -> List[SourceLayer]:
""" Check for Google or Bing layers. """
layers = []
for layer in project.mapLayers().values():
datasource = layer.source().lower()
if 'google.com' in datasource and google:
layers.append(SourceLayer(layer.name(), layer.id()))
elif 'virtualearth.net' in datasource and bing:
layers.append(SourceLayer(layer.name(), layer.id()))

return layers


def auto_generated_primary_key_field(layer: QgsVectorLayer) -> Tuple[bool, Optional[str]]:
""" If the primary key has been detected as tid/ctid but the field does not exist. """
# In QGIS source code, look for "Primary key is ctid"
Expand Down
39 changes: 37 additions & 2 deletions lizmap/widgets/check_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def __init__(self):
),
),
Levels.Project,
Severities().low,
Severities().important,
QIcon(':/images/themes/default/mIconWms.svg'),
)
self.ServerVersion = Check(
Expand Down Expand Up @@ -650,6 +650,41 @@ def __init__(self):
Severities().blocking,
QIcon(':/images/themes/default/mIconWms.svg'),
)
self.LayerMissingApiKey = Check(
'layer_without_api_key',
tr('Missing API key'),
tr(
"The layer requires an API key to be exposed on the internet but the Lizmap configuration is missing "
"the API key. The layer will be discarded "
"on the server side."
),
(
'<ul>'
'<li>{}</li>'
'<li>{}</li>'
'<li>{}</li>'
'</ul>'.format(
tr("Either add the API key for this provider"),
tr('Or remove the layer.'),
tr(
'Or disable these API checks using environment variables on the server side of the Lizmap '
'server plugin.'
),
)
),
Levels.Layer,
Severities().low,
QIcon(':/images/themes/default/locked.svg'),
(
'<ul>'
'<li>{}</li>'
'<li>{}</li>'
'</ul>'.format(
tr("Either add the API key for this provider"),
tr('Or remove the layer.'),
)
),
)
self.TrustProject = Check(
'trust_project_metadata',
tr('Trust project metadata'),
Expand Down Expand Up @@ -1040,7 +1075,7 @@ def add_error(self, error: Error, lizmap_cloud: bool = False, severity=None, ico
used_severity = error.check.severity
if used_severity == Severities().unknown:
if severity:
# The given severity is overriden the one in the error
# The given severity is overridden the one in the error
used_severity = severity
else:
raise NotImplementedError('Missing severity level')
Expand Down

0 comments on commit 6f5de0a

Please sign in to comment.