From afc43c8ac8631091021e7df89cf7ad01b55cb02e Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Mon, 25 Nov 2024 15:53:01 +0100 Subject: [PATCH] Check for raster layer using AuthDB if needed --- lizmap/project_checker_tools.py | 27 +++++++++++------ lizmap/test/test_project_checker_tools.py | 4 +++ lizmap/widgets/check_project.py | 35 ++++++++++++++++++++++- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lizmap/project_checker_tools.py b/lizmap/project_checker_tools.py index 6b41ff67..2722e55f 100644 --- a/lizmap/project_checker_tools.py +++ b/lizmap/project_checker_tools.py @@ -78,8 +78,11 @@ def project_safeguards_checks( if layer.source().lower().endswith('ecw') and prevent_ecw: results[SourceLayer(layer.name(), layer.id())] = checks.PreventEcw - if french_geopf_authcfg_url_parameters(layer.source()) and prevent_auth_id: - results[SourceLayer(layer.name(), layer.id())] = checks.FrenchGeoPlateformeUrl + if prevent_auth_id: + if french_geopf_authcfg_url_parameters(layer.source()): + results[SourceLayer(layer.name(), layer.id())] = checks.FrenchGeoPlateformeUrl + elif authcfg_url_parameters(layer.source()): + results[SourceLayer(layer.name(), layer.id())] = checks.RasterAuthenticationDb if is_vector_pg(layer): # Make a copy by using a string, so we are sure to have user or password @@ -470,23 +473,31 @@ def trailing_layer_group_name(layer_tree: QgsLayerTreeNode, project, results: Li return results -def french_geopf_authcfg_url_parameters(datasource: str) -> bool: +def authcfg_url_parameters(datasource: str) -> bool: """ Check for authcfg in a datasource, using a plain string. This function is not using QgsDataSourceUri::authConfigId() """ - if 'data.geopf.fr' not in datasource.lower(): - return False - url_param = QUrlQuery(html.unescape(datasource)) for param in url_param.queryItems(): if param[0].lower() == 'authcfg' and param[1] != '': return True - # if param[0].lower().startswith("http-header:"): - # return True + return False +def french_geopf_authcfg_url_parameters(datasource: str) -> bool: + """ Check for authcfg in a datasource, using a plain string. + + This function is not using QgsDataSourceUri::authConfigId() + """ + if 'data.geopf.fr' not in datasource.lower(): + return False + # if param[0].lower().startswith("http-header:"): + # return True + return authcfg_url_parameters(datasource) + + def duplicated_rule_key_legend(project: QgsProject, filter_data: bool = True) -> Dict[str, Dict[str, int]]: """ Check for all duplicated rule keys in the legend. """ results = {} diff --git a/lizmap/test/test_project_checker_tools.py b/lizmap/test/test_project_checker_tools.py index ac7d9b8b..15fb450e 100644 --- a/lizmap/test/test_project_checker_tools.py +++ b/lizmap/test/test_project_checker_tools.py @@ -11,6 +11,7 @@ from lizmap.project_checker_tools import ( _duplicated_label_legend_layer, _split_layer_uri, + authcfg_url_parameters, duplicated_layer_with_filter, duplicated_layer_with_filter_legend, duplicated_rule_key_legend, @@ -169,6 +170,7 @@ def test_french_raster_datasource_authcfg(self): "&authCFG=x3rzac9" ) self.assertFalse(french_geopf_authcfg_url_parameters(raster)) + self.assertTrue(authcfg_url_parameters(raster)) # data.GEOPF.fr raster = ( @@ -177,6 +179,7 @@ def test_french_raster_datasource_authcfg(self): "&authCFG=x3rzac9" ) self.assertTrue(french_geopf_authcfg_url_parameters(raster)) + self.assertTrue(authcfg_url_parameters(raster)) # Correct raster = ( @@ -184,6 +187,7 @@ def test_french_raster_datasource_authcfg(self): "layers=SCAN25TOUR_PYR-JPEG_WLD_WM&styles&url=https://data.geopf.fr/private/wms-r?VERSION%3D1.3.0" ) self.assertFalse(french_geopf_authcfg_url_parameters(raster)) + self.assertFalse(authcfg_url_parameters(raster)) # http-header # raster = ( diff --git a/lizmap/widgets/check_project.py b/lizmap/widgets/check_project.py index 28489fbb..949763fe 100644 --- a/lizmap/widgets/check_project.py +++ b/lizmap/widgets/check_project.py @@ -251,7 +251,7 @@ def __init__(self): other_auth = tr('Either switch to another authentication mechanism') safeguard = tr('Or disable this safeguard in your Lizmap plugin settings') global_connection = tr( - 'To fix layers loaded later, edit your global PostgreSQL connection to enable this option, then ' + 'To fix layers loaded later, edit your global PostgreSQL/raster connection to enable this option, then ' 'change the datasource by right clicking on each layer above, then click "Change datasource" in the menu. ' 'Finally reselect your layer in the new dialog with the updated connection. When opening a QGIS project in ' 'your computer, with a fresh launched QGIS software, you mustn\'t have any prompt for a user or password. ' @@ -799,6 +799,39 @@ def __init__(self): '' ).format(help=tr('Switch to a COG format')) ) + self.RasterAuthenticationDb = Check( + f"{Settings.PreventPgAuthDb}_raster", + tr('QGIS Authentication database'), + tr( + 'The layer is using the QGIS authentication database. You have activated a safeguard preventing you ' + 'using the QGIS authentication database.' + ), + ( + ''.format( + help=other_auth, + other=safeguard, + global_connection=global_connection, + ) + ), + Levels.Layer, + Severities().unknown, + QIcon(':/images/themes/default/mIconPostgis.svg'), + tr('The layer is using the QGIS authentication database. This is not compatible with {}').format(CLOUD_NAME), + ( + '' + ).format( + login_pass=tr( + 'Store the login and password in the layer by editing the global connection and do the ' + '"Change datasource" on each layer.' + ) + ) + ) self.AuthenticationDb = Check( Settings.PreventPgAuthDb, tr('QGIS Authentication database'),