QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
30 QgsAuthConfigEditor::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  mActionSetMasterPassword = new QAction( QStringLiteral( "Input master password" ), this );
94  mActionClearCachedMasterPassword = new QAction( QStringLiteral( "Clear cached master password" ), this );
95  mActionResetMasterPassword = new QAction( QStringLiteral( "Reset master password" ), this );
96  mActionClearCachedAuthConfigs = new QAction( QStringLiteral( "Clear cached authentication configurations" ), this );
97  mActionRemoveAuthConfigs = new QAction( QStringLiteral( "Remove all authentication configurations" ), this );
98  mActionEraseAuthDatabase = new QAction( QStringLiteral( "Erase authentication database" ), this );
99 
100  connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
101  connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
102  connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
103  connect( mActionClearCachedAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedAuthenticationConfigs );
104  connect( mActionRemoveAuthConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::removeAuthenticationConfigs );
105  connect( mActionEraseAuthDatabase, &QAction::triggered, this, &QgsAuthConfigEditor::eraseAuthenticationDatabase );
106 
107  mAuthUtilitiesMenu = new QMenu( this );
108  mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
109  mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
110  mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
111  mAuthUtilitiesMenu->addSeparator();
112  mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
113  mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
114  mAuthUtilitiesMenu->addSeparator();
115  mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
116 
117  btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
118  lblAuthConfigDb->setVisible( false );
119  }
120 }
121 
122 void QgsAuthConfigEditor::setMasterPassword()
123 {
124  QgsAuthGuiUtils::setMasterPassword( messageBar(), messageTimeout() );
125 }
126 
127 void QgsAuthConfigEditor::clearCachedMasterPassword()
128 {
129  QgsAuthGuiUtils::clearCachedMasterPassword( messageBar(), messageTimeout() );
130 }
131 
132 void QgsAuthConfigEditor::resetMasterPassword()
133 {
134  QgsAuthGuiUtils::resetMasterPassword( messageBar(), messageTimeout(), this );
135 }
136 
137 void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
138 {
139  QgsAuthGuiUtils::clearCachedAuthenticationConfigs( messageBar(), messageTimeout() );
140 }
141 
142 void QgsAuthConfigEditor::removeAuthenticationConfigs()
143 {
144  QgsAuthGuiUtils::removeAuthenticationConfigs( messageBar(), messageTimeout(), this );
145 }
146 
147 void QgsAuthConfigEditor::eraseAuthenticationDatabase()
148 {
149  QgsAuthGuiUtils::eraseAuthenticationDatabase( messageBar(), messageTimeout(), this );
150 }
151 
152 void QgsAuthConfigEditor::authMessageOut( const QString &message, const QString &authtag, QgsAuthManager::MessageLevel level )
153 {
154  int levelint = static_cast<int>( level );
155  messageBar()->pushMessage( authtag, message, ( Qgis::MessageLevel )levelint, 7 );
156 }
157 
159 {
160  if ( !mDisabled )
161  {
162  lblAuthConfigDb->setVisible( visible );
163  }
164 }
165 
167 {
168  if ( !mDisabled )
169  {
170  btnAuthUtilities->setVisible( show );
171  }
172 }
173 
175 {
176  if ( mDisabled )
177  {
178  return;
179  }
180  if ( relay == mRelayMessages )
181  {
182  return;
183  }
184 
185  if ( mRelayMessages )
186  {
188  this, &QgsAuthConfigEditor::authMessageOut );
189  mRelayMessages = relay;
190  return;
191  }
192 
194  this, &QgsAuthConfigEditor::authMessageOut );
195  mRelayMessages = relay;
196 }
197 
198 void QgsAuthConfigEditor::refreshTableView()
199 {
200  mConfigModel->select();
201  tableViewConfigs->reset();
202 }
203 
204 void QgsAuthConfigEditor::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
205 {
206  Q_UNUSED( selected )
207  Q_UNUSED( deselected )
208  checkSelection();
209 }
210 
211 void QgsAuthConfigEditor::checkSelection()
212 {
213  bool hasselection = tableViewConfigs->selectionModel()->selection().length() > 0;
214  btnEditConfig->setEnabled( hasselection );
215  btnRemoveConfig->setEnabled( hasselection );
216 }
217 
218 void QgsAuthConfigEditor::btnAddConfig_clicked()
219 {
220  if ( !QgsApplication::authManager()->setMasterPassword( true ) )
221  return;
222 
223  QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this );
224  ace->setWindowModality( Qt::WindowModal );
225  if ( ace->exec() )
226  {
227  mConfigModel->select();
228  }
229  ace->deleteLater();
230 }
231 
232 void QgsAuthConfigEditor::btnEditConfig_clicked()
233 {
234  QString authcfg = selectedConfigId();
235 
236  if ( authcfg.isEmpty() )
237  return;
238 
239  if ( !QgsApplication::authManager()->setMasterPassword( true ) )
240  return;
241 
242  QgsAuthConfigEdit *ace = new QgsAuthConfigEdit( this, authcfg );
243  ace->setWindowModality( Qt::WindowModal );
244  if ( ace->exec() )
245  {
246  mConfigModel->select();
247  }
248  ace->deleteLater();
249 }
250 
251 void QgsAuthConfigEditor::btnRemoveConfig_clicked()
252 {
253  QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
254 
255  if ( selection.empty() )
256  return;
257 
258  QModelIndex indx = selection.at( 0 );
259  QString name = indx.sibling( indx.row(), 1 ).data().toString();
260 
261  if ( QMessageBox::warning( this, tr( "Remove Configuration" ),
262  tr( "Are you sure you want to remove '%1'?\n\n"
263  "Operation can NOT be undone!" ).arg( name ),
264  QMessageBox::Ok | QMessageBox::Cancel,
265  QMessageBox::Cancel ) == QMessageBox::Ok )
266  {
267  mConfigModel->removeRow( indx.row() );
268  }
269 }
270 
271 QgsMessageBar *QgsAuthConfigEditor::messageBar()
272 {
273  return mMsgBar;
274 }
275 
276 int QgsAuthConfigEditor::messageTimeout()
277 {
278  QgsSettings settings;
279  return settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
280 }
281 
282 QString QgsAuthConfigEditor::selectedConfigId()
283 {
284  QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
285 
286  if ( selection.empty() )
287  return QString();
288 
289  QModelIndex indx = selection.at( 0 );
290  return indx.sibling( indx.row(), 0 ).data().toString();
291 }
QgsAuthManager::messageOut
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.
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsAuthGuiUtils::clearCachedAuthenticationConfigs
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0)
Clear all cached authentication configs for session.
Definition: qgsauthguiutils.cpp:159
qgsauthmanager.h
QgsAuthGuiUtils::removeAuthenticationConfigs
static void removeAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Remove all authentication configs.
Definition: qgsauthguiutils.cpp:169
QgsAuthGuiUtils::clearCachedMasterPassword
static void clearCachedMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Clear the currently cached master password (not its hash in database)
Definition: qgsauthguiutils.cpp:93
QgsAuthGuiUtils::resetMasterPassword
static void resetMasterPassword(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Reset the cached master password, updating its hash in authentication database and resetting all exis...
Definition: qgsauthguiutils.cpp:115
QgsSettings
Definition: qgssettings.h:61
QgsAuthGuiUtils::eraseAuthenticationDatabase
static void eraseAuthenticationDatabase(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Completely clear out the authentication database (configs and master password)
Definition: qgsauthguiutils.cpp:196
qgsauthconfigeditor.h
qgsauthguiutils.h
QgsAuthConfigEditor::setShowUtilitiesButton
void setShowUtilitiesButton(bool show=true)
Sets whether to show the widget's utilities button, e.g. when embedding.
Definition: qgsauthconfigeditor.cpp:166
QgsApplication::authManager
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Definition: qgsapplication.cpp:1263
qgsapplication.h
QgsAuthConfigEditor::toggleTitleVisibility
void toggleTitleVisibility(bool visible)
Hide the widget's title, e.g. when embedding.
Definition: qgsauthconfigeditor.cpp:158
QgsAuthManager::MessageLevel
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
Definition: qgsauthmanager.h:71
qgsauthconfigedit.h
QgsMessageBar
Definition: qgsmessagebar.h:60
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:101
QgsAuthManager::authDatabaseChanged
void authDatabaseChanged()
Emitted when the authentication db is significantly changed, e.g. large record removal,...
QgsAuthConfigEdit
Definition: qgsauthconfigedit.h:36
qgssettings.h
QgsAuthConfigEditor::setRelayMessages
void setRelayMessages(bool relay=true)
Sets whether to relay auth manager messages to internal message bar, e.g. when embedding.
Definition: qgsauthconfigeditor.cpp:174
QgsAuthConfigEditor::QgsAuthConfigEditor
QgsAuthConfigEditor(QWidget *parent=nullptr, bool showUtilities=true, bool relayMessages=true)
Widget for editing authentication configurations directly in database.
Definition: qgsauthconfigeditor.cpp:30
QgsAuthGuiUtils::setMasterPassword
static void setMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Sets the cached master password (and verifies it if its hash is in authentication database)
Definition: qgsauthguiutils.cpp:78
QgsMessageBar::pushMessage
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
A convenience method for pushing a message with the specified text to the bar.
Definition: qgsmessagebar.cpp:379