QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsautheditorwidgets.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsautheditorwidgets.cpp
3  ---------------------
4  begin : April 26, 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 "qgsautheditorwidgets.h"
18 #include "ui_qgsauthmethodplugins.h"
19 
20 #include <QAction>
21 #include <QMenu>
22 #include <QWidget>
23 #include <QTableWidget>
24 
25 #include "qgssettings.h"
27 #include "qgsauthguiutils.h"
28 #include "qgsauthmanager.h"
29 #include "qgsapplication.h"
31 
32 
34  : QDialog( parent )
35 
36 {
37  if ( QgsApplication::authManager()->isDisabled() )
38  {
39  mAuthNotifyLayout = new QVBoxLayout;
40  this->setLayout( mAuthNotifyLayout );
41  mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
42  mAuthNotifyLayout->addWidget( mAuthNotify );
43  }
44  else
45  {
46  setupUi( this );
47  connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
48 
49  setupTable();
50  populateTable();
51  }
52 }
53 
54 void QgsAuthMethodPlugins::setupTable()
55 {
56  tblAuthPlugins->setColumnCount( 3 );
57  tblAuthPlugins->verticalHeader()->hide();
58  tblAuthPlugins->horizontalHeader()->setVisible( true );
59  tblAuthPlugins->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Method" ) ) );
60  tblAuthPlugins->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "Description" ) ) );
61  tblAuthPlugins->setHorizontalHeaderItem( 2, new QTableWidgetItem( tr( "Works with" ) ) );
62  tblAuthPlugins->horizontalHeader()->setStretchLastSection( true );
63  tblAuthPlugins->setAlternatingRowColors( true );
64  tblAuthPlugins->setColumnWidth( 0, 150 );
65  tblAuthPlugins->setColumnWidth( 1, 300 );
66  tblAuthPlugins->setRowCount( QgsApplication::authManager()->authMethodsKeys().size() );
67  tblAuthPlugins->verticalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );
68  tblAuthPlugins->setSortingEnabled( true );
69  tblAuthPlugins->setSelectionBehavior( QAbstractItemView::SelectRows );
70 }
71 
72 void QgsAuthMethodPlugins::populateTable()
73 {
74  QgsAuthMethodsMap authmethods( QgsApplication::authManager()->authMethodsMap() );
75 
76  int i = 0;
77  for ( QgsAuthMethodsMap::const_iterator it = authmethods.constBegin(); it != authmethods.constEnd(); ++it, i++ )
78  {
79  QgsAuthMethod *authmethod( it.value() );
80  if ( !authmethod )
81  {
82  continue;
83  }
84 
85  QTableWidgetItem *twi = new QTableWidgetItem( authmethod->key() );
86  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
87  tblAuthPlugins->setItem( i, 0, twi );
88 
89  twi = new QTableWidgetItem( authmethod->displayDescription() );
90  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
91  tblAuthPlugins->setItem( i, 1, twi );
92 
93  twi = new QTableWidgetItem( authmethod->supportedDataProviders().join( QLatin1String( ", " ) ) );
94  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
95  tblAuthPlugins->setItem( i, 2, twi );
96  }
97  tblAuthPlugins->sortItems( 0 );
98 }
99 
100 
101 
103  : QWidget( parent )
104 
105 {
106  setupUi( this );
107  connect( btnCertManager, &QPushButton::clicked, this, &QgsAuthEditorWidgets::btnCertManager_clicked );
108  connect( btnAuthPlugins, &QPushButton::clicked, this, &QgsAuthEditorWidgets::btnAuthPlugins_clicked );
109  if ( !QgsApplication::authManager()->isDisabled() )
110  {
111  wdgtConfigEditor->setRelayMessages( false );
112  wdgtConfigEditor->setShowUtilitiesButton( false );
113  setupUtilitiesMenu();
114  }
115  else
116  {
117  grpbxManagers->setEnabled( false );
118  }
119 }
120 
121 void QgsAuthEditorWidgets::btnCertManager_clicked()
122 {
123  QgsAuthCertManager *dlg = new QgsAuthCertManager( this );
124  dlg->setWindowModality( Qt::ApplicationModal );
125  dlg->resize( 750, 500 );
126  dlg->exec();
127  dlg->deleteLater();
128 }
129 
130 void QgsAuthEditorWidgets::btnAuthPlugins_clicked()
131 {
132  QgsAuthMethodPlugins *dlg = new QgsAuthMethodPlugins( this );
133  dlg->setWindowModality( Qt::WindowModal );
134  dlg->resize( 675, 500 );
135  dlg->exec();
136  dlg->deleteLater();
137 }
138 
139 void QgsAuthEditorWidgets::setupUtilitiesMenu()
140 {
142  this, &QgsAuthEditorWidgets::authMessageOut );
143 
144  // set up utility actions menu
145  mActionImportAuthenticationConfigs = new QAction( tr( "Import authentication configurations from file" ), this );
146  mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export selected authentication configurations to file" ), this );
147  mActionSetMasterPassword = new QAction( tr( "Input master password" ), this );
148  mActionClearCachedMasterPassword = new QAction( tr( "Clear cached master password" ), this );
149  mActionResetMasterPassword = new QAction( tr( "Reset master password" ), this );
150  mActionClearCachedAuthConfigs = new QAction( tr( "Clear cached authentication configurations" ), this );
151  mActionRemoveAuthConfigs = new QAction( tr( "Remove all authentication configurations" ), this );
152  mActionEraseAuthDatabase = new QAction( tr( "Erase authentication database" ), this );
153 
154  mActionClearAccessCacheNow = new QAction( tr( "Clear network authentication access cache" ), this );
155  mActionAutoClearAccessCache = new QAction( tr( "Automatically clear network authentication access cache on SSL errors" ), this );
156  mActionAutoClearAccessCache->setCheckable( true );
157  mActionAutoClearAccessCache->setChecked( QgsSettings().value( QStringLiteral( "clear_auth_cache_on_errors" ), true, QgsSettings::Section::Auth ).toBool( ) );
158 
159  mActionPasswordHelperSync = new QAction( tr( "Store/update the master password in your %1" )
161  mActionPasswordHelperDelete = new QAction( tr( "Clear the master password from your %1" )
163  mActionPasswordHelperEnable = new QAction( tr( "Integrate master password with your %1" )
165  mActionPasswordHelperLoggingEnable = new QAction( tr( "Enable password helper debug log" ), this );
166 
167  mActionPasswordHelperEnable->setCheckable( true );
168  mActionPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
169 
170  mActionPasswordHelperLoggingEnable->setCheckable( true );
171  mActionPasswordHelperLoggingEnable->setChecked( QgsApplication::authManager()->passwordHelperLoggingEnabled() );
172 
173  connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::importAuthenticationConfigs );
174  connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::exportSelectedAuthenticationConfigs );
175  connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::setMasterPassword );
176  connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedMasterPassword );
177  connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::resetMasterPassword );
178  connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedAuthenticationConfigs );
179  connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::removeAuthenticationConfigs );
180  connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthEditorWidgets::eraseAuthenticationDatabase );
181 
182  connect( mActionPasswordHelperSync, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperSync );
183  connect( mActionPasswordHelperDelete, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperDelete );
184  connect( mActionPasswordHelperEnable, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperEnableTriggered );
185  connect( mActionPasswordHelperLoggingEnable, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperLoggingEnableTriggered );
186 
187  connect( mActionClearAccessCacheNow, &QAction::triggered, this, [ = ]
188  {
189  QgsNetworkAccessManager::instance()->clearAccessCache();
190  messageBar()->pushSuccess( tr( "Auth cache cleared" ), tr( "Network authentication cache has been cleared" ) );
191  } );
192  connect( mActionAutoClearAccessCache, &QAction::triggered, this, [ ]( bool checked )
193  {
194  QgsSettings().setValue( QStringLiteral( "clear_auth_cache_on_errors" ), checked, QgsSettings::Section::Auth );
195  } );
196 
197  mAuthUtilitiesMenu = new QMenu( this );
198  mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
199  mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
200  mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
201  mAuthUtilitiesMenu->addSeparator();
202  mAuthUtilitiesMenu->addAction( mActionClearAccessCacheNow );
203  mAuthUtilitiesMenu->addAction( mActionAutoClearAccessCache );
204  mAuthUtilitiesMenu->addSeparator();
205  mAuthUtilitiesMenu->addAction( mActionPasswordHelperEnable );
206  mAuthUtilitiesMenu->addAction( mActionPasswordHelperSync );
207  mAuthUtilitiesMenu->addAction( mActionPasswordHelperDelete );
208  mAuthUtilitiesMenu->addAction( mActionPasswordHelperLoggingEnable );
209  mAuthUtilitiesMenu->addSeparator();
210  mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
211  mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
212  mAuthUtilitiesMenu->addSeparator();
213  mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
214  mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
215  mAuthUtilitiesMenu->addSeparator();
216  mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
217 
218  btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
219 }
220 
221 void QgsAuthEditorWidgets::importAuthenticationConfigs()
222 {
224 }
225 
226 void QgsAuthEditorWidgets::exportSelectedAuthenticationConfigs()
227 {
228  if ( !wdgtConfigEditor )
229  return;
230 
231  QgsAuthGuiUtils::exportSelectedAuthenticationConfigs( wdgtConfigEditor->selectedAuthenticationConfigIds(), messageBar() );
232 }
233 
234 void QgsAuthEditorWidgets::setMasterPassword()
235 {
236  QgsAuthGuiUtils::setMasterPassword( messageBar() );
237 }
238 
239 void QgsAuthEditorWidgets::clearCachedMasterPassword()
240 {
242 }
243 
244 void QgsAuthEditorWidgets::resetMasterPassword()
245 {
246  QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
247 }
248 
249 void QgsAuthEditorWidgets::clearCachedAuthenticationConfigs()
250 {
252 }
253 
254 void QgsAuthEditorWidgets::removeAuthenticationConfigs()
255 {
256  QgsAuthGuiUtils::removeAuthenticationConfigs( messageBar(), this );
257 }
258 
259 void QgsAuthEditorWidgets::eraseAuthenticationDatabase()
260 {
261  QgsAuthGuiUtils::eraseAuthenticationDatabase( messageBar(), this );
262 }
263 
264 void QgsAuthEditorWidgets::authMessageOut( const QString &message, const QString &authtag, QgsAuthManager::MessageLevel level )
265 {
266  int levelint = static_cast<int>( level );
267  messageBar()->pushMessage( authtag, message, ( Qgis::MessageLevel )levelint );
268 }
269 
270 void QgsAuthEditorWidgets::passwordHelperDelete()
271 {
272  QgsAuthGuiUtils::passwordHelperDelete( messageBar(), this );
273 }
274 
275 void QgsAuthEditorWidgets::passwordHelperSync()
276 {
277  QgsAuthGuiUtils::passwordHelperSync( messageBar() );
278 }
279 
280 void QgsAuthEditorWidgets::passwordHelperEnableTriggered()
281 {
282  // Only fire on real changes
283  QgsAuthGuiUtils::passwordHelperEnable( mActionPasswordHelperEnable->isChecked(), messageBar() );
284 }
285 
286 void QgsAuthEditorWidgets::passwordHelperLoggingEnableTriggered()
287 {
288  QgsAuthGuiUtils::passwordHelperLoggingEnable( mActionPasswordHelperLoggingEnable->isChecked(), messageBar() );
289 }
290 
291 QgsMessageBar *QgsAuthEditorWidgets::messageBar()
292 {
293  return mMsgBar;
294 }
295 
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:105
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Dialog wrapper for widget to manage available certificate editors.
QgsAuthEditorWidgets(QWidget *parent=nullptr)
Construct a widget to contain various authentication editors.
static void importAuthenticationConfigs(QgsMessageBar *msgbar)
Import authentication configurations from a XML file.
static void exportSelectedAuthenticationConfigs(QStringList authenticationConfigIds, QgsMessageBar *msgbar)
Exports selected authentication configurations to a XML file.
static void resetMasterPassword(QgsMessageBar *msgbar, QWidget *parent=nullptr)
Reset the cached master password, updating its hash in authentication database and resetting all exis...
static void clearCachedMasterPassword(QgsMessageBar *msgbar)
Clear the currently cached master password (not its hash in database)
static void passwordHelperEnable(bool enabled, QgsMessageBar *msgbar)
Sets password helper enabled (enable/disable)
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar)
Clear all cached authentication configs for session.
static void passwordHelperLoggingEnable(bool enabled, QgsMessageBar *msgbar, int timeout=0)
Sets password helper logging enabled (enable/disable)
static void eraseAuthenticationDatabase(QgsMessageBar *msgbar, QWidget *parent=nullptr)
Completely clear out the authentication database (configs and master password)
static void removeAuthenticationConfigs(QgsMessageBar *msgbar, QWidget *parent=nullptr)
Remove all authentication configs.
static void setMasterPassword(QgsMessageBar *msgbar)
Sets the cached master password (and verifies it if its hash is in authentication database)
static void passwordHelperDelete(QgsMessageBar *msgbar, QWidget *parent=nullptr)
Remove master password from wallet.
static void passwordHelperSync(QgsMessageBar *msgbar)
Store master password into the wallet.
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
void messageOut(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level=QgsAuthManager::INFO) const
Custom logging signal to relay to console output and QgsMessageLog.
static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME
The display name of the password helper (platform dependent)
Dialog for viewing available authentication method plugins.
QgsAuthMethodPlugins(QWidget *parent=nullptr)
Construct a dialog for viewing available authentication method plugins.
Abstract base class for authentication method plugins.
Definition: qgsauthmethod.h:42
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
QHash< QString, QgsAuthMethod * > QgsAuthMethodsMap