QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QSettings>
23 #include <QSqlTableModel>
24 
25 #include "qgsauthmanager.h"
26 #include "qgsauthconfigedit.h"
27 #include "qgsauthguiutils.h"
28 
29 QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, bool relayMessages )
30  : QWidget( parent )
31  , mRelayMessages( relayMessages )
32  , mConfigModel( nullptr )
33  , mAuthUtilitiesMenu( nullptr )
34  , mActionSetMasterPassword( nullptr )
35  , mActionClearCachedMasterPassword( nullptr )
36  , mActionResetMasterPassword( nullptr )
37  , mActionClearCachedAuthConfigs( nullptr )
38  , mActionRemoveAuthConfigs( nullptr )
39  , mActionEraseAuthDatabase( nullptr )
40  , mDisabled( false )
41  , mAuthNotifyLayout( nullptr )
42  , mAuthNotify( nullptr )
43 {
44  if ( QgsAuthManager::instance()->isDisabled() )
45  {
46  mDisabled = true;
47  mAuthNotifyLayout = new QVBoxLayout;
48  this->setLayout( mAuthNotifyLayout );
49  mAuthNotify = new QLabel( QgsAuthManager::instance()->disabledMessage(), this );
50  mAuthNotifyLayout->addWidget( mAuthNotify );
51  }
52  else
53  {
54  setupUi( this );
55 
56  setShowUtilitiesButton( showUtilities );
57 
58  mConfigModel = new QSqlTableModel( this, QgsAuthManager::instance()->authDbConnection() );
59  mConfigModel->setTable( QgsAuthManager::instance()->authDbConfigTable() );
60  mConfigModel->select();
61 
62  mConfigModel->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
63  mConfigModel->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
64  mConfigModel->setHeaderData( 2, Qt::Horizontal, tr( "URI" ) );
65  mConfigModel->setHeaderData( 3, Qt::Horizontal, tr( "Type" ) );
66  mConfigModel->setHeaderData( 4, Qt::Horizontal, tr( "Version" ) );
67  mConfigModel->setHeaderData( 5, Qt::Horizontal, tr( "Config" ) );
68 
69  tableViewConfigs->setModel( mConfigModel );
70  tableViewConfigs->resizeColumnsToContents();
71 // tableViewConfigs->resizeColumnToContents( 0 );
72 // tableViewConfigs->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
73 // tableViewConfigs->horizontalHeader()->setResizeMode(2, QHeaderView::Interactive);
74 // tableViewConfigs->resizeColumnToContents( 3 );
75  tableViewConfigs->hideColumn( 4 );
76  tableViewConfigs->hideColumn( 5 );
77 
78  // sort by config 'name'
79  tableViewConfigs->sortByColumn( 1, Qt::AscendingOrder );
80  tableViewConfigs->setSortingEnabled( true );
81 
82  connect( tableViewConfigs->selectionModel(), SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
83  this, SLOT( selectionChanged( const QItemSelection&, const QItemSelection& ) ) );
84 
85  connect( tableViewConfigs, SIGNAL( doubleClicked( QModelIndex ) ),
86  this, SLOT( on_btnEditConfig_clicked() ) );
87 
88  if ( mRelayMessages )
89  {
90  connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
91  this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
92  }
93 
94  connect( QgsAuthManager::instance(), SIGNAL( authDatabaseChanged() ),
95  this, SLOT( refreshTableView() ) );
96 
97  checkSelection();
98 
99  // set up utility actions menu
100  mActionSetMasterPassword = new QAction( "Input master password", this );
101  mActionClearCachedMasterPassword = new QAction( "Clear cached master password", this );
102  mActionResetMasterPassword = new QAction( "Reset master password", this );
103  mActionClearCachedAuthConfigs = new QAction( "Clear cached authentication configurations", this );
104  mActionRemoveAuthConfigs = new QAction( "Remove all authentication configurations", this );
105  mActionEraseAuthDatabase = new QAction( "Erase authentication database", this );
106 
107  connect( mActionSetMasterPassword, SIGNAL( triggered() ), this, SLOT( setMasterPassword() ) );
108  connect( mActionClearCachedMasterPassword, SIGNAL( triggered() ), this, SLOT( clearCachedMasterPassword() ) );
109  connect( mActionResetMasterPassword, SIGNAL( triggered() ), this, SLOT( resetMasterPassword() ) );
110  connect( mActionClearCachedAuthConfigs, SIGNAL( triggered() ), this, SLOT( clearCachedAuthenticationConfigs() ) );
111  connect( mActionRemoveAuthConfigs, SIGNAL( triggered() ), this, SLOT( removeAuthenticationConfigs() ) );
112  connect( mActionEraseAuthDatabase, SIGNAL( triggered() ), this, SLOT( eraseAuthenticationDatabase() ) );
113 
114  mAuthUtilitiesMenu = new QMenu( this );
115  mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
116  mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
117  mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
118  mAuthUtilitiesMenu->addSeparator();
119  mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
120  mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
121  mAuthUtilitiesMenu->addSeparator();
122  mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
123 
124  btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
125  lblAuthConfigDb->setVisible( false );
126  }
127 }
128 
130 {
131 }
132 
133 void QgsAuthConfigEditor::setMasterPassword()
134 {
135  QgsAuthGuiUtils::setMasterPassword( messageBar(), messageTimeout() );
136 }
137 
138 void QgsAuthConfigEditor::clearCachedMasterPassword()
139 {
140  QgsAuthGuiUtils::clearCachedMasterPassword( messageBar(), messageTimeout() );
141 }
142 
143 void QgsAuthConfigEditor::resetMasterPassword()
144 {
145  QgsAuthGuiUtils::resetMasterPassword( messageBar(), messageTimeout(), this );
146 }
147 
148 void QgsAuthConfigEditor::clearCachedAuthenticationConfigs()
149 {
150  QgsAuthGuiUtils::clearCachedAuthenticationConfigs( messageBar(), messageTimeout() );
151 }
152 
153 void QgsAuthConfigEditor::removeAuthenticationConfigs()
154 {
155  QgsAuthGuiUtils::removeAuthenticationConfigs( messageBar(), messageTimeout(), this );
156 }
157 
158 void QgsAuthConfigEditor::eraseAuthenticationDatabase()
159 {
160  QgsAuthGuiUtils::eraseAuthenticationDatabase( messageBar(), messageTimeout(), this );
161 }
162 
163 void QgsAuthConfigEditor::authMessageOut( const QString& message, const QString& authtag, QgsAuthManager::MessageLevel level )
164 {
165  int levelint = ( int )level;
166  messageBar()->pushMessage( authtag, message, ( QgsMessageBar::MessageLevel )levelint, 7 );
167 }
168 
170 {
171  if ( !mDisabled )
172  {
173  lblAuthConfigDb->setVisible( visible );
174  }
175 }
176 
178 {
179  if ( !mDisabled )
180  {
181  btnAuthUtilities->setVisible( show );
182  }
183 }
184 
186 {
187  if ( mDisabled )
188  {
189  return;
190  }
191  if ( relay == mRelayMessages )
192  {
193  return;
194  }
195 
196  if ( mRelayMessages )
197  {
198  disconnect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
199  this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
200  mRelayMessages = relay;
201  return;
202  }
203 
204  connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
205  this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
206  mRelayMessages = relay;
207 }
208 
209 void QgsAuthConfigEditor::refreshTableView()
210 {
211  mConfigModel->select();
212  tableViewConfigs->reset();
213 }
214 
215 void QgsAuthConfigEditor::selectionChanged( const QItemSelection& selected , const QItemSelection& deselected )
216 {
217  Q_UNUSED( selected );
218  Q_UNUSED( deselected );
219  checkSelection();
220 }
221 
222 void QgsAuthConfigEditor::checkSelection()
223 {
224  bool hasselection = tableViewConfigs->selectionModel()->selection().length() > 0;
225  btnEditConfig->setEnabled( hasselection );
226  btnRemoveConfig->setEnabled( hasselection );
227 }
228 
229 void QgsAuthConfigEditor::on_btnAddConfig_clicked()
230 {
231  if ( !QgsAuthManager::instance()->setMasterPassword( true ) )
232  return;
233 
234  QgsAuthConfigEdit * ace = new QgsAuthConfigEdit( this );
235  ace->setWindowModality( Qt::WindowModal );
236  if ( ace->exec() )
237  {
238  mConfigModel->select();
239  }
240  ace->deleteLater();
241 }
242 
243 void QgsAuthConfigEditor::on_btnEditConfig_clicked()
244 {
245  QString authcfg = selectedConfigId();
246 
247  if ( authcfg.isEmpty() )
248  return;
249 
251  return;
252 
253  QgsAuthConfigEdit * ace = new QgsAuthConfigEdit( this, authcfg );
254  ace->setWindowModality( Qt::WindowModal );
255  if ( ace->exec() )
256  {
257  mConfigModel->select();
258  }
259  ace->deleteLater();
260 }
261 
262 void QgsAuthConfigEditor::on_btnRemoveConfig_clicked()
263 {
264  QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
265 
266  if ( selection.empty() )
267  return;
268 
269  QModelIndex indx = selection.at( 0 );
270  QString name = indx.sibling( indx.row(), 1 ).data().toString();
271 
272  if ( QMessageBox::warning( this, tr( "Remove Configuration" ),
273  tr( "Are you sure you want to remove '%1'?\n\n"
274  "Operation can NOT be undone!" ).arg( name ),
275  QMessageBox::Ok | QMessageBox::Cancel,
276  QMessageBox::Cancel ) == QMessageBox::Ok )
277  {
278  mConfigModel->removeRow( indx.row() );
279  }
280 }
281 
282 QgsMessageBar * QgsAuthConfigEditor::messageBar()
283 {
284  return mMsgBar;
285 }
286 
287 int QgsAuthConfigEditor::messageTimeout()
288 {
289  QSettings settings;
290  return settings.value( "/qgis/messageTimeout", 5 ).toInt();
291 }
292 
293 QString QgsAuthConfigEditor::selectedConfigId()
294 {
295  QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
296 
297  if ( selection.empty() )
298  return QString();
299 
300  QModelIndex indx = selection.at( 0 );
301  return indx.sibling( indx.row(), 0 ).data().toString();
302 }
static void resetMasterPassword(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Reset the cached master password, updating its hash in authentication database and reseting all exist...
static void eraseAuthenticationDatabase(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Completely clear out the authentication database (configs and master password)
void setupUi(QWidget *widget)
static QgsAuthManager * instance()
Enforce singleton pattern.
void setWindowModality(Qt::WindowModality windowModality)
bool removeRow(int row, const QModelIndex &parent)
static void clearCachedMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Clear the currently cached master password (not its hash in database)
static void removeAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Remove all authentication configs.
void addAction(QAction *action)
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
int exec()
static void setMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Sets the cached master password (and verifies it if its hash is in authentication database) ...
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QString tr(const char *sourceText, const char *disambiguation, int n)
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0)
Clear all cached authentication configs for session.
const char * name() const
void pushMessage(const QString &text, MessageLevel level=INFO, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:90
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
void setLayout(QLayout *layout)
int toInt(bool *ok) const
bool isEmpty() const
int row() const
QgsAuthConfigEditor(QWidget *parent=nullptr, bool showUtilities=true, bool relayMessages=true)
Widget for editing authentication configurations directly in database.
void deleteLater()
QAction * addSeparator()
void setShowUtilitiesButton(bool show=true)
Set whether to show the widget&#39;s utilities button, e.g.
void toggleTitleVisibility(bool visible)
Hide the widget&#39;s title, e.g.
QVariant value(const QString &key, const QVariant &defaultValue) const
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
QModelIndex sibling(int row, int column) const
virtual bool select()
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
Widget for editing an authentication configuration.
void show()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setRelayMessages(bool relay=true)
Set whether to relay auth manager messages to internal message bar, e.g.
virtual void setTable(const QString &tableName)