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