Skip to content

Commit

Permalink
[auth] Add authcfg ID edit widget and support for changing the ID
Browse files Browse the repository at this point in the history
  • Loading branch information
dakcarto committed Sep 25, 2015
1 parent 0975c9f commit 12de332
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 65 deletions.
6 changes: 3 additions & 3 deletions python/gui/auth/qgsauthconfigeditor.sip
Expand Up @@ -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 );
};
21 changes: 17 additions & 4 deletions src/core/auth/qgsauthmanager.cpp
Expand Up @@ -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;
Expand All @@ -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() );
Expand All @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
75 changes: 60 additions & 15 deletions src/gui/auth/qgsauthconfigedit.cpp
Expand Up @@ -19,6 +19,7 @@
#include <QPushButton>

#include "qgsauthconfig.h"
#include "qgsauthconfigidedit.h"
#include "qgsauthmanager.h"
#include "qgsauthmethodedit.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -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
{
Expand All @@ -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 ) ) );
Expand Down Expand Up @@ -128,7 +132,9 @@ void QgsAuthConfigEdit::populateAuthMethods()

void QgsAuthConfigEdit::loadConfig()
{
if ( mAuthCfg.isEmpty() )
bool emptyAuthCfg = mAuthCfg.isEmpty();
authCfgEdit->setAllowEmptyId( emptyAuthCfg );
if ( emptyAuthCfg )
{
return;
}
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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" ) );
}
}
}

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

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

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

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

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

Expand Down
34 changes: 24 additions & 10 deletions src/gui/auth/qgsauthconfigeditor.cpp
Expand Up @@ -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 )
Expand All @@ -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() );
Expand Down Expand Up @@ -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() ) );
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions src/gui/auth/qgsauthconfigeditor.h
Expand Up @@ -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 */
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 12de332

Please sign in to comment.