Skip to content

Commit

Permalink
[authentication manager] Allow configurations to be overwritten when …
Browse files Browse the repository at this point in the history
…importing
  • Loading branch information
nirvn committed May 15, 2021
1 parent 8abb178 commit 8513b80
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 4 additions & 2 deletions python/core/auto_generated/auth/qgsauthmanager.sip.in
Expand Up @@ -262,11 +262,12 @@ Returns the regular expression for authcfg=.{7} key/value token for authenticati
Gets list of authentication ids from database
%End

bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig /In,Out/ );
bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig /In,Out/, bool overwrite = false );
%Docstring
Store an authentication config in the database

:param mconfig: Associated authentication config id
:param overwrite: If set to ``True``, pre-existing authentication configurations will be overwritten

:return: Whether operation succeeded
%End
Expand Down Expand Up @@ -311,12 +312,13 @@ Export authentication configurations to an XML file
.. versionadded:: 3.20
%End

bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString() );
bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString(), bool overwrite = false );
%Docstring
Import authentication configurations from an XML file

:param filename: The file path from which the XML content will be read
:param password: A password string to decrypt the XML content
:param overwrite: If set to ``True``, pre-existing authentication configurations will be overwritten

.. versionadded:: 3.20
%End
Expand Down
23 changes: 14 additions & 9 deletions src/core/auth/qgsauthmanager.cpp
Expand Up @@ -1076,7 +1076,7 @@ QgsAuthMethod::Expansions QgsAuthManager::supportedAuthMethodExpansions( const Q
return QgsAuthMethod::Expansions();
}

bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig, bool overwrite )
{
QMutexLocker locker( mMutex.get() );
if ( !setMasterPassword( true ) )
Expand All @@ -1099,10 +1099,16 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
}
else if ( configIds().contains( uid ) )
{
const char *err = QT_TR_NOOP( "Store config: FAILED because pre-defined config ID is not unique" );
QgsDebugMsg( err );
emit messageOut( tr( err ), authManTag(), WARNING );
return false;
if ( !overwrite )
{
const char *err = QT_TR_NOOP( "Store config: FAILED because pre-defined config ID %1 is not unique" );
QgsDebugMsg( err );
emit messageOut( tr( err ), authManTag(), WARNING );
return false;
}
locker.unlock();
removeAuthenticationConfig( uid );
locker.relock();
}

QString configstring = mconfig.configString();
Expand Down Expand Up @@ -1142,15 +1148,14 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
if ( !authDbCommit() )
return false;

// passed-in config should now be like as if it was just loaded from db
// passed-in config should now be like as if it was just loaded from db
if ( !passedinID )
mconfig.setId( uid );

updateConfigAuthMethods();

QgsDebugMsgLevel( QStringLiteral( "Store config SUCCESS for authcfg: %1" ).arg( uid ), 2 );
return true;

}

bool QgsAuthManager::updateAuthenticationConfig( const QgsAuthMethodConfig &config )
Expand Down Expand Up @@ -1384,7 +1389,7 @@ bool QgsAuthManager::exportAuthenticationConfigsToXml( const QString &filename,
return true;
}

bool QgsAuthManager::importAuthenticationConfigsFromXml( const QString &filename, const QString &password )
bool QgsAuthManager::importAuthenticationConfigsFromXml( const QString &filename, const QString &password, bool overwrite )
{
QFile file( filename );
if ( !file.open( QFile::ReadOnly ) )
Expand Down Expand Up @@ -1428,7 +1433,7 @@ bool QgsAuthManager::importAuthenticationConfigsFromXml( const QString &filename
{
QgsAuthMethodConfig authMethodConfig;
authMethodConfig.readXml( configuration );
storeAuthenticationConfig( authMethodConfig );
storeAuthenticationConfig( authMethodConfig, overwrite );

configuration = configuration.nextSiblingElement();
}
Expand Down
6 changes: 4 additions & 2 deletions src/core/auth/qgsauthmanager.h
Expand Up @@ -273,9 +273,10 @@ class CORE_EXPORT QgsAuthManager : public QObject
/**
* Store an authentication config in the database
* \param mconfig Associated authentication config id
* \param overwrite If set to TRUE, pre-existing authentication configurations will be overwritten
* \returns Whether operation succeeded
*/
bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig SIP_INOUT );
bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig SIP_INOUT, bool overwrite = false );

/**
* Update an authentication config in the database
Expand Down Expand Up @@ -313,9 +314,10 @@ class CORE_EXPORT QgsAuthManager : public QObject
* Import authentication configurations from an XML file
* \param filename The file path from which the XML content will be read
* \param password A password string to decrypt the XML content
* \param overwrite If set to TRUE, pre-existing authentication configurations will be overwritten
* \since QGIS 3.20
*/
bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString() );
bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString(), bool overwrite = false );

/**
* Clear all authentication configs from table in database and from provider caches
Expand Down

0 comments on commit 8513b80

Please sign in to comment.