Skip to content

Commit 6f5de0a

Browse files
committed
Check for layers which might need an API key, according to server configuration
1 parent 2b2c08e commit 6f5de0a

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

lizmap/plugin.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
duplicated_rule_key_legend,
152152
project_invalid_pk,
153153
project_safeguards_checks,
154+
project_tos_layers,
154155
project_trust_layer_metadata,
155156
simplify_provider_side,
156157
trailing_layer_group_name,
@@ -3062,6 +3063,7 @@ def check_project(
30623063
SourceLayer,
30633064
)
30643065
server_metadata = self.dlg.server_combo.currentData(ServerComboData.JsonMetadata.value)
3066+
lizmap_cloud = is_lizmap_cloud(server_metadata)
30653067
beginner_mode = QgsSettings().value(Settings.key(Settings.BeginnerMode), True, bool)
30663068
severities = Severities()
30673069

@@ -3072,7 +3074,7 @@ def check_project(
30723074
self.dlg.html_help.setHtml(
30733075
checks.html(
30743076
severity=severities.blocking if beginner_mode else severities.important,
3075-
lizmap_cloud=is_lizmap_cloud(server_metadata)
3077+
lizmap_cloud=lizmap_cloud
30763078
)
30773079
)
30783080

@@ -3300,7 +3302,7 @@ def check_project(
33003302

33013303
if check_server:
33023304

3303-
if is_lizmap_cloud(server_metadata):
3305+
if lizmap_cloud:
33043306
error, message = check_project_ssl_postgis(self.project)
33053307
for layer in error:
33063308
self.dlg.check_results.add_error(
@@ -3342,6 +3344,21 @@ def check_project(
33423344
for result in results:
33433345
self.dlg.check_results.add_error(result)
33443346

3347+
tos_checks = server_metadata.get("qgis_server_info").get("external_providers_tos_checks")
3348+
if tos_checks is not None:
3349+
google = tos_checks.get('google', False)
3350+
bing = tos_checks.get('bing', False)
3351+
if google or bing:
3352+
for layer in project_tos_layers(self.project, google, bing):
3353+
self.dlg.check_results.add_error(
3354+
Error(
3355+
layer.name,
3356+
checks.LayerMissingApiKey,
3357+
source_type=SourceLayer(layer.name, layer.layer_id),
3358+
),
3359+
lizmap_cloud=lizmap_cloud,
3360+
)
3361+
33453362
if lwc_version >= LwcVersions.Lizmap_3_7:
33463363
results = duplicated_layer_with_filter_legend(self.project)
33473364
if results:
@@ -3482,7 +3499,7 @@ def check_project(
34823499
msg = tr('You are using the "{mode}" mode.').format(mode=mode)
34833500
self.dlg.label_check_resume.setText(msg)
34843501

3485-
self.dlg.auto_fix_tooltip(is_lizmap_cloud(server_metadata))
3502+
self.dlg.auto_fix_tooltip(lizmap_cloud)
34863503
self.dlg.label_autofix.setVisible(self.dlg.has_auto_fix())
34873504
self.dlg.push_visit_settings.setVisible(self.dlg.has_auto_fix())
34883505

lizmap/project_checker_tools.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ def project_invalid_pk(project: QgsProject) -> Tuple[List[SourceLayer], List[Sou
182182
return autogenerated_keys, int8, varchar
183183

184184

185+
def project_tos_layers(project: QgsProject, google: bool, bing: bool) -> List[SourceLayer]:
186+
""" Check for Google or Bing layers. """
187+
layers = []
188+
for layer in project.mapLayers().values():
189+
datasource = layer.source().lower()
190+
if 'google.com' in datasource and google:
191+
layers.append(SourceLayer(layer.name(), layer.id()))
192+
elif 'virtualearth.net' in datasource and bing:
193+
layers.append(SourceLayer(layer.name(), layer.id()))
194+
195+
return layers
196+
197+
185198
def auto_generated_primary_key_field(layer: QgsVectorLayer) -> Tuple[bool, Optional[str]]:
186199
""" If the primary key has been detected as tid/ctid but the field does not exist. """
187200
# In QGIS source code, look for "Primary key is ctid"

lizmap/widgets/check_project.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def __init__(self):
288288
),
289289
),
290290
Levels.Project,
291-
Severities().low,
291+
Severities().important,
292292
QIcon(':/images/themes/default/mIconWms.svg'),
293293
)
294294
self.ServerVersion = Check(
@@ -650,6 +650,41 @@ def __init__(self):
650650
Severities().blocking,
651651
QIcon(':/images/themes/default/mIconWms.svg'),
652652
)
653+
self.LayerMissingApiKey = Check(
654+
'layer_without_api_key',
655+
tr('Missing API key'),
656+
tr(
657+
"The layer requires an API key to be exposed on the internet but the Lizmap configuration is missing "
658+
"the API key. The layer will be discarded "
659+
"on the server side."
660+
),
661+
(
662+
'<ul>'
663+
'<li>{}</li>'
664+
'<li>{}</li>'
665+
'<li>{}</li>'
666+
'</ul>'.format(
667+
tr("Either add the API key for this provider"),
668+
tr('Or remove the layer.'),
669+
tr(
670+
'Or disable these API checks using environment variables on the server side of the Lizmap '
671+
'server plugin.'
672+
),
673+
)
674+
),
675+
Levels.Layer,
676+
Severities().low,
677+
QIcon(':/images/themes/default/locked.svg'),
678+
(
679+
'<ul>'
680+
'<li>{}</li>'
681+
'<li>{}</li>'
682+
'</ul>'.format(
683+
tr("Either add the API key for this provider"),
684+
tr('Or remove the layer.'),
685+
)
686+
),
687+
)
653688
self.TrustProject = Check(
654689
'trust_project_metadata',
655690
tr('Trust project metadata'),
@@ -1040,7 +1075,7 @@ def add_error(self, error: Error, lizmap_cloud: bool = False, severity=None, ico
10401075
used_severity = error.check.severity
10411076
if used_severity == Severities().unknown:
10421077
if severity:
1043-
# The given severity is overriden the one in the error
1078+
# The given severity is overridden the one in the error
10441079
used_severity = severity
10451080
else:
10461081
raise NotImplementedError('Missing severity level')

0 commit comments

Comments
 (0)