QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsauthguiutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsauthutils.cpp
3  ---------------------
4  begin : October 24, 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 "qgsauthguiutils.h"
18 
19 #include <QFileDialog>
20 #include <QLineEdit>
21 #include <QMessageBox>
22 #include <QSettings>
23 
24 #include "qgsauthmanager.h"
26 #include "qgslogger.h"
27 #include "qgsmessagebar.h"
28 
29 
31 {
32  return QColor( 0, 170, 0 );
33 }
34 
36 {
37  return QColor( 255, 128, 0 );
38 }
39 
41 {
42  return QColor( 200, 0, 0 );
43 }
44 
46 {
47  return QColor( 255, 255, 125 );
48 }
49 
51 {
52  return QString( "%1{color: %2;}" ).arg( selector, QgsAuthGuiUtils::greenColor().name() );
53 }
54 
56 {
57  return QString( "%1{color: %2;}" ).arg( selector, QgsAuthGuiUtils::orangeColor().name() );
58 }
59 
61 {
62  return QString( "%1{color: %2;}" ).arg( selector, QgsAuthGuiUtils::redColor().name() );
63 }
64 
65 bool QgsAuthGuiUtils::isDisabled( QgsMessageBar *msgbar, int timeout )
66 {
68  {
69  msgbar->pushMessage( QObject::tr( "Authentication System" ),
70  QObject::tr( "DISABLED. Resources authenticating via the system can not be accessed" ),
71  QgsMessageBar::CRITICAL, timeout );
72  return true;
73  }
74  return false;
75 }
76 
78 {
79  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
80  return;
81 
82  if ( QgsAuthManager::instance()->masterPasswordIsSet() )
83  {
84  msgbar->pushMessage( QgsAuthManager::instance()->authManTag(),
85  QObject::tr( "Master password already set" ),
86  QgsMessageBar::INFO, timeout );
87  return;
88  }
90 }
91 
93 {
94  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
95  return;
96 
97  QString msg( QObject::tr( "Master password not cleared because it is not set" ) );
99 
100  if ( QgsAuthManager::instance()->masterPasswordIsSet() )
101  {
103  msg = QObject::tr( "Master password cleared (NOTE: network connections may be cached)" );
104  if ( QgsAuthManager::instance()->masterPasswordIsSet() )
105  {
106  msg = QObject::tr( "Master password FAILED to be cleared" );
107  level = QgsMessageBar::WARNING;
108  }
109  }
110 
111  msgbar->pushMessage( QgsAuthManager::instance()->authManTag(), msg, level, timeout );
112 }
113 
114 void QgsAuthGuiUtils::resetMasterPassword( QgsMessageBar *msgbar, int timeout, QWidget *parent )
115 {
116  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
117  return;
118 
119  QString msg( QObject::tr( "Master password reset" ) );
121 
122  // check that a master password is even set in auth db
123  if ( !QgsAuthManager::instance()->masterPasswordHashInDb() )
124  {
125  msg = QObject::tr( "Master password reset: NO current password hash in database" );
126  msgbar->pushMessage( QgsAuthManager::instance()->authManTag(), msg, QgsMessageBar::WARNING, 0 );
127  return;
128  }
129 
130  // get new password via dialog; do current password verification in-dialog
131  QString newpass;
132  QString oldpass;
133  bool keepbackup = false;
134  QgsMasterPasswordResetDialog dlg( parent );
135 
136  if ( !dlg.requestMasterPasswordReset( &newpass, &oldpass, &keepbackup ) )
137  {
138  QgsDebugMsg( "Master password reset: input canceled by user" );
139  return;
140  }
141 
142  QString backuppath;
143  if ( !QgsAuthManager::instance()->resetMasterPassword( newpass, oldpass, keepbackup, &backuppath ) )
144  {
145  msg = QObject::tr( "Master password FAILED to be reset" );
146  level = QgsMessageBar::WARNING;
147  }
148 
149  if ( !backuppath.isEmpty() )
150  {
151  msg += QObject::tr( " (database backup: %1)" ).arg( backuppath );
152  timeout = 0; // no timeout, so user can read backup message
153  }
154 
155  msgbar->pushMessage( QgsAuthManager::instance()->authManTag(), msg, level, timeout );
156 }
157 
159 {
160  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
161  return;
162 
164  QString msg = QObject::tr( "Cached authentication configurations for session cleared" );
165  msgbar->pushMessage( QgsAuthManager::instance()->authManTag(), msg, QgsMessageBar::INFO, timeout );
166 }
167 
169 {
170  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
171  return;
172 
173  if ( QMessageBox::warning( parent,
174  QObject::tr( "Remove Configurations" ),
175  QObject::tr( "Are you sure you want to remove ALL authentication configurations?\n\n"
176  "Operation can NOT be undone!" ),
177  QMessageBox::Ok | QMessageBox::Cancel,
178  QMessageBox::Cancel ) == QMessageBox::Cancel )
179  {
180  return;
181  }
182 
183  QString msg( QObject::tr( "Authentication configurations removed" ) );
185 
186  if ( !QgsAuthManager::instance()->removeAllAuthenticationConfigs() )
187  {
188  msg = QObject::tr( "Authentication configurations FAILED to be removed" );
189  level = QgsMessageBar::WARNING;
190  }
191 
192  msgbar->pushMessage( QgsAuthManager::instance()->authManTag(), msg, level, timeout );
193 }
194 
196 {
197  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
198  return;
199 
200  QMessageBox::StandardButton btn = QMessageBox::warning(
201  parent,
202  QObject::tr( "Erase Database" ),
203  QObject::tr( "Are you sure you want to ERASE the entire authentication database?\n\n"
204  "Operation can NOT be undone!\n\n"
205  "(Current database will be backed up and new one created.)" ),
206  QMessageBox::Ok | QMessageBox::Cancel,
207  QMessageBox::Cancel );
208 
210 
211  if ( btn == QMessageBox::Cancel )
212  {
213  return;
214  }
215 
216  QString msg( QObject::tr( "Active authentication database erased" ) );
218 
219  QString backuppath;
220  if ( !QgsAuthManager::instance()->eraseAuthenticationDatabase( true, &backuppath ) )
221  {
222  msg = QObject::tr( "Authentication database FAILED to be erased" );
223  level = QgsMessageBar::WARNING;
224  }
225  else
226  {
227  if ( !backuppath.isEmpty() )
228  {
229  msg += QObject::tr( " (backup: %1)" ).arg( backuppath );
230  }
231  level = QgsMessageBar::CRITICAL;
232  }
233 
234  timeout = 0; // no timeout, so user can read restart message
235  msgbar->pushMessage( QObject::tr( "RESTART QGIS" ), msg, level, timeout );
236 }
237 
238 void QgsAuthGuiUtils::fileFound( bool found, QWidget *widget )
239 {
240  if ( !found )
241  {
242  widget->setStyleSheet( QgsAuthGuiUtils::redTextStyleSheet( "QLineEdit" ) );
243  widget->setToolTip( QObject::tr( "File not found" ) );
244  }
245  else
246  {
247  widget->setStyleSheet( "" );
248  widget->setToolTip( "" );
249  }
250 }
251 
252 QString QgsAuthGuiUtils::getOpenFileName( QWidget *parent, const QString &title, const QString &extfilter )
253 {
254  QSettings settings;
255  QString recentdir = settings.value( "UI/lastAuthOpenFileDir", QDir::homePath() ).toString();
256  QString f = QFileDialog::getOpenFileName( parent, title, recentdir, extfilter );
257  if ( !f.isEmpty() )
258  {
259  settings.setValue( "UI/lastAuthOpenFileDir", QFileInfo( f ).absoluteDir().path() );
260  }
261  return f;
262 }
static bool isDisabled(QgsMessageBar *msgbar, int timeout=0)
Verify the authentication system is active, else notify user.
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...
void setStyleSheet(const QString &styleSheet)
static void eraseAuthenticationDatabase(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Completely clear out the authentication database (configs and master password)
static void fileFound(bool found, QWidget *widget)
Color a widget via a stylesheet if a file path is found or not.
static QgsAuthManager * instance()
Enforce singleton pattern.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
static void setMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Sets the cached master password (and verifies it if its hash is in authentication database) ...
static QColor yellowColor()
Yellow color representing caution regarding action.
QString homePath()
QString tr(const char *sourceText, const char *disambiguation, int n)
static QColor orangeColor()
Orange color representing loaded component, but not stored in database.
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0)
Clear all cached authentication configs for session.
void setValue(const QString &key, const QVariant &value)
void pushMessage(const QString &text, MessageLevel level=INFO, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:90
static QColor redColor()
Red color representing invalid, untrusted, etc.
static QColor greenColor()
Green color representing valid, trusted, etc.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc.
static QString getOpenFileName(QWidget *parent, const QString &title, const QString &extfilter)
Open file dialog for auth associated widgets.
bool isEmpty() const
bool requestMasterPasswordReset(QString *newpass, QString *oldpass, bool *keepbackup)
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc.
QVariant value(const QString &key, const QVariant &defaultValue) const
void clearMasterPassword()
Clear supplied master password.
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
void setScheduledAuthDbErase(bool scheduleErase)
Schedule an optional erase of authentication database, starting when mutex is lockable.
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void setToolTip(const QString &)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
Dialog to verify current master password and initiate reset of authentication database with a new pas...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
static QString orangeTextStyleSheet(const QString &selector="*")
Orange text stylesheet representing loaded component, but not stored in database. ...
void clearAllCachedConfigs()
Clear all authentication configs from authentication method caches.