QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsauthconfigedit.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthconfigedit.cpp
3 ---------------------
4 begin : September 1, 2015
5 copyright : (C) 2015 by Boundless Spatial, Inc. USA
6 author : Larry Shaffer
7 email : lshaffer at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsauthconfigedit.h"
18#include "moc_qgsauthconfigedit.cpp"
19
20#include <QPushButton>
21
23#include "qgsauthconfig.h"
24#include "qgsauthconfigidedit.h"
25#include "qgsauthmanager.h"
26#include "qgsauthmethodedit.h"
27#include "qgslogger.h"
28#include "qgsapplication.h"
29#include "qgsgui.h"
30
31QgsAuthConfigEdit::QgsAuthConfigEdit( QWidget *parent, const QString &authcfg, const QString &dataprovider )
32 : QDialog( parent )
33 , mAuthCfg( authcfg )
34 , mDataProvider( dataprovider )
35
36{
37 const bool disabled = QgsApplication::authManager()->isDisabled();
38 bool idok = true;
39
40 if ( !disabled && !authcfg.isEmpty() )
41 {
42 idok = QgsApplication::authManager()->configIds().contains( authcfg );
43 }
44
45 if ( disabled || !idok )
46 {
47 mAuthNotifyLayout = new QVBoxLayout;
48 this->setLayout( mAuthNotifyLayout );
49
50 QString msg( disabled ? QgsApplication::authManager()->disabledMessage() : QString() );
51 if ( !authcfg.isEmpty() )
52 {
53 msg += "\n\n" + tr( "Authentication config id not loaded: %1" ).arg( authcfg );
54 }
55 mAuthNotify = new QLabel( msg, this );
56 mAuthNotifyLayout->addWidget( mAuthNotify );
57
58 mAuthCfg.clear(); // otherwise will continue to try authenticate (and fail) after save
59 buttonBox->button( QDialogButtonBox::Save )->setEnabled( false );
60 }
61 else
62 {
63 setupUi( this );
64 connect( btnClear, &QToolButton::clicked, this, &QgsAuthConfigEdit::btnClear_clicked );
65 connect( leName, &QLineEdit::textChanged, this, &QgsAuthConfigEdit::leName_textChanged );
66 connect( buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close );
67 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAuthConfigEdit::saveConfig );
68 connect( buttonBox->button( QDialogButtonBox::Reset ), &QAbstractButton::clicked, this, &QgsAuthConfigEdit::resetConfig );
69
70 populateAuthMethods();
71
72 connect( cmbAuthMethods, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), stkwAuthMethods, &QStackedWidget::setCurrentIndex );
73 connect( cmbAuthMethods, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [=] { validateAuth(); } );
74
75 connect( authCfgEdit, &QgsAuthConfigIdEdit::validityChanged, this, &QgsAuthConfigEdit::validateAuth );
76
77 // needed (if only combobox is ever changed)?
78 // connect( stkwAuthMethods, SIGNAL( currentChanged( int ) ),
79 // cmbAuthMethods, SLOT( setCurrentIndex( int ) ) );
80
81 // connect( stkwAuthMethods, SIGNAL( currentChanged( int ) ),
82 // this, SLOT( validateAuth() ) );
83
84 if ( cmbAuthMethods->count() > 0 )
85 {
86 cmbAuthMethods->setCurrentIndex( 0 );
87 stkwAuthMethods->setCurrentIndex( 0 );
88 }
89
90 loadConfig();
91 validateAuth();
92
93 leName->setFocus();
94 }
95
97}
98
99void QgsAuthConfigEdit::populateAuthMethods()
100{
101 const QStringList authMethodKeys = QgsApplication::authManager()->authMethodsKeys( mDataProvider );
102
103 // sort by auth method description attribute, then populate
104 QMap<QString, const QgsAuthMethodMetadata *> descmap;
105 const auto constAuthMethodKeys = authMethodKeys;
106 for ( const QString &authMethodKey : constAuthMethodKeys )
107 {
109 if ( !meta )
110 {
111 QgsDebugError( QStringLiteral( "Load auth method instance FAILED for auth method key (%1)" ).arg( authMethodKey ) );
112 continue;
113 }
114 descmap.insert( meta->description(), meta );
115 }
116
117 QMap<QString, const QgsAuthMethodMetadata *>::iterator it = descmap.begin();
118 for ( it = descmap.begin(); it != descmap.end(); ++it )
119 {
120 QgsAuthMethodEdit *editWidget = qobject_cast<QgsAuthMethodEdit *>(
121 QgsApplication::authManager()->authMethodEditWidget( it.value()->key(), this )
122 );
123 if ( !editWidget )
124 {
125 QgsDebugError( QStringLiteral( "Load auth method edit widget FAILED for auth method key (%1)" ).arg( it.value()->key() ) );
126 continue;
127 }
128 connect( editWidget, &QgsAuthMethodEdit::validityChanged, this, &QgsAuthConfigEdit::validateAuth );
129
130 cmbAuthMethods->addItem( it.key(), QVariant( it.value()->key() ) );
131 stkwAuthMethods->addWidget( editWidget );
132 }
133}
134
135void QgsAuthConfigEdit::loadConfig()
136{
137 const bool emptyAuthCfg = mAuthCfg.isEmpty();
138 authCfgEdit->setAllowEmptyId( emptyAuthCfg );
139 if ( emptyAuthCfg )
140 {
141 return;
142 }
143
144 // edit mode requires master password to have been set and verified against auth db
145 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
146 {
147 mAuthCfg.clear();
148 return;
149 }
150
151 QgsAuthMethodConfig mconfig;
152 if ( !QgsApplication::authManager()->loadAuthenticationConfig( mAuthCfg, mconfig, true ) )
153 {
154 QgsDebugError( QStringLiteral( "Loading FAILED for authcfg: %1" ).arg( mAuthCfg ) );
155 return;
156 }
157
158 if ( !mconfig.isValid( true ) )
159 {
160 QgsDebugError( QStringLiteral( "Loading FAILED for authcfg (%1): invalid config" ).arg( mAuthCfg ) );
161 return;
162 }
163
164 // load basic info
165 leName->setText( mconfig.name() );
166 leResource->setText( mconfig.uri() );
167 authCfgEdit->setAuthConfigId( mconfig.id() );
168
169 const QString authMethodKey = QgsApplication::authManager()->configAuthMethodKey( mAuthCfg );
170
171 QgsDebugMsgLevel( QStringLiteral( "Loading authcfg: %1" ).arg( mAuthCfg ), 2 );
172 QgsDebugMsgLevel( QStringLiteral( "Loading auth method: %1" ).arg( authMethodKey ), 2 );
173
174 if ( authMethodKey.isEmpty() )
175 {
176 QgsDebugError( QStringLiteral( "Loading FAILED for authcfg (%1): no auth method found" ).arg( mAuthCfg ) );
177 return;
178 }
179
180 if ( mconfig.method() != authMethodKey )
181 {
182 QgsDebugError( QStringLiteral( "Loading FAILED for authcfg (%1): auth method and key mismatch" ).arg( mAuthCfg ) );
183 return;
184 }
185
186 const int indx = authMethodIndex( authMethodKey );
187 if ( indx == -1 )
188 {
189 QgsDebugError( QStringLiteral( "Loading FAILED for authcfg (%1): no edit widget loaded for auth method '%2'" )
190 .arg( mAuthCfg, authMethodKey ) );
191 if ( cmbAuthMethods->count() > 0 )
192 {
193 cmbAuthMethods->setCurrentIndex( 0 );
194 stkwAuthMethods->setCurrentIndex( 0 );
195 }
196 return;
197 }
198
199 cmbAuthMethods->setCurrentIndex( indx );
200 stkwAuthMethods->setCurrentIndex( indx );
201
202 QgsAuthMethodEdit *editWidget = currentEditWidget();
203 if ( !editWidget )
204 {
205 QgsDebugError( QStringLiteral( "Cast to edit widget FAILED for authcfg (%1) and auth method key (%2)" )
206 .arg( mAuthCfg, authMethodKey ) );
207 return;
208 }
209
210 editWidget->loadConfig( mconfig.configMap() );
211}
212
213void QgsAuthConfigEdit::resetConfig()
214{
215 clearAll();
216 loadConfig();
217 validateAuth();
218}
219
220void QgsAuthConfigEdit::saveConfig()
221{
222 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
223 return;
224
225 const QString authMethodKey = cmbAuthMethods->currentData().toString();
226
227 QgsAuthMethodEdit *editWidget = currentEditWidget();
228 if ( !editWidget )
229 {
230 QgsDebugError( QStringLiteral( "Cast to edit widget FAILED)" ) );
231 return;
232 }
233
234 QgsAuthMethod *authmethod = QgsApplication::authManager()->authMethod( authMethodKey );
235 if ( !authmethod )
236 {
237 QgsDebugError( QStringLiteral( "Save auth config FAILED when loading auth method instance from key (%1)" ).arg( authMethodKey ) );
238 return;
239 }
240
241 QgsAuthMethodConfig mconfig;
242 mconfig.setName( leName->text() );
243 mconfig.setUri( leResource->text() );
244 mconfig.setMethod( authMethodKey );
245 mconfig.setVersion( authmethod->version() );
246 mconfig.setConfigMap( editWidget->configMap() );
247
248 if ( !mconfig.isValid() )
249 {
250 QgsDebugError( QStringLiteral( "Save auth config FAILED: config invalid" ) );
251 return;
252 }
253
254 const QString authCfgId( authCfgEdit->configId() );
255 if ( !mAuthCfg.isEmpty() )
256 {
257 if ( authCfgId == mAuthCfg ) // update
258 {
259 mconfig.setId( mAuthCfg );
260 if ( QgsApplication::authManager()->updateAuthenticationConfig( mconfig ) )
261 {
262 emit authenticationConfigUpdated( mAuthCfg );
263 }
264 else
265 {
266 QgsDebugError( QStringLiteral( "Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
267 }
268 }
269 else // store new with unique ID, then delete previous
270 {
271 mconfig.setId( authCfgId );
272 if ( QgsApplication::authManager()->storeAuthenticationConfig( mconfig ) )
273 {
274 emit authenticationConfigStored( authCfgId );
275 if ( !QgsApplication::authManager()->removeAuthenticationConfig( mAuthCfg ) )
276 {
277 QgsDebugError( QStringLiteral( "Removal of older auth config FAILED" ) );
278 }
279 mAuthCfg = authCfgId;
280 }
281 else
282 {
283 QgsDebugError( QStringLiteral( "Storing new auth config with user-created unique ID FAILED" ) );
284 }
285 }
286 }
287 else if ( mAuthCfg.isEmpty() )
288 {
289 if ( authCfgId.isEmpty() ) // create new with generated ID
290 {
291 if ( QgsApplication::authManager()->storeAuthenticationConfig( mconfig ) )
292 {
293 mAuthCfg = mconfig.id();
294 emit authenticationConfigStored( mAuthCfg );
295 }
296 else
297 {
298 QgsDebugError( QStringLiteral( "Storing new auth config FAILED" ) );
299 }
300 }
301 else // create new with user-created unique ID
302 {
303 mconfig.setId( authCfgId );
304 if ( QgsApplication::authManager()->storeAuthenticationConfig( mconfig ) )
305 {
306 mAuthCfg = authCfgId;
307 emit authenticationConfigStored( mAuthCfg );
308 }
309 else
310 {
311 QgsDebugError( QStringLiteral( "Storing new auth config with user-created unique ID FAILED" ) );
312 }
313 }
314 }
315
316 this->accept();
317}
318
319void QgsAuthConfigEdit::btnClear_clicked()
320{
321 QgsAuthMethodEdit *editWidget = currentEditWidget();
322 if ( !editWidget )
323 {
324 QgsDebugError( QStringLiteral( "Cast to edit widget FAILED)" ) );
325 return;
326 }
327
328 editWidget->clearConfig();
329
330 validateAuth();
331}
332
333void QgsAuthConfigEdit::clearAll()
334{
335 leName->clear();
336 leResource->clear();
337 authCfgEdit->clear();
338
339 for ( int i = 0; i < stkwAuthMethods->count(); i++ )
340 {
341 QgsAuthMethodEdit *editWidget = qobject_cast<QgsAuthMethodEdit *>( stkwAuthMethods->widget( i ) );
342 if ( editWidget )
343 {
344 editWidget->clearConfig();
345 }
346 }
347
348 validateAuth();
349}
350
351void QgsAuthConfigEdit::validateAuth()
352{
353 bool authok = !leName->text().isEmpty();
354
355 QgsAuthMethodEdit *editWidget = currentEditWidget();
356 if ( !editWidget )
357 {
358 QgsDebugError( QStringLiteral( "Cast to edit widget FAILED" ) );
359 }
360 else
361 {
362 authok = authok && editWidget->validateConfig();
363 }
364 authok = authok && authCfgEdit->validate();
365
366 buttonBox->button( QDialogButtonBox::Save )->setEnabled( authok );
367}
368
369void QgsAuthConfigEdit::leName_textChanged( const QString &txt )
370{
371 Q_UNUSED( txt )
372 validateAuth();
373}
374
375int QgsAuthConfigEdit::authMethodIndex( const QString &authMethodKey )
376{
377 return cmbAuthMethods->findData( QVariant( authMethodKey ) );
378}
379
380QgsAuthMethodEdit *QgsAuthConfigEdit::currentEditWidget()
381{
382 return qobject_cast<QgsAuthMethodEdit *>( stkwAuthMethods->currentWidget() );
383}
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void authenticationConfigStored(const QString &authcfg)
Emit generated id when a new config is saved to auth database.
void authenticationConfigUpdated(const QString &authcfg)
Emit current id when an existing config is updated in auth database.
QgsAuthConfigEdit(QWidget *parent=nullptr, const QString &authcfg=QString(), const QString &dataprovider=QString())
Create a dialog for editing an authentication configuration.
void validityChanged(bool valid)
Validity of the ID has changed.
bool isDisabled() const
Whether QCA has the qca-ossl plugin, which a base run-time requirement.
QStringList configIds() const
Gets list of authentication ids from database.
QgsAuthMethod * authMethod(const QString &authMethodKey)
Gets authentication method from the config/provider cache via its key.
QStringList authMethodsKeys(const QString &dataprovider=QString())
Gets keys of supported authentication methods.
const QgsAuthMethodMetadata * authMethodMetadata(const QString &authMethodKey)
Gets authentication method metadata via its key.
QString configAuthMethodKey(const QString &authcfg) const
Gets key of authentication method associated with config ID.
Configuration storage class for authentication method configurations.
bool isValid(bool validateid=false) const
Whether the configuration is valid.
QString method() const
Textual key of the associated authentication method.
const QString uri() const
A URI to auto-select a config when connecting to a resource.
void setName(const QString &name)
Sets name of configuration.
void setConfigMap(const QgsStringMap &map)
Set extended configuration map.
void setVersion(int version)
Sets version of the configuration.
const QString name() const
Gets name of configuration.
const QString id() const
Gets 'authcfg' 7-character alphanumeric ID of the config.
QgsStringMap configMap() const
Gets extended configuration, mapped to key/value pairs of QStrings.
void setMethod(const QString &method)
void setUri(const QString &uri)
void setId(const QString &id)
Sets auth config ID.
Abstract base class for the edit widget of authentication method plugins.
virtual bool validateConfig()=0
Validate the configuration of subclasses.
void validityChanged(bool valid)
Emitted when the configuration validatity changes.
virtual void clearConfig()=0
Clear GUI controls in subclassed widget.
virtual QgsStringMap configMap() const =0
The configuration key-vale map of subclasses.
virtual void loadConfig(const QgsStringMap &configmap)=0
Load an existing config map into subclassed widget.
Holds data auth method key, description, and associated shared library file information.
QString description() const
Returns descriptive text for the method.
Abstract base class for authentication method plugins.
int version() const
Increment this if method is significantly updated, allow updater code to be written for previously st...
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38