Skip to content
Permalink
Browse files

[auth] Add authcfg ID edit widget and support for changing the ID

  • Loading branch information
dakcarto committed Sep 25, 2015
1 parent 0975c9f commit 12de332ebdc7a925921d3d899e85c43600c08676
@@ -5,14 +5,14 @@ class QgsAuthConfigEditor : QWidget
%End

public:
explicit QgsAuthConfigEditor( QWidget *parent /TransferThis/ = 0, bool showUtilities = true, QgsMessageBar *msgbar /TransferThis/ = 0 );
explicit QgsAuthConfigEditor( QWidget *parent /TransferThis/ = 0, bool showUtilities = true, bool relayMessages = true );
~QgsAuthConfigEditor();

void toggleTitleVisibility( bool visible );

public slots:

void showUtilitiesButton( bool show = true );
void setShowUtilitiesButton( bool show = true );

void setMessageBar( QgsMessageBar *msgbar /TransferThis/ = 0 );
void setRelayMessages( bool relay = true );
};
@@ -915,10 +915,24 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
return false;
}

QString uid = mconfig.id();
bool passedinID = !uid.isEmpty();
if ( uid.isEmpty() )
{
uid = uniqueConfigId();
}
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;
}

QString configstring = mconfig.configString();
if ( configstring.isEmpty() )
{
const char* err = QT_TR_NOOP( "Store config: FAILED because config is empty" );
const char* err = QT_TR_NOOP( "Store config: FAILED because config string is empty" );
QgsDebugMsg( err );
emit messageOut( tr( err ), authManTag(), WARNING );
return false;
@@ -936,8 +950,6 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
query.prepare( QString( "INSERT INTO %1 (id, name, uri, type, version, config) "
"VALUES (:id, :name, :uri, :type, :version, :config)" ).arg( authDbConfigTable() ) );

QString uid = uniqueConfigId();

query.bindValue( ":id", uid );
query.bindValue( ":name", mconfig.name() );
query.bindValue( ":uri", mconfig.uri() );
@@ -955,7 +967,8 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
return false;

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

updateConfigAuthMethods();

@@ -65,6 +65,7 @@ SET(QGIS_GUI_SRCS
auth/qgsauthcerttrustpolicycombobox.cpp
auth/qgsauthconfigedit.cpp
auth/qgsauthconfigeditor.cpp
auth/qgsauthconfigidedit.cpp
auth/qgsauthconfigselect.cpp
auth/qgsautheditorwidgets.cpp
auth/qgsauthguiutils.cpp
@@ -450,6 +451,7 @@ SET(QGIS_GUI_MOC_HDRS
auth/qgsauthcerttrustpolicycombobox.h
auth/qgsauthconfigedit.h
auth/qgsauthconfigeditor.h
auth/qgsauthconfigidedit.h
auth/qgsauthconfigselect.h
auth/qgsautheditorwidgets.h
auth/qgsauthidentitieseditor.h
@@ -567,6 +569,7 @@ SET(QGIS_GUI_HDRS
auth/qgsauthcerttrustpolicycombobox.h
auth/qgsauthconfigedit.h
auth/qgsauthconfigeditor.h
auth/qgsauthconfigidedit.h
auth/qgsauthconfigselect.h
auth/qgsautheditorwidgets.h
auth/qgsauthguiutils.h
@@ -19,6 +19,7 @@
#include <QPushButton>

#include "qgsauthconfig.h"
#include "qgsauthconfigidedit.h"
#include "qgsauthmanager.h"
#include "qgsauthmethodedit.h"
#include "qgslogger.h"
@@ -53,6 +54,7 @@ QgsAuthConfigEdit::QgsAuthConfigEdit( QWidget *parent , const QString& authcfg ,
mAuthNotifyLayout->addWidget( mAuthNotify );

mAuthCfg.clear(); // otherwise will contiue to try authenticate (and fail) after save
buttonBox->button( QDialogButtonBox::Save )->setEnabled( false );
}
else
{
@@ -68,6 +70,8 @@ QgsAuthConfigEdit::QgsAuthConfigEdit( QWidget *parent , const QString& authcfg ,
connect( cmbAuthMethods, SIGNAL( currentIndexChanged( int ) ),
this, SLOT( validateAuth() ) );

connect( authCfgEdit, SIGNAL( validityChanged( bool ) ), this, SLOT( validateAuth() ) );

// needed (if only combobox is ever changed)?
// connect( stkwAuthMethods, SIGNAL( currentChanged( int ) ),
// cmbAuthMethods, SLOT( setCurrentIndex( int ) ) );
@@ -128,7 +132,9 @@ void QgsAuthConfigEdit::populateAuthMethods()

void QgsAuthConfigEdit::loadConfig()
{
if ( mAuthCfg.isEmpty() )
bool emptyAuthCfg = mAuthCfg.isEmpty();
authCfgEdit->setAllowEmptyId( emptyAuthCfg );
if ( emptyAuthCfg )
{
return;
}
@@ -156,7 +162,7 @@ void QgsAuthConfigEdit::loadConfig()
// load basic info
leName->setText( mconfig.name() );
leResource->setText( mconfig.uri() );
leAuthCfg->setText( mconfig.id() );
authCfgEdit->setAuthConfigId( mconfig.id() );

QString authMethodKey = QgsAuthManager::instance()->configAuthMethodKey( mAuthCfg );

@@ -243,28 +249,65 @@ void QgsAuthConfigEdit::saveConfig()
return;
}

if ( !mAuthCfg.isEmpty() ) // update
QString authCfgId( authCfgEdit->configId() );
if ( !mAuthCfg.isEmpty() )
{
mconfig.setId( mAuthCfg );
if ( QgsAuthManager::instance()->updateAuthenticationConfig( mconfig ) )
if ( authCfgId == mAuthCfg ) // update
{
emit authenticationConfigUpdated( mAuthCfg );
mconfig.setId( mAuthCfg );
if ( QgsAuthManager::instance()->updateAuthenticationConfig( mconfig ) )
{
emit authenticationConfigUpdated( mAuthCfg );
}
else
{
QgsDebugMsg( QString( "Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
}
}
else
else // store new with unique ID, then delete previous
{
QgsDebugMsg( QString( "Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
mconfig.setId( authCfgId );
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
{
emit authenticationConfigStored( authCfgId );
if ( !QgsAuthManager::instance()->removeAuthenticationConfig( mAuthCfg ) )
{
QgsDebugMsg( QString( "Removal of older auth config FAILED" ) );
}
mAuthCfg = authCfgId;
}
else
{
QgsDebugMsg( QString( "Storing new auth config with user-created unique ID FAILED" ) );
}
}
}
else // create new
else if ( mAuthCfg.isEmpty() )
{
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
if ( authCfgId.isEmpty() ) // create new with generated ID
{
mAuthCfg = mconfig.id();
emit authenticationConfigStored( mAuthCfg );
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
{
mAuthCfg = mconfig.id();
emit authenticationConfigStored( mAuthCfg );
}
else
{
QgsDebugMsg( QString( "Storing new auth config FAILED" ) );
}
}
else
else // create new with user-created unique ID
{
QgsDebugMsg( QString( "Storing new auth config FAILED" ) );
mconfig.setId( authCfgId );
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
{
mAuthCfg = authCfgId;
emit authenticationConfigStored( mAuthCfg );
}
else
{
QgsDebugMsg( QString( "Storing new auth config with user-created unique ID FAILED" ) );
}
}
}

@@ -289,7 +332,7 @@ void QgsAuthConfigEdit::clearAll()
{
leName->clear();
leResource->clear();
leAuthCfg->clear();
authCfgEdit->clear();

for ( int i = 0; i < stkwAuthMethods->count(); i++ )
{
@@ -315,6 +358,8 @@ void QgsAuthConfigEdit::validateAuth()

authok = authok && editWidget->validateConfig();

authok = authok && authCfgEdit->validate();

buttonBox->button( QDialogButtonBox::Save )->setEnabled( authok );
}

@@ -26,8 +26,9 @@
#include "qgsauthconfigedit.h"
#include "qgsauthguiutils.h"

QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, QgsMessageBar *msgbar )
QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
: QWidget( parent )
, mRelayMessages( relayMessages )
, mConfigModel( 0 )
, mAuthUtilitiesMenu( 0 )
, mActionSetMasterPassword( 0 )
@@ -50,8 +51,7 @@ QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, Q
{
setupUi( this );

setMessageBar( msgbar );
showUtilitiesButton( showUtilities );
setShowUtilitiesButton( showUtilities );

mConfigModel = new QSqlTableModel( this, QgsAuthManager::instance()->authDbConnection() );
mConfigModel->setTable( QgsAuthManager::instance()->authDbConfigTable() );
@@ -83,8 +83,11 @@ QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, Q
connect( tableViewConfigs, SIGNAL( doubleClicked( QModelIndex ) ),
this, SLOT( on_btnEditConfig_clicked() ) );

connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
if ( mRelayMessages )
{
connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
}

connect( QgsAuthManager::instance(), SIGNAL( authDatabaseChanged() ),
this, SLOT( refreshTableView() ) );
@@ -169,18 +172,29 @@ void QgsAuthConfigEditor::toggleTitleVisibility( bool visible )
}
}

void QgsAuthConfigEditor::showUtilitiesButton( bool show )
void QgsAuthConfigEditor::setShowUtilitiesButton( bool show )
{
btnAuthUtilities->setVisible( show );
}

void QgsAuthConfigEditor::setMessageBar( QgsMessageBar *msgbar )
void QgsAuthConfigEditor::setRelayMessages( bool relay )
{
if ( msgbar )
if ( relay == mRelayMessages )
{
delete mMsgBar;
mMsgBar = msgbar;
return;
}

if ( mRelayMessages )
{
disconnect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
mRelayMessages = relay;
return;
}

connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
mRelayMessages = relay;
}

void QgsAuthConfigEditor::refreshTableView()
@@ -35,21 +35,22 @@ class GUI_EXPORT QgsAuthConfigEditor : public QWidget, private Ui::QgsAuthConfig
public:
/**
* Widget for editing authentication configurations directly in database
* @param parent Parent widget
* @param showUtilities Whether to show the widget's utilities button
* @param msgbar Substitute internal message bar for another
* @param relayMessages Whether to relay auth manager messages to internal message bar
*/
explicit QgsAuthConfigEditor( QWidget *parent = 0, bool showUtilities = true, QgsMessageBar *msgbar = 0 );
explicit QgsAuthConfigEditor( QWidget *parent = 0, bool showUtilities = true, bool relayMessages = true );
~QgsAuthConfigEditor();

/** Hide the widget's title, e.g. when embedding */
void toggleTitleVisibility( bool visible );

public slots:
/** Whether to show the widget's utilities button, e.g. when embedding */
void showUtilitiesButton( bool show = true );
/** Set whether to show the widget's utilities button, e.g. when embedding */
void setShowUtilitiesButton( bool show = true );

/** Substitute internal message bar for another, e.g. when embedding */
void setMessageBar( QgsMessageBar *msgbar = 0 );
/** Set whether to relay auth manager messages to internal message bar, e.g. when embedding */
void setRelayMessages( bool relay = true );

private slots:
/** Repopulate the view with table contents */
@@ -89,6 +90,7 @@ class GUI_EXPORT QgsAuthConfigEditor : public QWidget, private Ui::QgsAuthConfig
void on_btnRemoveConfig_clicked();

private:
bool mRelayMessages;
QgsMessageBar * messageBar();
int messageTimeout();
QString selectedConfigId();

0 comments on commit 12de332

Please sign in to comment.
You can’t perform that action at this time.