QGIS API Documentation 3.41.0-Master (cea29feecf2)
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, this, &QgsAuthConfigEditor::selectionChanged );
95
96 if ( mRelayMessages )
97 {
98 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
99 }
100
101 connect( QgsApplication::authManager(), &QgsAuthManager::authDatabaseChanged, this, &QgsAuthConfigEditor::refreshTableView );
102
103 checkSelection();
104
105 // set up utility actions menu
106 mActionImportAuthenticationConfigs = new QAction( tr( "Import Authentication Configurations from File…" ), this );
107 mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export Selected Authentication Configurations to File…" ), this );
108 mActionSetMasterPassword = new QAction( QStringLiteral( "Input Master Password…" ), this );
109 mActionClearCachedMasterPassword = new QAction( QStringLiteral( "Clear Cached Master Password" ), this );
110 mActionResetMasterPassword = new QAction( QStringLiteral( "Reset Master Password…" ), this );
111 mActionClearCachedAuthConfigs = new QAction( QStringLiteral( "Clear Cached Authentication Configurations" ), this );
112 mActionRemoveAuthConfigs = new QAction( QStringLiteral( "Remove all Authentication Configurations…" ), this );
113 mActionEraseAuthDatabase = new QAction( QStringLiteral( "Erase Authentication Database…" ), this );
114
115 connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::exportSelectedAuthenticationConfigs );
116 connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
117 connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
118 connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedAuthenticationConfigs );
119
120 if ( !mIsReadOnly )
121 {
122 connect( tableViewConfigs, &QAbstractItemView::doubleClicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
123
124 connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::importAuthenticationConfigs );
125 connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
126 connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::removeAuthenticationConfigs );
127 connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthConfigEditor::eraseAuthenticationDatabase );
128 }
129 else
130 {
131 mActionImportAuthenticationConfigs->setEnabled( false );
132 mActionSetMasterPassword->setEnabled( false );
133 mActionClearCachedMasterPassword->setEnabled( false );
134 mActionResetMasterPassword->setEnabled( false );
135 mActionClearCachedAuthConfigs->setEnabled( false );
136 mActionRemoveAuthConfigs->setEnabled( false );
137 mActionEraseAuthDatabase->setEnabled( false );
138 }
139
140 mAuthUtilitiesMenu = new QMenu( this );
141
142 if ( !mIsReadOnly )
143 {
144 mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
145 mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
146 mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
147 mAuthUtilitiesMenu->addSeparator();
148 }
149
150 mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
151
152 if ( !mIsReadOnly )
153 mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
154
155 mAuthUtilitiesMenu->addSeparator();
156
157 if ( !mIsReadOnly )
158 mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
159
160 mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
161 mAuthUtilitiesMenu->addSeparator();
162
163 if ( !mIsReadOnly )
164 mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
165
166 btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
167 lblAuthConfigDb->setVisible( false );
168 }
169}
170
171void QgsAuthConfigEditor::importAuthenticationConfigs()
172{
174}
175
176void QgsAuthConfigEditor::exportSelectedAuthenticationConfigs()
177{
179}
180
181void QgsAuthConfigEditor::setMasterPassword()
182{
184}
185
186void QgsAuthConfigEditor::clearCachedMasterPassword()
187{
189}
190
191void QgsAuthConfigEditor::resetMasterPassword()
192{
193 QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
194}
195
196void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
197{
199}
200
201void QgsAuthConfigEditor::removeAuthenticationConfigs()
202{
204}
205
206void QgsAuthConfigEditor::eraseAuthenticationDatabase()
207{
209}
210
211void QgsAuthConfigEditor::authMessageLog( const QString &message, const QString &authtag, Qgis::MessageLevel level )
212{
213 messageBar()->pushMessage( authtag, message, level );
214}
215
217{
218 if ( !mDisabled )
219 {
220 lblAuthConfigDb->setVisible( visible );
221 }
222}
223
225{
226 QStringList ids;
227 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
228 for ( const QModelIndex index : selection )
229 {
230 ids << index.sibling( index.row(), 0 ).data().toString();
231 }
232 return ids;
233}
234
236{
237 if ( !mDisabled )
238 {
239 btnAuthUtilities->setVisible( show );
240 }
241}
242
244{
245 if ( mDisabled )
246 {
247 return;
248 }
249 if ( relay == mRelayMessages )
250 {
251 return;
252 }
253
254 if ( mRelayMessages )
255 {
256 disconnect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
257 mRelayMessages = relay;
258 return;
259 }
260
261 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthConfigEditor::authMessageLog );
262 mRelayMessages = relay;
263}
264
265void QgsAuthConfigEditor::refreshTableView()
266{
267 mConfigModel->select();
268 tableViewConfigs->reset();
269}
270
271void QgsAuthConfigEditor::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
272{
273 Q_UNUSED( selected )
274 Q_UNUSED( deselected )
275 checkSelection();
276}
277
278void QgsAuthConfigEditor::checkSelection()
279{
280 if ( !mIsReadOnly )
281 {
282 const bool hasselection = tableViewConfigs->selectionModel()->selection().length() > 0;
283 btnEditConfig->setEnabled( hasselection );
284 btnRemoveConfig->setEnabled( hasselection );
285 }
286}
287
288void QgsAuthConfigEditor::btnAddConfig_clicked()
289{
290 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
291 return;
292
293 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this );
294 ace->setWindowModality( Qt::WindowModal );
295 if ( ace->exec() )
296 {
297 mConfigModel->select();
298 }
299 ace->deleteLater();
300}
301
302void QgsAuthConfigEditor::btnEditConfig_clicked()
303{
304 const QString authcfg = selectedConfigId();
305
306 if ( authcfg.isEmpty() )
307 return;
308
309 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
310 return;
311
312 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this, authcfg );
313 ace->setWindowModality( Qt::WindowModal );
314 if ( ace->exec() )
315 {
316 mConfigModel->select();
317 }
318 ace->deleteLater();
319}
320
321void QgsAuthConfigEditor::btnRemoveConfig_clicked()
322{
323 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
324
325 if ( selection.empty() )
326 return;
327
328 for ( const QModelIndex index : selection )
329 {
330 const QString name = index.sibling( index.row(), 1 ).data().toString();
331
332 if ( QMessageBox::warning( this, tr( "Remove Configuration" ), tr( "Are you sure you want to remove '%1'?\n\n"
333 "Operation can NOT be undone!" )
334 .arg( name ),
335 QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel )
336 == QMessageBox::Ok )
337 {
338 mConfigModel->removeRow( index.row() );
339 }
340 }
341}
342
343QgsMessageBar *QgsAuthConfigEditor::messageBar()
344{
345 return mMsgBar;
346}
347
348QString QgsAuthConfigEditor::selectedConfigId()
349{
350 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
351
352 if ( selection.empty() )
353 return QString();
354
355 const QModelIndex indx = selection.at( 0 );
356 return indx.sibling( indx.row(), 0 ).data().toString();
357}
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:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600