Skip to content

Commit 12de332

Browse files
committed
[auth] Add authcfg ID edit widget and support for changing the ID
1 parent 0975c9f commit 12de332

14 files changed

Lines changed: 435 additions & 65 deletions

‎python/gui/auth/qgsauthconfigeditor.sip‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ class QgsAuthConfigEditor : QWidget
55
%End
66

77
public:
8-
explicit QgsAuthConfigEditor( QWidget *parent /TransferThis/ = 0, bool showUtilities = true, QgsMessageBar *msgbar /TransferThis/ = 0 );
8+
explicit QgsAuthConfigEditor( QWidget *parent /TransferThis/ = 0, bool showUtilities = true, bool relayMessages = true );
99
~QgsAuthConfigEditor();
1010

1111
void toggleTitleVisibility( bool visible );
1212

1313
public slots:
1414

15-
void showUtilitiesButton( bool show = true );
15+
void setShowUtilitiesButton( bool show = true );
1616

17-
void setMessageBar( QgsMessageBar *msgbar /TransferThis/ = 0 );
17+
void setRelayMessages( bool relay = true );
1818
};

‎src/core/auth/qgsauthmanager.cpp‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,10 +915,24 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
915915
return false;
916916
}
917917

918+
QString uid = mconfig.id();
919+
bool passedinID = !uid.isEmpty();
920+
if ( uid.isEmpty() )
921+
{
922+
uid = uniqueConfigId();
923+
}
924+
else if ( configIds().contains( uid ) )
925+
{
926+
const char* err = QT_TR_NOOP( "Store config: FAILED because pre-defined config ID is not unique" );
927+
QgsDebugMsg( err );
928+
emit messageOut( tr( err ), authManTag(), WARNING );
929+
return false;
930+
}
931+
918932
QString configstring = mconfig.configString();
919933
if ( configstring.isEmpty() )
920934
{
921-
const char* err = QT_TR_NOOP( "Store config: FAILED because config is empty" );
935+
const char* err = QT_TR_NOOP( "Store config: FAILED because config string is empty" );
922936
QgsDebugMsg( err );
923937
emit messageOut( tr( err ), authManTag(), WARNING );
924938
return false;
@@ -936,8 +950,6 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
936950
query.prepare( QString( "INSERT INTO %1 (id, name, uri, type, version, config) "
937951
"VALUES (:id, :name, :uri, :type, :version, :config)" ).arg( authDbConfigTable() ) );
938952

939-
QString uid = uniqueConfigId();
940-
941953
query.bindValue( ":id", uid );
942954
query.bindValue( ":name", mconfig.name() );
943955
query.bindValue( ":uri", mconfig.uri() );
@@ -955,7 +967,8 @@ bool QgsAuthManager::storeAuthenticationConfig( QgsAuthMethodConfig &mconfig )
955967
return false;
956968

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

960973
updateConfigAuthMethods();
961974

‎src/gui/CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ SET(QGIS_GUI_SRCS
6565
auth/qgsauthcerttrustpolicycombobox.cpp
6666
auth/qgsauthconfigedit.cpp
6767
auth/qgsauthconfigeditor.cpp
68+
auth/qgsauthconfigidedit.cpp
6869
auth/qgsauthconfigselect.cpp
6970
auth/qgsautheditorwidgets.cpp
7071
auth/qgsauthguiutils.cpp
@@ -450,6 +451,7 @@ SET(QGIS_GUI_MOC_HDRS
450451
auth/qgsauthcerttrustpolicycombobox.h
451452
auth/qgsauthconfigedit.h
452453
auth/qgsauthconfigeditor.h
454+
auth/qgsauthconfigidedit.h
453455
auth/qgsauthconfigselect.h
454456
auth/qgsautheditorwidgets.h
455457
auth/qgsauthidentitieseditor.h
@@ -567,6 +569,7 @@ SET(QGIS_GUI_HDRS
567569
auth/qgsauthcerttrustpolicycombobox.h
568570
auth/qgsauthconfigedit.h
569571
auth/qgsauthconfigeditor.h
572+
auth/qgsauthconfigidedit.h
570573
auth/qgsauthconfigselect.h
571574
auth/qgsautheditorwidgets.h
572575
auth/qgsauthguiutils.h

‎src/gui/auth/qgsauthconfigedit.cpp‎

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QPushButton>
2020

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

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

73+
connect( authCfgEdit, SIGNAL( validityChanged( bool ) ), this, SLOT( validateAuth() ) );
74+
7175
// needed (if only combobox is ever changed)?
7276
// connect( stkwAuthMethods, SIGNAL( currentChanged( int ) ),
7377
// cmbAuthMethods, SLOT( setCurrentIndex( int ) ) );
@@ -128,7 +132,9 @@ void QgsAuthConfigEdit::populateAuthMethods()
128132

129133
void QgsAuthConfigEdit::loadConfig()
130134
{
131-
if ( mAuthCfg.isEmpty() )
135+
bool emptyAuthCfg = mAuthCfg.isEmpty();
136+
authCfgEdit->setAllowEmptyId( emptyAuthCfg );
137+
if ( emptyAuthCfg )
132138
{
133139
return;
134140
}
@@ -156,7 +162,7 @@ void QgsAuthConfigEdit::loadConfig()
156162
// load basic info
157163
leName->setText( mconfig.name() );
158164
leResource->setText( mconfig.uri() );
159-
leAuthCfg->setText( mconfig.id() );
165+
authCfgEdit->setAuthConfigId( mconfig.id() );
160166

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

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

246-
if ( !mAuthCfg.isEmpty() ) // update
252+
QString authCfgId( authCfgEdit->configId() );
253+
if ( !mAuthCfg.isEmpty() )
247254
{
248-
mconfig.setId( mAuthCfg );
249-
if ( QgsAuthManager::instance()->updateAuthenticationConfig( mconfig ) )
255+
if ( authCfgId == mAuthCfg ) // update
250256
{
251-
emit authenticationConfigUpdated( mAuthCfg );
257+
mconfig.setId( mAuthCfg );
258+
if ( QgsAuthManager::instance()->updateAuthenticationConfig( mconfig ) )
259+
{
260+
emit authenticationConfigUpdated( mAuthCfg );
261+
}
262+
else
263+
{
264+
QgsDebugMsg( QString( "Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
265+
}
252266
}
253-
else
267+
else // store new with unique ID, then delete previous
254268
{
255-
QgsDebugMsg( QString( "Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
269+
mconfig.setId( authCfgId );
270+
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
271+
{
272+
emit authenticationConfigStored( authCfgId );
273+
if ( !QgsAuthManager::instance()->removeAuthenticationConfig( mAuthCfg ) )
274+
{
275+
QgsDebugMsg( QString( "Removal of older auth config FAILED" ) );
276+
}
277+
mAuthCfg = authCfgId;
278+
}
279+
else
280+
{
281+
QgsDebugMsg( QString( "Storing new auth config with user-created unique ID FAILED" ) );
282+
}
256283
}
257284
}
258-
else // create new
285+
else if ( mAuthCfg.isEmpty() )
259286
{
260-
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
287+
if ( authCfgId.isEmpty() ) // create new with generated ID
261288
{
262-
mAuthCfg = mconfig.id();
263-
emit authenticationConfigStored( mAuthCfg );
289+
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
290+
{
291+
mAuthCfg = mconfig.id();
292+
emit authenticationConfigStored( mAuthCfg );
293+
}
294+
else
295+
{
296+
QgsDebugMsg( QString( "Storing new auth config FAILED" ) );
297+
}
264298
}
265-
else
299+
else // create new with user-created unique ID
266300
{
267-
QgsDebugMsg( QString( "Storing new auth config FAILED" ) );
301+
mconfig.setId( authCfgId );
302+
if ( QgsAuthManager::instance()->storeAuthenticationConfig( mconfig ) )
303+
{
304+
mAuthCfg = authCfgId;
305+
emit authenticationConfigStored( mAuthCfg );
306+
}
307+
else
308+
{
309+
QgsDebugMsg( QString( "Storing new auth config with user-created unique ID FAILED" ) );
310+
}
268311
}
269312
}
270313

@@ -289,7 +332,7 @@ void QgsAuthConfigEdit::clearAll()
289332
{
290333
leName->clear();
291334
leResource->clear();
292-
leAuthCfg->clear();
335+
authCfgEdit->clear();
293336

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

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

361+
authok = authok && authCfgEdit->validate();
362+
318363
buttonBox->button( QDialogButtonBox::Save )->setEnabled( authok );
319364
}
320365

‎src/gui/auth/qgsauthconfigeditor.cpp‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
#include "qgsauthconfigedit.h"
2727
#include "qgsauthguiutils.h"
2828

29-
QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, QgsMessageBar *msgbar )
29+
QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
3030
: QWidget( parent )
31+
, mRelayMessages( relayMessages )
3132
, mConfigModel( 0 )
3233
, mAuthUtilitiesMenu( 0 )
3334
, mActionSetMasterPassword( 0 )
@@ -50,8 +51,7 @@ QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, Q
5051
{
5152
setupUi( this );
5253

53-
setMessageBar( msgbar );
54-
showUtilitiesButton( showUtilities );
54+
setShowUtilitiesButton( showUtilities );
5555

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

86-
connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
87-
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
86+
if ( mRelayMessages )
87+
{
88+
connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
89+
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
90+
}
8891

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

172-
void QgsAuthConfigEditor::showUtilitiesButton( bool show )
175+
void QgsAuthConfigEditor::setShowUtilitiesButton( bool show )
173176
{
174177
btnAuthUtilities->setVisible( show );
175178
}
176179

177-
void QgsAuthConfigEditor::setMessageBar( QgsMessageBar *msgbar )
180+
void QgsAuthConfigEditor::setRelayMessages( bool relay )
178181
{
179-
if ( msgbar )
182+
if ( relay == mRelayMessages )
180183
{
181-
delete mMsgBar;
182-
mMsgBar = msgbar;
184+
return;
185+
}
186+
187+
if ( mRelayMessages )
188+
{
189+
disconnect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
190+
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
191+
mRelayMessages = relay;
192+
return;
183193
}
194+
195+
connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
196+
this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
197+
mRelayMessages = relay;
184198
}
185199

186200
void QgsAuthConfigEditor::refreshTableView()

‎src/gui/auth/qgsauthconfigeditor.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@ class GUI_EXPORT QgsAuthConfigEditor : public QWidget, private Ui::QgsAuthConfig
3535
public:
3636
/**
3737
* Widget for editing authentication configurations directly in database
38+
* @param parent Parent widget
3839
* @param showUtilities Whether to show the widget's utilities button
39-
* @param msgbar Substitute internal message bar for another
40+
* @param relayMessages Whether to relay auth manager messages to internal message bar
4041
*/
41-
explicit QgsAuthConfigEditor( QWidget *parent = 0, bool showUtilities = true, QgsMessageBar *msgbar = 0 );
42+
explicit QgsAuthConfigEditor( QWidget *parent = 0, bool showUtilities = true, bool relayMessages = true );
4243
~QgsAuthConfigEditor();
4344

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

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

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

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

9192
private:
93+
bool mRelayMessages;
9294
QgsMessageBar * messageBar();
9395
int messageTimeout();
9496
QString selectedConfigId();

0 commit comments

Comments
 (0)