QGIS API Documentation 3.39.0-Master (d85f3c2a281)
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 "qgsauthconfigeditor.h"
19#include "ui_qgsauthconfigeditor.h"
20
21#include <QMenu>
22#include <QMessageBox>
23#include <QSqlTableModel>
24
25#include "qgssettings.h"
26#include "qgsauthmanager.h"
27#include "qgsauthconfigedit.h"
28#include "qgsauthguiutils.h"
29#include "qgsapplication.h"
30
31QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
32 : QWidget( parent )
33 , mRelayMessages( relayMessages )
34{
35 if ( QgsApplication::authManager()->isDisabled() )
36 {
37 mDisabled = true;
38 mAuthNotifyLayout = new QVBoxLayout;
39 this->setLayout( mAuthNotifyLayout );
40 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
41 mAuthNotifyLayout->addWidget( mAuthNotify );
42 }
43 else
44 {
45 setupUi( this );
46 connect( btnAddConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnAddConfig_clicked );
47 connect( btnEditConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
48 connect( btnRemoveConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnRemoveConfig_clicked );
49
50 setShowUtilitiesButton( showUtilities );
51
53 const QSqlDatabase connection { QgsApplication::authManager()->authDatabaseConnection() };
55
57 if ( mIsReadOnly )
58 {
59 mConfigModel = new QSqlTableModel( this, connection );
60 btnAddConfig->setEnabled( false );
61 btnEditConfig->setEnabled( false );
62 btnRemoveConfig->setEnabled( false );
63 tableViewConfigs->setEditTriggers( QAbstractItemView::EditTrigger::NoEditTriggers );
64 }
65 else
66 {
67 mConfigModel = new QSqlTableModel( this, connection );
68 }
69 mConfigModel->setTable( QgsApplication::authManager()->methodConfigTableName() );
70
71 mConfigModel->select();
72
73 mConfigModel->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
74 mConfigModel->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
75 mConfigModel->setHeaderData( 2, Qt::Horizontal, tr( "URI" ) );
76 mConfigModel->setHeaderData( 3, Qt::Horizontal, tr( "Type" ) );
77 mConfigModel->setHeaderData( 4, Qt::Horizontal, tr( "Version" ) );
78 mConfigModel->setHeaderData( 5, Qt::Horizontal, tr( "Config" ) );
79
80 tableViewConfigs->setModel( mConfigModel );
81 tableViewConfigs->resizeColumnsToContents();
82// tableViewConfigs->resizeColumnToContents( 0 );
83// tableViewConfigs->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
84// tableViewConfigs->horizontalHeader()->setResizeMode(2, QHeaderView::Interactive);
85// tableViewConfigs->resizeColumnToContents( 3 );
86 tableViewConfigs->hideColumn( 4 );
87 tableViewConfigs->hideColumn( 5 );
88
89 // sort by config 'name'
90 tableViewConfigs->sortByColumn( 1, Qt::AscendingOrder );
91 tableViewConfigs->setSortingEnabled( true );
92
93 connect( tableViewConfigs->selectionModel(), &QItemSelectionModel::selectionChanged,
94 this, &QgsAuthConfigEditor::selectionChanged );
95
96 if ( mRelayMessages )
97 {
99 this, &QgsAuthConfigEditor::authMessageLog );
100 }
101
103 this, &QgsAuthConfigEditor::refreshTableView );
104
105 checkSelection();
106
107 // set up utility actions menu
108 mActionImportAuthenticationConfigs = new QAction( tr( "Import Authentication Configurations from File…" ), this );
109 mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export Selected Authentication Configurations to File…" ), this );
110 mActionSetMasterPassword = new QAction( QStringLiteral( "Input Master Password…" ), this );
111 mActionClearCachedMasterPassword = new QAction( QStringLiteral( "Clear Cached Master Password" ), this );
112 mActionResetMasterPassword = new QAction( QStringLiteral( "Reset Master Password…" ), this );
113 mActionClearCachedAuthConfigs = new QAction( QStringLiteral( "Clear Cached Authentication Configurations" ), this );
114 mActionRemoveAuthConfigs = new QAction( QStringLiteral( "Remove all Authentication Configurations…" ), this );
115 mActionEraseAuthDatabase = new QAction( QStringLiteral( "Erase Authentication Database…" ), this );
116
117 connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::exportSelectedAuthenticationConfigs );
118 connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
119 connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
120 connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedAuthenticationConfigs );
121
122 if ( ! mIsReadOnly )
123 {
124 connect( tableViewConfigs, &QAbstractItemView::doubleClicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
125
126 connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::importAuthenticationConfigs );
127 connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
128 connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::removeAuthenticationConfigs );
129 connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthConfigEditor::eraseAuthenticationDatabase );
130 }
131 else
132 {
133 mActionImportAuthenticationConfigs->setEnabled( false );
134 mActionSetMasterPassword->setEnabled( false );
135 mActionClearCachedMasterPassword->setEnabled( false );
136 mActionResetMasterPassword->setEnabled( false );
137 mActionClearCachedAuthConfigs->setEnabled( false );
138 mActionRemoveAuthConfigs->setEnabled( false );
139 mActionEraseAuthDatabase->setEnabled( false );
140 }
141
142 mAuthUtilitiesMenu = new QMenu( this );
143
144 if ( ! mIsReadOnly )
145 {
146 mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
147 mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
148 mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
149 mAuthUtilitiesMenu->addSeparator();
150 }
151
152 mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
153
154 if ( ! mIsReadOnly )
155 mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
156
157 mAuthUtilitiesMenu->addSeparator();
158
159 if ( ! mIsReadOnly )
160 mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
161
162 mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
163 mAuthUtilitiesMenu->addSeparator();
164
165 if ( ! mIsReadOnly )
166 mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
167
168 btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
169 lblAuthConfigDb->setVisible( false );
170 }
171}
172
173void QgsAuthConfigEditor::importAuthenticationConfigs()
174{
176}
177
178void QgsAuthConfigEditor::exportSelectedAuthenticationConfigs()
179{
181}
182
183void QgsAuthConfigEditor::setMasterPassword()
184{
186}
187
188void QgsAuthConfigEditor::clearCachedMasterPassword()
189{
191}
192
193void QgsAuthConfigEditor::resetMasterPassword()
194{
195 QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
196}
197
198void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
199{
201}
202
203void QgsAuthConfigEditor::removeAuthenticationConfigs()
204{
206}
207
208void QgsAuthConfigEditor::eraseAuthenticationDatabase()
209{
211}
212
213void QgsAuthConfigEditor::authMessageLog( const QString &message, const QString &authtag, Qgis::MessageLevel level )
214{
215 messageBar()->pushMessage( authtag, message, level );
216}
217
219{
220 if ( !mDisabled )
221 {
222 lblAuthConfigDb->setVisible( visible );
223 }
224}
225
227{
228 QStringList ids;
229 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
230 for ( const QModelIndex index : selection )
231 {
232 ids << index.sibling( index.row(), 0 ).data().toString();
233 }
234 return ids;
235}
236
238{
239 if ( !mDisabled )
240 {
241 btnAuthUtilities->setVisible( show );
242 }
243}
244
246{
247 if ( mDisabled )
248 {
249 return;
250 }
251 if ( relay == mRelayMessages )
252 {
253 return;
254 }
255
256 if ( mRelayMessages )
257 {
259 this, &QgsAuthConfigEditor::authMessageLog );
260 mRelayMessages = relay;
261 return;
262 }
263
265 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" ),
337 tr( "Are you sure you want to remove '%1'?\n\n"
338 "Operation can NOT be undone!" ).arg( name ),
339 QMessageBox::Ok | QMessageBox::Cancel,
340 QMessageBox::Cancel ) == 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:154
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Widget for editing an authentication configuration.
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.
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.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6434
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6433