QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 "ui_qgsauthconfigeditor.h"
19
20#include <QMenu>
21#include <QMessageBox>
22#include <QSqlTableModel>
23
24#include "qgssettings.h"
25#include "qgsauthmanager.h"
26#include "qgsauthconfigedit.h"
27#include "qgsauthguiutils.h"
28#include "qgsapplication.h"
29
30QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
31 : QWidget( parent )
32 , mRelayMessages( relayMessages )
33{
34 if ( QgsApplication::authManager()->isDisabled() )
35 {
36 mDisabled = true;
37 mAuthNotifyLayout = new QVBoxLayout;
38 this->setLayout( mAuthNotifyLayout );
39 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
40 mAuthNotifyLayout->addWidget( mAuthNotify );
41 }
42 else
43 {
44 setupUi( this );
45 connect( btnAddConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnAddConfig_clicked );
46 connect( btnEditConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnEditConfig_clicked );
47 connect( btnRemoveConfig, &QToolButton::clicked, this, &QgsAuthConfigEditor::btnRemoveConfig_clicked );
48
49 setShowUtilitiesButton( showUtilities );
50
51 mConfigModel = new QSqlTableModel( this, QgsApplication::authManager()->authDatabaseConnection() );
52 mConfigModel->setTable( QgsApplication::authManager()->authDatabaseConfigTable() );
53 mConfigModel->select();
54
55 mConfigModel->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
56 mConfigModel->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
57 mConfigModel->setHeaderData( 2, Qt::Horizontal, tr( "URI" ) );
58 mConfigModel->setHeaderData( 3, Qt::Horizontal, tr( "Type" ) );
59 mConfigModel->setHeaderData( 4, Qt::Horizontal, tr( "Version" ) );
60 mConfigModel->setHeaderData( 5, Qt::Horizontal, tr( "Config" ) );
61
62 tableViewConfigs->setModel( mConfigModel );
63 tableViewConfigs->resizeColumnsToContents();
64// tableViewConfigs->resizeColumnToContents( 0 );
65// tableViewConfigs->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
66// tableViewConfigs->horizontalHeader()->setResizeMode(2, QHeaderView::Interactive);
67// tableViewConfigs->resizeColumnToContents( 3 );
68 tableViewConfigs->hideColumn( 4 );
69 tableViewConfigs->hideColumn( 5 );
70
71 // sort by config 'name'
72 tableViewConfigs->sortByColumn( 1, Qt::AscendingOrder );
73 tableViewConfigs->setSortingEnabled( true );
74
75 connect( tableViewConfigs->selectionModel(), &QItemSelectionModel::selectionChanged,
76 this, &QgsAuthConfigEditor::selectionChanged );
77
78 connect( tableViewConfigs, &QAbstractItemView::doubleClicked,
79 this, &QgsAuthConfigEditor::btnEditConfig_clicked );
80
81 if ( mRelayMessages )
82 {
84 this, &QgsAuthConfigEditor::authMessageOut );
85 }
86
88 this, &QgsAuthConfigEditor::refreshTableView );
89
90 checkSelection();
91
92 // set up utility actions menu
93 mActionImportAuthenticationConfigs = new QAction( tr( "Import authentication configurations from file" ), this );
94 mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export selected authentication configurations to file" ), this );
95 mActionSetMasterPassword = new QAction( QStringLiteral( "Input master password" ), this );
96 mActionClearCachedMasterPassword = new QAction( QStringLiteral( "Clear cached master password" ), this );
97 mActionResetMasterPassword = new QAction( QStringLiteral( "Reset master password" ), this );
98 mActionClearCachedAuthConfigs = new QAction( QStringLiteral( "Clear cached authentication configurations" ), this );
99 mActionRemoveAuthConfigs = new QAction( QStringLiteral( "Remove all authentication configurations" ), this );
100 mActionEraseAuthDatabase = new QAction( QStringLiteral( "Erase authentication database" ), this );
101
102 connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::importAuthenticationConfigs );
103 connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::exportSelectedAuthenticationConfigs );
104 connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
105 connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
106 connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
107 connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedAuthenticationConfigs );
108 connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::removeAuthenticationConfigs );
109 connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthConfigEditor::eraseAuthenticationDatabase );
110
111 mAuthUtilitiesMenu = new QMenu( this );
112 mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
113 mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
114 mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
115 mAuthUtilitiesMenu->addSeparator();
116 mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
117 mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
118 mAuthUtilitiesMenu->addSeparator();
119 mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
120 mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
121 mAuthUtilitiesMenu->addSeparator();
122 mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
123
124 btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
125 lblAuthConfigDb->setVisible( false );
126 }
127}
128
129void QgsAuthConfigEditor::importAuthenticationConfigs()
130{
132}
133
134void QgsAuthConfigEditor::exportSelectedAuthenticationConfigs()
135{
137}
138
139void QgsAuthConfigEditor::setMasterPassword()
140{
142}
143
144void QgsAuthConfigEditor::clearCachedMasterPassword()
145{
147}
148
149void QgsAuthConfigEditor::resetMasterPassword()
150{
151 QgsAuthGuiUtils::resetMasterPassword( messageBar(), this );
152}
153
154void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
155{
157}
158
159void QgsAuthConfigEditor::removeAuthenticationConfigs()
160{
162}
163
164void QgsAuthConfigEditor::eraseAuthenticationDatabase()
165{
167}
168
169void QgsAuthConfigEditor::authMessageOut( const QString &message, const QString &authtag, QgsAuthManager::MessageLevel level )
170{
171 const int levelint = static_cast<int>( level );
172 messageBar()->pushMessage( authtag, message, ( Qgis::MessageLevel )levelint );
173}
174
176{
177 if ( !mDisabled )
178 {
179 lblAuthConfigDb->setVisible( visible );
180 }
181}
182
184{
185 QStringList ids;
186 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
187 for ( const QModelIndex index : selection )
188 {
189 ids << index.sibling( index.row(), 0 ).data().toString();
190 }
191 return ids;
192}
193
195{
196 if ( !mDisabled )
197 {
198 btnAuthUtilities->setVisible( show );
199 }
200}
201
203{
204 if ( mDisabled )
205 {
206 return;
207 }
208 if ( relay == mRelayMessages )
209 {
210 return;
211 }
212
213 if ( mRelayMessages )
214 {
216 this, &QgsAuthConfigEditor::authMessageOut );
217 mRelayMessages = relay;
218 return;
219 }
220
222 this, &QgsAuthConfigEditor::authMessageOut );
223 mRelayMessages = relay;
224}
225
226void QgsAuthConfigEditor::refreshTableView()
227{
228 mConfigModel->select();
229 tableViewConfigs->reset();
230}
231
232void QgsAuthConfigEditor::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
233{
234 Q_UNUSED( selected )
235 Q_UNUSED( deselected )
236 checkSelection();
237}
238
239void QgsAuthConfigEditor::checkSelection()
240{
241 const bool hasselection = tableViewConfigs->selectionModel()->selection().length() > 0;
242 btnEditConfig->setEnabled( hasselection );
243 btnRemoveConfig->setEnabled( hasselection );
244}
245
246void QgsAuthConfigEditor::btnAddConfig_clicked()
247{
248 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
249 return;
250
251 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this );
252 ace->setWindowModality( Qt::WindowModal );
253 if ( ace->exec() )
254 {
255 mConfigModel->select();
256 }
257 ace->deleteLater();
258}
259
260void QgsAuthConfigEditor::btnEditConfig_clicked()
261{
262 const QString authcfg = selectedConfigId();
263
264 if ( authcfg.isEmpty() )
265 return;
266
267 if ( !QgsApplication::authManager()->setMasterPassword( true ) )
268 return;
269
270 QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this, authcfg );
271 ace->setWindowModality( Qt::WindowModal );
272 if ( ace->exec() )
273 {
274 mConfigModel->select();
275 }
276 ace->deleteLater();
277}
278
279void QgsAuthConfigEditor::btnRemoveConfig_clicked()
280{
281 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
282
283 if ( selection.empty() )
284 return;
285
286 for ( const QModelIndex index : selection )
287 {
288 const QString name = index.sibling( index.row(), 1 ).data().toString();
289
290 if ( QMessageBox::warning( this, tr( "Remove Configuration" ),
291 tr( "Are you sure you want to remove '%1'?\n\n"
292 "Operation can NOT be undone!" ).arg( name ),
293 QMessageBox::Ok | QMessageBox::Cancel,
294 QMessageBox::Cancel ) == QMessageBox::Ok )
295 {
296 mConfigModel->removeRow( index.row() );
297 }
298 }
299}
300
301QgsMessageBar *QgsAuthConfigEditor::messageBar()
302{
303 return mMsgBar;
304}
305
306QString QgsAuthConfigEditor::selectedConfigId()
307{
308 const QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
309
310 if ( selection.empty() )
311 return QString();
312
313 const QModelIndex indx = selection.at( 0 );
314 return indx.sibling( indx.row(), 0 ).data().toString();
315}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:115
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.
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)
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
void messageOut(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level=QgsAuthManager::INFO) const
Custom logging signal to relay to console output and QgsMessageLog.
void authDatabaseChanged()
Emitted when the authentication db is significantly changed, e.g. large record removal,...
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
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.