QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgsauthmethodmetadata.h"
32 
33 
35  : QDialog( parent )
36 
37 {
38  if ( QgsApplication::authManager()->isDisabled() )
39  {
40  mAuthNotifyLayout = new QVBoxLayout;
41  this->setLayout( mAuthNotifyLayout );
42  mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
43  mAuthNotifyLayout->addWidget( mAuthNotify );
44  }
45  else
46  {
47  setupUi( this );
48  connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
49 
50  setupTable();
51  populateTable();
52  }
53 }
54 
55 void QgsAuthMethodPlugins::setupTable()
56 {
57  tblAuthPlugins->setColumnCount( 3 );
58  tblAuthPlugins->verticalHeader()->hide();
59  tblAuthPlugins->horizontalHeader()->setVisible( true );
60  tblAuthPlugins->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Method" ) ) );
61  tblAuthPlugins->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "Description" ) ) );
62  tblAuthPlugins->setHorizontalHeaderItem( 2, new QTableWidgetItem( tr( "Works with" ) ) );
63  tblAuthPlugins->horizontalHeader()->setStretchLastSection( true );
64  tblAuthPlugins->setAlternatingRowColors( true );
65  tblAuthPlugins->setColumnWidth( 0, 150 );
66  tblAuthPlugins->setColumnWidth( 1, 300 );
67  tblAuthPlugins->setRowCount( QgsApplication::authManager()->authMethodsKeys().size() );
68  tblAuthPlugins->verticalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );
69  tblAuthPlugins->setSortingEnabled( true );
70  tblAuthPlugins->setSelectionBehavior( QAbstractItemView::SelectRows );
71 }
72 
73 void QgsAuthMethodPlugins::populateTable()
74 {
75  const QStringList authMethodKeys = QgsApplication::authManager()->authMethodsKeys();
76 
77  int i = 0;
78  const auto constAuthMethodKeys = authMethodKeys;
79  for ( const QString &authMethodKey : constAuthMethodKeys )
80  {
82  const QgsAuthMethod *method = QgsApplication::authManager()->authMethod( authMethodKey );
83  if ( !meta || !method )
84  {
85  QgsDebugMsg( QStringLiteral( "Load auth method instance FAILED for auth method key (%1)" ).arg( authMethodKey ) );
86  continue;
87  }
88 
89  QTableWidgetItem *twi = new QTableWidgetItem( meta->key() );
90  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
91  tblAuthPlugins->setItem( i, 0, twi );
92 
93  twi = new QTableWidgetItem( meta->description() );
94  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
95  tblAuthPlugins->setItem( i, 1, twi );
96 
97  twi = new QTableWidgetItem( method->supportedDataProviders().join( QLatin1String( ", " ) ) );
98  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
99  tblAuthPlugins->setItem( i, 2, twi );
100 
101  i++;
102  }
103  tblAuthPlugins->sortItems( 0 );
104 }
105 
106 
107 
109  : QWidget( parent )
110 
111 {
112  setupUi( this );
113  connect( btnCertManager, &QPushButton::clicked, this, &QgsAuthEditorWidgets::btnCertManager_clicked );
114  connect( btnAuthPlugins, &QPushButton::clicked, this, &QgsAuthEditorWidgets::btnAuthPlugins_clicked );
115  if ( !QgsApplication::authManager()->isDisabled() )
116  {
117  wdgtConfigEditor->setRelayMessages( false );
118  wdgtConfigEditor->setShowUtilitiesButton( false );
119  setupUtilitiesMenu();
120  }
121  else
122  {
123  grpbxManagers->setEnabled( false );
124  }
125 }
126 
127 void QgsAuthEditorWidgets::btnCertManager_clicked()
128 {
129  QgsAuthCertManager *dlg = new QgsAuthCertManager( this );
130  dlg->setWindowModality( Qt::ApplicationModal );
131  dlg->resize( 750, 500 );
132  dlg->exec();
133  dlg->deleteLater();
134 }
135 
136 void QgsAuthEditorWidgets::btnAuthPlugins_clicked()
137 {
138  QgsAuthMethodPlugins *dlg = new QgsAuthMethodPlugins( this );
139  dlg->setWindowModality( Qt::WindowModal );
140  dlg->resize( 675, 500 );
141  dlg->exec();
142  dlg->deleteLater();
143 }
144 
145 void QgsAuthEditorWidgets::setupUtilitiesMenu()
146 {
148  this, &QgsAuthEditorWidgets::authMessageOut );
149 
150  // set up utility actions menu
151  mActionImportAuthenticationConfigs = new QAction( tr( "Import authentication configurations from file" ), this );
152  mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export selected authentication configurations to file" ), this );
153  mActionSetMasterPassword = new QAction( tr( "Input master password" ), this );
154  mActionClearCachedMasterPassword = new QAction( tr( "Clear cached master password" ), this );
155  mActionResetMasterPassword = new QAction( tr( "Reset master password" ), this );
156  mActionClearCachedAuthConfigs = new QAction( tr( "Clear cached authentication configurations" ), this );
157  mActionRemoveAuthConfigs = new QAction( tr( "Remove all authentication configurations" ), this );
158  mActionEraseAuthDatabase = new QAction( tr( "Erase authentication database" ), this );
159 
160  mActionClearAccessCacheNow = new QAction( tr( "Clear network authentication access cache" ), this );
161  mActionAutoClearAccessCache = new QAction( tr( "Automatically clear network authentication access cache on SSL errors" ), this );
162  mActionAutoClearAccessCache->setCheckable( true );
163  mActionAutoClearAccessCache->setChecked( QgsSettings().value( QStringLiteral( "clear_auth_cache_on_errors" ), true, QgsSettings::Section::Auth ).toBool( ) );
164 
165  mActionPasswordHelperSync = new QAction( tr( "Store/update the master password in your %1" )
167  mActionPasswordHelperDelete = new QAction( tr( "Clear the master password from your %1" )
169  mActionPasswordHelperEnable = new QAction( tr( "Integrate master password with your %1" )
171  mActionPasswordHelperLoggingEnable = new QAction( tr( "Enable password helper debug log" ), this );
172 
173  mActionPasswordHelperEnable->setCheckable( true );
174  mActionPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
175 
176  mActionPasswordHelperLoggingEnable->setCheckable( true );
177  mActionPasswordHelperLoggingEnable->setChecked( QgsApplication::authManager()->passwordHelperLoggingEnabled() );
178 
179  connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::importAuthenticationConfigs );
180  connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::exportSelectedAuthenticationConfigs );
181  connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::setMasterPassword );
182  connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedMasterPassword );
183  connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::resetMasterPassword );
184  connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedAuthenticationConfigs );
185  connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::removeAuthenticationConfigs );
186  connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthEditorWidgets::eraseAuthenticationDatabase );
187 
188  connect( mActionPasswordHelperSync, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperSync );
189  connect( mActionPasswordHelperDelete, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperDelete );
190  connect( mActionPasswordHelperEnable, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperEnableTriggered );
191  connect( mActionPasswordHelperLoggingEnable, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperLoggingEnableTriggered );
192 
193  connect( mActionClearAccessCacheNow, &QAction::triggered, this, [ = ]
194  {
195  QgsNetworkAccessManager::instance()->clearAccessCache();
196  messageBar()->pushSuccess( tr( "Auth cache cleared" ), tr( "Network authentication cache has been cleared" ) );
197  } );
198  connect( mActionAutoClearAccessCache, &QAction::triggered, this, [ ]( bool checked )
199  {
200  QgsSettings().setValue( QStringLiteral( "clear_auth_cache_on_errors" ), checked, QgsSettings::Section::Auth );
201  } );
202 
203  mAuthUtilitiesMenu = new QMenu( this );
204  mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
205  mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
206  mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
207  mAuthUtilitiesMenu->addSeparator();
208  mAuthUtilitiesMenu->addAction( mActionClearAccessCacheNow );
209  mAuthUtilitiesMenu->addAction( mActionAutoClearAccessCache );
210  mAuthUtilitiesMenu->addSeparator();
211  mAuthUtilitiesMenu->addAction( mActionPasswordHelperEnable );
212  mAuthUtilitiesMenu->addAction( mActionPasswordHelperSync );
213  mAuthUtilitiesMenu->addAction( mActionPasswordHelperDelete );
214  mAuthUtilitiesMenu->addAction( mActionPasswordHelperLoggingEnable );
215  mAuthUtilitiesMenu->addSeparator();
216  mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
217  mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
218  mAuthUtilitiesMenu->addSeparator();
219  mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
220  mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
221  mAuthUtilitiesMenu->addSeparator();
222  mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
223 
224  btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
225 }
226 
227 void QgsAuthEditorWidgets::importAuthenticationConfigs()
228 {
230 }
231 
232 void QgsAuthEditorWidgets::exportSelectedAuthenticationConfigs()
233 {
234  if ( !wdgtConfigEditor )
235  return;
236 
237  QgsAuthGuiUtils::exportSelectedAuthenticationConfigs( wdgtConfigEditor->selectedAuthenticationConfigIds(), messageBar() );
238 }
239 
240 void QgsAuthEditorWidgets::setMasterPassword()
241 {
242  QgsAuthGuiUtils::setMasterPassword( messageBar() );
243 }
244 
245 void QgsAuthEditorWidgets::clearCachedMasterPassword()
246 {
248 }
249 
250 void QgsAuthEditorWidgets::resetMasterPassword()
251 {
252  QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
253 }
254 
255 void QgsAuthEditorWidgets::clearCachedAuthenticationConfigs()
256 {
258 }
259 
260 void QgsAuthEditorWidgets::removeAuthenticationConfigs()
261 {
262  QgsAuthGuiUtils::removeAuthenticationConfigs( messageBar(), this );
263 }
264 
265 void QgsAuthEditorWidgets::eraseAuthenticationDatabase()
266 {
267  QgsAuthGuiUtils::eraseAuthenticationDatabase( messageBar(), this );
268 }
269 
270 void QgsAuthEditorWidgets::authMessageOut( const QString &message, const QString &authtag, QgsAuthManager::MessageLevel level )
271 {
272  const int levelint = static_cast<int>( level );
273  messageBar()->pushMessage( authtag, message, ( Qgis::MessageLevel )levelint );
274 }
275 
276 void QgsAuthEditorWidgets::passwordHelperDelete()
277 {
278  QgsAuthGuiUtils::passwordHelperDelete( messageBar(), this );
279 }
280 
281 void QgsAuthEditorWidgets::passwordHelperSync()
282 {
283  QgsAuthGuiUtils::passwordHelperSync( messageBar() );
284 }
285 
286 void QgsAuthEditorWidgets::passwordHelperEnableTriggered()
287 {
288  // Only fire on real changes
289  QgsAuthGuiUtils::passwordHelperEnable( mActionPasswordHelperEnable->isChecked(), messageBar() );
290 }
291 
292 void QgsAuthEditorWidgets::passwordHelperLoggingEnableTriggered()
293 {
294  QgsAuthGuiUtils::passwordHelperLoggingEnable( mActionPasswordHelperLoggingEnable->isChecked(), messageBar() );
295 }
296 
297 QgsMessageBar *QgsAuthEditorWidgets::messageBar()
298 {
299  return mMsgBar;
300 }
301 
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:107
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.
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.
static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME
The display name of the password helper (platform dependent)
const QgsAuthMethodMetadata * authMethodMetadata(const QString &authMethodKey)
Gets authentication method metadata via its key.
Holds data auth method key, description, and associated shared library file information.
QString description() const
Returns descriptive text for the method.
QString key() const
Returns the unique key associated with the method.
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:43
QStringList supportedDataProviders() const
The data providers that the method supports, allowing for filtering out authcfgs that are not applica...
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.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38