QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsauthconfigeditor.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthconfigeditor.cpp
3 ---------------------
4 begin : October 5, 2014
5 copyright : (C) 2014 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 "ui_qgsauthconfigeditor.h"
18#include "qgsauthconfigeditor.h"
19
20#include "qgsapplication.h"
21#include "qgsauthconfigedit.h"
23#include "qgsauthguiutils.h"
24#include "qgsauthmanager.h"
25#include "qgssettings.h"
26
27#include <QMenu>
28#include <QMessageBox>
29#include <QSqlTableModel>
30#include <QString>
31
32#include "moc_qgsauthconfigeditor.cpp"
33
34using namespace Qt::StringLiterals;
35
36QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
37 : QWidget( parent )
38 , mRelayMessages( relayMessages )
39{
40 if ( QgsApplication::authManager()->isDisabled() )
41 {
42 mDisabled = true;
43 mAuthNotifyLayout = new QVBoxLayout;
44 this->setLayout( mAuthNotifyLayout );
45 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
46 mAuthNotifyLayout->addWidget( mAuthNotify );
47 }
48 else
49 {
50 setupUi( this );
51 connect( btnAddConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnAddConfig_clicked );
52 connect( btnEditConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
53 connect( btnRemoveConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnRemoveConfig_clicked );
54
55 setShowUtilitiesButton( showUtilities );
56
58 const QSqlDatabase connection { QgsApplication::authManager()->authDatabaseConnection() };
60
62 if ( mIsReadOnly )
63 {
64 mConfigModel = new QSqlTableModel( this, connection );
65 btnAddConfig->setEnabled( false );
66 btnEditConfig->setEnabled( false );
67 btnRemoveConfig->setEnabled( false );
68 tableViewConfigs->setEditTriggers( QAbstractItemView::EditTrigger::NoEditTriggers );
69 }
70 else
71 {
72 mConfigModel = new QSqlTableModel( this, connection );
73 }
74 mConfigModel->setTable( QgsApplication::authManager()->methodConfigTableName() );
75
76 mConfigModel->select();
77
78 mConfigModel->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
79 mConfigModel->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
80 mConfigModel->setHeaderData( 2, Qt::Horizontal, tr( "URI" ) );
81 mConfigModel->setHeaderData( 3, Qt::Horizontal, tr( "Type" ) );
82 mConfigModel->setHeaderData( 4, Qt::Horizontal, tr( "Version" ) );
83 mConfigModel->setHeaderData( 5, Qt::Horizontal, tr( "Config" ) );
84
85 tableViewConfigs->setModel( mConfigModel );
86 tableViewConfigs->resizeColumnsToContents();
87 // tableViewConfigs->resizeColumnToContents( 0 );
88 // tableViewConfigs->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
89 // tableViewConfigs->horizontalHeader()->setResizeMode(2, QHeaderView::Interactive);
90 // tableViewConfigs->resizeColumnToContents( 3 );
91 tableViewConfigs->hideColumn( 4 );
92 tableViewConfigs->hideColumn( 5 );
93
94 // sort by config 'name'
95 tableViewConfigs->sortByColumn( 1, Qt::AscendingOrder );
96 tableViewConfigs->setSortingEnabled( true );
97
98 connect( tableViewConfigs->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsAuthConfigEditor::selectionChanged );
99
100 if ( mRelayMessages )
101 {
102 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
103 }
104
105 connect( QgsApplication::authManager(), &QgsAuthManager::authDatabaseChanged, this, &QgsAuthConfigEditor::refreshTableView );
106
107 checkSelection();
108
109 // set up utility actions menu
110 mActionImportAuthenticationConfigs = new QAction( tr( "Import Authentication Configurations from File…" ), this );
111 mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export Selected Authentication Configurations to File…" ), this );
112 mActionSetMasterPassword = new QAction( u"Input Master Password…"_s, this );
113 mActionClearCachedMasterPassword = new QAction( u"Clear Cached Master Password"_s, this );
114 mActionResetMasterPassword = new QAction( u"Reset Master Password…"_s, this );
115 mActionClearCachedAuthConfigs = new QAction( u"Clear Cached Authentication Configurations"_s, this );
116 mActionRemoveAuthConfigs = new QAction( u"Remove all Authentication Configurations…"_s, this );
117 mActionEraseAuthDatabase = new QAction( u"Erase Authentication Database…"_s, this );
118
119 connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::exportSelectedAuthenticationConfigs );
120 connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
121 connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
122 connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedAuthenticationConfigs );
123
124 if ( !mIsReadOnly )
125 {
126 connect( tableViewConfigs, &QAbstractItemView::doubleClicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
127
128 connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::importAuthenticationConfigs );
129 connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
130 connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::removeAuthenticationConfigs );
131 connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthConfigEditor::eraseAuthenticationDatabase );
132 }
133 else
134 {
135 mActionImportAuthenticationConfigs->setEnabled( false );
136 mActionSetMasterPassword->setEnabled( false );
137 mActionClearCachedMasterPassword->setEnabled( false );
138 mActionResetMasterPassword->setEnabled( false );
139 mActionClearCachedAuthConfigs->setEnabled( false );
140 mActionRemoveAuthConfigs->setEnabled( false );
141 mActionEraseAuthDatabase->setEnabled( false );
142 }
143
144 mAuthUtilitiesMenu = new QMenu( this );
145
146 if ( !mIsReadOnly )
147 {
148 mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
149 mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
150 mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
151 mAuthUtilitiesMenu->addSeparator();
152 }
153
154 mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
155
156 if ( !mIsReadOnly )
157 mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
158
159 mAuthUtilitiesMenu->addSeparator();
160
161 if ( !mIsReadOnly )
162 mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
163
164 mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
165 mAuthUtilitiesMenu->addSeparator();
166
167 if ( !mIsReadOnly )
168 mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
169
170 btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
171 lblAuthConfigDb->setVisible( false );
172 }
173}
174
175void QgsAuthConfigEditor::importAuthenticationConfigs()
176{
178}
179
180void QgsAuthConfigEditor::exportSelectedAuthenticationConfigs()
181{
183}
184
185void QgsAuthConfigEditor::setMasterPassword()
186{
188}
189
190void QgsAuthConfigEditor::clearCachedMasterPassword()
191{
193}
194
195void QgsAuthConfigEditor::resetMasterPassword()
196{
197 QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
198}
199
200void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
201{
203}
204
205void QgsAuthConfigEditor::removeAuthenticationConfigs()
206{
208}
209
210void QgsAuthConfigEditor::eraseAuthenticationDatabase()
211{
213}
214
215void QgsAuthConfigEditor::authMessageLog( const QString &message, const QString &authtag, Qgis::MessageLevel level )
216{
217 messageBar()->pushMessage( authtag, message, level );
218}
219
221{
222 if ( !mDisabled )
223 {
224 lblAuthConfigDb->setVisible( visible );
225 }
226}
227
229{
230 QStringList ids;
231 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
232 for ( const QModelIndex index : selection )
233 {
234 ids << index.sibling( index.row(), 0 ).data().toString();
235 }
236 return ids;
237}
238
240{
241 if ( !mDisabled )
242 {
243 btnAuthUtilities->setVisible( show );
244 }
245}
246
248{
249 if ( mDisabled )
250 {
251 return;
252 }
253 if ( relay == mRelayMessages )
254 {
255 return;
256 }
257
258 if ( mRelayMessages )
259 {
260 disconnect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
261 mRelayMessages = relay;
262 return;
263 }
264
265 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
266 mRelayMessages = relay;
267}
268
269void QgsAuthConfigEditor::refreshTableView()
270{
271 mConfigModel->select();
272 tableViewConfigs->reset();
273}
274
275void QgsAuthConfigEditor::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
276{
277 Q_UNUSED( selected )
278 Q_UNUSED( deselected )
279 checkSelection();
280}
281
282void QgsAuthConfigEditor::checkSelection()
283{
284 if ( !mIsReadOnly )
285 {
286 const bool hasselection = tableViewConfigs->selectionModel()->selection().length() > 0;
287 btnEditConfig->setEnabled( hasselection );
288 btnRemoveConfig->setEnabled( hasselection );
289 }
290}
291
292void QgsAuthConfigEditor::btnAddConfig_clicked()
293{
294 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
295 return;
296
297 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this );
298 ace->setWindowModality( Qt::WindowModal );
299 if ( ace->exec() )
300 {
301 mConfigModel->select();
302 }
303 ace->deleteLater();
304}
305
306void QgsAuthConfigEditor::btnEditConfig_clicked()
307{
308 const QString authcfg = selectedConfigId();
309
310 if ( authcfg.isEmpty() )
311 return;
312
313 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
314 return;
315
316 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this, authcfg );
317 ace->setWindowModality( Qt::WindowModal );
318 if ( ace->exec() )
319 {
320 mConfigModel->select();
321 }
322 ace->deleteLater();
323}
324
325void QgsAuthConfigEditor::btnRemoveConfig_clicked()
326{
327 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
328
329 if ( selection.empty() )
330 return;
331
332 for ( const QModelIndex index : selection )
333 {
334 const QString name = index.sibling( index.row(), 1 ).data().toString();
335
336 if ( QMessageBox::warning( this, tr( "Remove Configuration" ), tr( "Are you sure you want to remove '%1'?\n\n"
337 "Operation can NOT be undone!" )
338 .arg( name ),
339 QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel )
340 == QMessageBox::Ok )
341 {
342 mConfigModel->removeRow( index.row() );
343 }
344 }
345}
346
347QgsMessageBar *QgsAuthConfigEditor::messageBar()
348{
349 return mMsgBar;
350}
351
352QString QgsAuthConfigEditor::selectedConfigId()
353{
354 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
355
356 if ( selection.empty() )
357 return QString();
358
359 const QModelIndex indx = selection.at( 0 );
360 return indx.sibling( indx.row(), 0 ).data().toString();
361}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:159
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
QgsAuthConfigEditor(QWidget *parent=nullptr, bool showUtilities=true, bool relayMessages=true)
Widget for editing authentication configurations directly in database.
void setRelayMessages(bool relay=true)
Sets whether to relay auth manager messages to internal message bar, e.g. when embedding.
void setShowUtilitiesButton(bool show=true)
Sets whether to show the widget's utilities button, e.g. when embedding.
QStringList selectedAuthenticationConfigIds() const
Returns the list of selected authentication configuration IDs.
void toggleTitleVisibility(bool visible)
Hide the widget's title, e.g. when embedding.
virtual bool isReadOnly() const
Returns true if the storage is read-only, false otherwise.
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 clearCachedAuthenticationConfigs(QgsMessageBar *msgbar)
Clear all cached authentication configs for session.
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).
void authDatabaseChanged()
Emitted when the authentication db is significantly changed, e.g. large record removal,...
QgsAuthConfigurationStorageDb * defaultDbStorage() const
Transitional proxy to the first ready storage of database type.
void messageLog(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level=Qgis::MessageLevel::Info) const
Custom logging signal to relay to console output and QgsMessageLog.
Q_DECL_DEPRECATED QSqlDatabase authDatabaseConnection() const
Sets up the application instance of the authentication database connection.
A bar for displaying non-blocking messages to the user.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7486
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7485