QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
31#include "moc_qgsauthconfigeditor.cpp"
32
33QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
34 : QWidget( parent )
35 , mRelayMessages( relayMessages )
36{
37 if ( QgsApplication::authManager()->isDisabled() )
38 {
39 mDisabled = true;
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( btnAddConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnAddConfig_clicked );
49 connect( btnEditConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
50 connect( btnRemoveConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnRemoveConfig_clicked );
51
52 setShowUtilitiesButton( showUtilities );
53
55 const QSqlDatabase connection { QgsApplication::authManager()->authDatabaseConnection() };
57
59 if ( mIsReadOnly )
60 {
61 mConfigModel = new QSqlTableModel( this, connection );
62 btnAddConfig->setEnabled( false );
63 btnEditConfig->setEnabled( false );
64 btnRemoveConfig->setEnabled( false );
65 tableViewConfigs->setEditTriggers( QAbstractItemView::EditTrigger::NoEditTriggers );
66 }
67 else
68 {
69 mConfigModel = new QSqlTableModel( this, connection );
70 }
71 mConfigModel->setTable( QgsApplication::authManager()->methodConfigTableName() );
72
73 mConfigModel->select();
74
75 mConfigModel->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
76 mConfigModel->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
77 mConfigModel->setHeaderData( 2, Qt::Horizontal, tr( "URI" ) );
78 mConfigModel->setHeaderData( 3, Qt::Horizontal, tr( "Type" ) );
79 mConfigModel->setHeaderData( 4, Qt::Horizontal, tr( "Version" ) );
80 mConfigModel->setHeaderData( 5, Qt::Horizontal, tr( "Config" ) );
81
82 tableViewConfigs->setModel( mConfigModel );
83 tableViewConfigs->resizeColumnsToContents();
84 // tableViewConfigs->resizeColumnToContents( 0 );
85 // tableViewConfigs->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
86 // tableViewConfigs->horizontalHeader()->setResizeMode(2, QHeaderView::Interactive);
87 // tableViewConfigs->resizeColumnToContents( 3 );
88 tableViewConfigs->hideColumn( 4 );
89 tableViewConfigs->hideColumn( 5 );
90
91 // sort by config 'name'
92 tableViewConfigs->sortByColumn( 1, Qt::AscendingOrder );
93 tableViewConfigs->setSortingEnabled( true );
94
95 connect( tableViewConfigs->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsAuthConfigEditor::selectionChanged );
96
97 if ( mRelayMessages )
98 {
99 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
100 }
101
102 connect( QgsApplication::authManager(), &QgsAuthManager::authDatabaseChanged, this, &QgsAuthConfigEditor::refreshTableView );
103
104 checkSelection();
105
106 // set up utility actions menu
107 mActionImportAuthenticationConfigs = new QAction( tr( "Import Authentication Configurations from File…" ), this );
108 mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export Selected Authentication Configurations to File…" ), this );
109 mActionSetMasterPassword = new QAction( QStringLiteral( "Input Master Password…" ), this );
110 mActionClearCachedMasterPassword = new QAction( QStringLiteral( "Clear Cached Master Password" ), this );
111 mActionResetMasterPassword = new QAction( QStringLiteral( "Reset Master Password…" ), this );
112 mActionClearCachedAuthConfigs = new QAction( QStringLiteral( "Clear Cached Authentication Configurations" ), this );
113 mActionRemoveAuthConfigs = new QAction( QStringLiteral( "Remove all Authentication Configurations…" ), this );
114 mActionEraseAuthDatabase = new QAction( QStringLiteral( "Erase Authentication Database…" ), this );
115
116 connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::exportSelectedAuthenticationConfigs );
117 connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
118 connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
119 connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedAuthenticationConfigs );
120
121 if ( !mIsReadOnly )
122 {
123 connect( tableViewConfigs, &QAbstractItemView::doubleClicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
124
125 connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::importAuthenticationConfigs );
126 connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
127 connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::removeAuthenticationConfigs );
128 connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthConfigEditor::eraseAuthenticationDatabase );
129 }
130 else
131 {
132 mActionImportAuthenticationConfigs->setEnabled( false );
133 mActionSetMasterPassword->setEnabled( false );
134 mActionClearCachedMasterPassword->setEnabled( false );
135 mActionResetMasterPassword->setEnabled( false );
136 mActionClearCachedAuthConfigs->setEnabled( false );
137 mActionRemoveAuthConfigs->setEnabled( false );
138 mActionEraseAuthDatabase->setEnabled( false );
139 }
140
141 mAuthUtilitiesMenu = new QMenu( this );
142
143 if ( !mIsReadOnly )
144 {
145 mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
146 mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
147 mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
148 mAuthUtilitiesMenu->addSeparator();
149 }
150
151 mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
152
153 if ( !mIsReadOnly )
154 mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
155
156 mAuthUtilitiesMenu->addSeparator();
157
158 if ( !mIsReadOnly )
159 mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
160
161 mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
162 mAuthUtilitiesMenu->addSeparator();
163
164 if ( !mIsReadOnly )
165 mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
166
167 btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
168 lblAuthConfigDb->setVisible( false );
169 }
170}
171
172void QgsAuthConfigEditor::importAuthenticationConfigs()
173{
175}
176
177void QgsAuthConfigEditor::exportSelectedAuthenticationConfigs()
178{
180}
181
182void QgsAuthConfigEditor::setMasterPassword()
183{
185}
186
187void QgsAuthConfigEditor::clearCachedMasterPassword()
188{
190}
191
192void QgsAuthConfigEditor::resetMasterPassword()
193{
194 QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
195}
196
197void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
198{
200}
201
202void QgsAuthConfigEditor::removeAuthenticationConfigs()
203{
205}
206
207void QgsAuthConfigEditor::eraseAuthenticationDatabase()
208{
210}
211
212void QgsAuthConfigEditor::authMessageLog( const QString &message, const QString &authtag, Qgis::MessageLevel level )
213{
214 messageBar()->pushMessage( authtag, message, level );
215}
216
218{
219 if ( !mDisabled )
220 {
221 lblAuthConfigDb->setVisible( visible );
222 }
223}
224
226{
227 QStringList ids;
228 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
229 for ( const QModelIndex index : selection )
230 {
231 ids << index.sibling( index.row(), 0 ).data().toString();
232 }
233 return ids;
234}
235
237{
238 if ( !mDisabled )
239 {
240 btnAuthUtilities->setVisible( show );
241 }
242}
243
245{
246 if ( mDisabled )
247 {
248 return;
249 }
250 if ( relay == mRelayMessages )
251 {
252 return;
253 }
254
255 if ( mRelayMessages )
256 {
257 disconnect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
258 mRelayMessages = relay;
259 return;
260 }
261
262 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
263 mRelayMessages = relay;
264}
265
266void QgsAuthConfigEditor::refreshTableView()
267{
268 mConfigModel->select();
269 tableViewConfigs->reset();
270}
271
272void QgsAuthConfigEditor::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
273{
274 Q_UNUSED( selected )
275 Q_UNUSED( deselected )
276 checkSelection();
277}
278
279void QgsAuthConfigEditor::checkSelection()
280{
281 if ( !mIsReadOnly )
282 {
283 const bool hasselection = tableViewConfigs->selectionModel()->selection().length() > 0;
284 btnEditConfig->setEnabled( hasselection );
285 btnRemoveConfig->setEnabled( hasselection );
286 }
287}
288
289void QgsAuthConfigEditor::btnAddConfig_clicked()
290{
291 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
292 return;
293
294 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this );
295 ace->setWindowModality( Qt::WindowModal );
296 if ( ace->exec() )
297 {
298 mConfigModel->select();
299 }
300 ace->deleteLater();
301}
302
303void QgsAuthConfigEditor::btnEditConfig_clicked()
304{
305 const QString authcfg = selectedConfigId();
306
307 if ( authcfg.isEmpty() )
308 return;
309
310 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
311 return;
312
313 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this, authcfg );
314 ace->setWindowModality( Qt::WindowModal );
315 if ( ace->exec() )
316 {
317 mConfigModel->select();
318 }
319 ace->deleteLater();
320}
321
322void QgsAuthConfigEditor::btnRemoveConfig_clicked()
323{
324 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
325
326 if ( selection.empty() )
327 return;
328
329 for ( const QModelIndex index : selection )
330 {
331 const QString name = index.sibling( index.row(), 1 ).data().toString();
332
333 if ( QMessageBox::warning( this, tr( "Remove Configuration" ), tr( "Are you sure you want to remove '%1'?\n\n"
334 "Operation can NOT be undone!" )
335 .arg( name ),
336 QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel )
337 == QMessageBox::Ok )
338 {
339 mConfigModel->removeRow( index.row() );
340 }
341 }
342}
343
344QgsMessageBar *QgsAuthConfigEditor::messageBar()
345{
346 return mMsgBar;
347}
348
349QString QgsAuthConfigEditor::selectedConfigId()
350{
351 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
352
353 if ( selection.empty() )
354 return QString();
355
356 const QModelIndex indx = selection.at( 0 );
357 return indx.sibling( indx.row(), 0 ).data().toString();
358}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:156
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:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169