QGIS API Documentation  3.6.0-Noosa (5873452)
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( QStringLiteral( ", " ) ) );
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  mActionSetMasterPassword = new QAction( tr( "Input master password" ), this );
146  mActionClearCachedMasterPassword = new QAction( tr( "Clear cached master password" ), this );
147  mActionResetMasterPassword = new QAction( tr( "Reset master password" ), this );
148  mActionClearCachedAuthConfigs = new QAction( tr( "Clear cached authentication configurations" ), this );
149  mActionRemoveAuthConfigs = new QAction( tr( "Remove all authentication configurations" ), this );
150  mActionEraseAuthDatabase = new QAction( tr( "Erase authentication database" ), this );
151 
152  mActionClearAccessCacheNow = new QAction( tr( "Clear network authentication access cache" ), this );
153  mActionAutoClearAccessCache = new QAction( tr( "Automatically clear network authentication access cache on SSL errors" ), this );
154  mActionAutoClearAccessCache->setCheckable( true );
155  mActionAutoClearAccessCache->setChecked( QgsSettings().value( QStringLiteral( "clear_auth_cache_on_errors" ), true, QgsSettings::Section::Auth ).toBool( ) );
156 
157  mActionPasswordHelperSync = new QAction( tr( "Store/update the master password in your %1" )
159  mActionPasswordHelperDelete = new QAction( tr( "Clear the master password from your %1" )
161  mActionPasswordHelperEnable = new QAction( tr( "Integrate master password with your %1" )
163  mActionPasswordHelperLoggingEnable = new QAction( tr( "Enable password helper debug log" ), this );
164 
165  mActionPasswordHelperEnable->setCheckable( true );
166  mActionPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
167 
168  mActionPasswordHelperLoggingEnable->setCheckable( true );
169  mActionPasswordHelperLoggingEnable->setChecked( QgsApplication::authManager()->passwordHelperLoggingEnabled() );
170 
171  connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::setMasterPassword );
172  connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedMasterPassword );
173  connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::resetMasterPassword );
174  connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedAuthenticationConfigs );
175  connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::removeAuthenticationConfigs );
176  connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthEditorWidgets::eraseAuthenticationDatabase );
177 
178  connect( mActionPasswordHelperSync, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperSync );
179  connect( mActionPasswordHelperDelete, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperDelete );
180  connect( mActionPasswordHelperEnable, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperEnableTriggered );
181  connect( mActionPasswordHelperLoggingEnable, &QAction::triggered, this, &QgsAuthEditorWidgets::passwordHelperLoggingEnableTriggered );
182 
183  connect( mActionClearAccessCacheNow, &QAction::triggered, this, [ = ]
184  {
185  QgsNetworkAccessManager::instance()->clearAccessCache();
186  messageBar()->pushSuccess( tr( "Auth cache cleared" ), tr( "Network authentication cache has been cleared" ) );
187  } );
188  connect( mActionAutoClearAccessCache, &QAction::triggered, this, [ ]( bool checked )
189  {
190  QgsSettings().setValue( QStringLiteral( "clear_auth_cache_on_errors" ), checked, QgsSettings::Section::Auth );
191  } );
192 
193  mAuthUtilitiesMenu = new QMenu( this );
194  mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
195  mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
196  mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
197  mAuthUtilitiesMenu->addSeparator();
198  mAuthUtilitiesMenu->addAction( mActionClearAccessCacheNow );
199  mAuthUtilitiesMenu->addAction( mActionAutoClearAccessCache );
200  mAuthUtilitiesMenu->addSeparator();
201  mAuthUtilitiesMenu->addAction( mActionPasswordHelperEnable );
202  mAuthUtilitiesMenu->addAction( mActionPasswordHelperSync );
203  mAuthUtilitiesMenu->addAction( mActionPasswordHelperDelete );
204  mAuthUtilitiesMenu->addAction( mActionPasswordHelperLoggingEnable );
205  mAuthUtilitiesMenu->addSeparator();
206  mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
207  mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
208  mAuthUtilitiesMenu->addSeparator();
209  mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
210 
211  btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
212 }
213 
214 void QgsAuthEditorWidgets::setMasterPassword()
215 {
216  QgsAuthGuiUtils::setMasterPassword( messageBar(), messageTimeout() );
217 }
218 
219 void QgsAuthEditorWidgets::clearCachedMasterPassword()
220 {
221  QgsAuthGuiUtils::clearCachedMasterPassword( messageBar(), messageTimeout() );
222 }
223 
224 void QgsAuthEditorWidgets::resetMasterPassword()
225 {
226  QgsAuthGuiUtils::resetMasterPassword( messageBar(), messageTimeout(), this );
227 }
228 
229 void QgsAuthEditorWidgets::clearCachedAuthenticationConfigs()
230 {
231  QgsAuthGuiUtils::clearCachedAuthenticationConfigs( messageBar(), messageTimeout() );
232 }
233 
234 void QgsAuthEditorWidgets::removeAuthenticationConfigs()
235 {
236  QgsAuthGuiUtils::removeAuthenticationConfigs( messageBar(), messageTimeout(), this );
237 }
238 
239 void QgsAuthEditorWidgets::eraseAuthenticationDatabase()
240 {
241  QgsAuthGuiUtils::eraseAuthenticationDatabase( messageBar(), messageTimeout(), this );
242 }
243 
244 void QgsAuthEditorWidgets::authMessageOut( const QString &message, const QString &authtag, QgsAuthManager::MessageLevel level )
245 {
246  int levelint = static_cast<int>( level );
247  messageBar()->pushMessage( authtag, message, ( Qgis::MessageLevel )levelint, 7 );
248 }
249 
250 void QgsAuthEditorWidgets::passwordHelperDelete()
251 {
252  QgsAuthGuiUtils::passwordHelperDelete( messageBar(), messageTimeout(), this );
253 }
254 
255 void QgsAuthEditorWidgets::passwordHelperSync()
256 {
257  QgsAuthGuiUtils::passwordHelperSync( messageBar(), messageTimeout() );
258 }
259 
260 void QgsAuthEditorWidgets::passwordHelperEnableTriggered()
261 {
262  // Only fire on real changes
263  QgsAuthGuiUtils::passwordHelperEnable( mActionPasswordHelperEnable->isChecked(), messageBar(), messageTimeout() );
264 }
265 
266 void QgsAuthEditorWidgets::passwordHelperLoggingEnableTriggered()
267 {
268  QgsAuthGuiUtils::passwordHelperLoggingEnable( mActionPasswordHelperLoggingEnable->isChecked(), messageBar(), messageTimeout() );
269 }
270 
271 QgsMessageBar *QgsAuthEditorWidgets::messageBar()
272 {
273  return mMsgBar;
274 }
275 
276 int QgsAuthEditorWidgets::messageTimeout()
277 {
278  QgsSettings settings;
279  return settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
280 }
static void resetMasterPassword(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Reset the cached master password, updating its hash in authentication database and resetting all exis...
static void eraseAuthenticationDatabase(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Completely clear out the authentication database (configs and master password)
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.
Dialog for viewing available authentication method plugins.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static void clearCachedMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Clear the currently cached master password (not its hash in database)
static void removeAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Remove all authentication configs.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
static void setMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Sets the cached master password (and verifies it if its hash is in authentication database) ...
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:66
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar.
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME
The display name of the password helper (platform dependent)
static void passwordHelperLoggingEnable(bool enabled, QgsMessageBar *msgbar, int timeout=0)
Sets password helper logging enabled (enable/disable)
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0)
Clear all cached authentication configs for session.
Dialog wrapper for widget to manage available certificate editors.
QgsAuthMethodPlugins(QWidget *parent=nullptr)
Construct a dialog for viewing available authentication method plugins.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:88
static void passwordHelperEnable(bool enabled, QgsMessageBar *msgbar, int timeout=0)
Sets password helper enabled (enable/disable)
static QgsAuthManager * authManager()
Returns the application&#39;s authentication manager instance.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
static void passwordHelperSync(QgsMessageBar *msgbar, int timeout=0)
Store master password into the wallet.
Abstract base class for authentication method plugins.
Definition: qgsauthmethod.h:36
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsAuthEditorWidgets(QWidget *parent=nullptr)
Construct a widget to contain various authentication editors.
QHash< QString, QgsAuthMethod * > QgsAuthMethodsMap
static void passwordHelperDelete(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Remove master password from wallet.