diff --git a/python/PyQt6/core/auto_generated/qgsmaplayer.sip.in b/python/PyQt6/core/auto_generated/qgsmaplayer.sip.in index 73545c5cd377..f185105e38a3 100644 --- a/python/PyQt6/core/auto_generated/qgsmaplayer.sip.in +++ b/python/PyQt6/core/auto_generated/qgsmaplayer.sip.in @@ -567,13 +567,14 @@ or other problem. Child classes set this flag when initialized. :return: ``True`` if the layer is valid and can be accessed %End + QString publicSource( bool hidePassword = false ) const; %Docstring Gets a version of the internal layer definition that has sensitive bits removed (for example, the password). This function should be used when displaying the source name for general viewing. -:param hidePassword: False, if the password should be removed or replaced by an arbitrary string, since QGIS 3.34 +:param hidePassword: ``True`` to replace the value of credentials with 'xxxxxxxx', ``False`` to completely remove credentials (key and value). Since QGIS 3.34 .. seealso:: :py:func:`source` %End diff --git a/python/core/auto_generated/qgsmaplayer.sip.in b/python/core/auto_generated/qgsmaplayer.sip.in index cfa3b903874f..41b536975391 100644 --- a/python/core/auto_generated/qgsmaplayer.sip.in +++ b/python/core/auto_generated/qgsmaplayer.sip.in @@ -567,13 +567,14 @@ or other problem. Child classes set this flag when initialized. :return: ``True`` if the layer is valid and can be accessed %End + QString publicSource( bool hidePassword = false ) const; %Docstring Gets a version of the internal layer definition that has sensitive bits removed (for example, the password). This function should be used when displaying the source name for general viewing. -:param hidePassword: False, if the password should be removed or replaced by an arbitrary string, since QGIS 3.34 +:param hidePassword: ``True`` to replace the value of credentials with 'xxxxxxxx', ``False`` to completely remove credentials (key and value). Since QGIS 3.34 .. seealso:: :py:func:`source` %End diff --git a/src/core/qgsmaplayer.cpp b/src/core/qgsmaplayer.cpp index e1cfb6040556..d4b31630664d 100644 --- a/src/core/qgsmaplayer.cpp +++ b/src/core/qgsmaplayer.cpp @@ -473,13 +473,37 @@ QString QgsMapLayer::metadataUrlFormat() const } } -QString QgsMapLayer::publicSource( bool hidePassword ) const +QString QgsMapLayer::publicSource( bool redactCredentials ) const { QGIS_PROTECT_QOBJECT_THREAD_ACCESS + QString safeName = mDataSource; + + if ( providerType() == QLatin1String( "gdal" ) ) + { + QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), safeName ); + QVariantMap credentialOptions = components.value( QStringLiteral( "credentialOptions" ) ).toMap(); + if ( !credentialOptions.empty() ) + { + if ( redactCredentials ) + { + for ( auto it = credentialOptions.begin(); it != credentialOptions.end(); ++it ) + { + it.value() = QStringLiteral( "XXXXXXXX" ); + } + components.insert( QStringLiteral( "credentialOptions" ), credentialOptions ); + } + else + { + components.remove( QStringLiteral( "credentialOptions" ) ); + } + } + safeName = QgsProviderRegistry::instance()->encodeUri( providerType(), components ); + } + // Redo this every time we're asked for it, as we don't know if // dataSource has changed. - QString safeName = QgsDataSourceUri::removePassword( mDataSource, hidePassword ); + safeName = QgsDataSourceUri::removePassword( safeName, redactCredentials ); return safeName; } @@ -3232,12 +3256,14 @@ QString QgsMapLayer::generalHtmlMetadata() const // name metadata += QStringLiteral( "