QGIS API Documentation  3.0.2-Girona (307d082)
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 
23 #include "qgssettings.h"
24 #include "qgsauthmanager.h"
26 #include "qgslogger.h"
27 #include "qgsmessagebar.h"
28 #include "qgsapplication.h"
29 
30 
32 {
33  return QColor( 0, 170, 0 );
34 }
35 
37 {
38  return QColor( 255, 128, 0 );
39 }
40 
42 {
43  return QColor( 200, 0, 0 );
44 }
45 
47 {
48  return QColor( 255, 255, 125 );
49 }
50 
51 QString QgsAuthGuiUtils::greenTextStyleSheet( const QString &selector )
52 {
53  return QStringLiteral( "%1{color: %2;}" ).arg( selector, QgsAuthGuiUtils::greenColor().name() );
54 }
55 
56 QString QgsAuthGuiUtils::orangeTextStyleSheet( const QString &selector )
57 {
58  return QStringLiteral( "%1{color: %2;}" ).arg( selector, QgsAuthGuiUtils::orangeColor().name() );
59 }
60 
61 QString QgsAuthGuiUtils::redTextStyleSheet( const QString &selector )
62 {
63  return QStringLiteral( "%1{color: %2;}" ).arg( selector, QgsAuthGuiUtils::redColor().name() );
64 }
65 
66 bool QgsAuthGuiUtils::isDisabled( QgsMessageBar *msgbar, int timeout )
67 {
69  {
70  msgbar->pushMessage( QObject::tr( "Authentication System" ),
71  QObject::tr( "DISABLED. Resources authenticating via the system can not be accessed" ),
72  Qgis::Critical, timeout );
73  return true;
74  }
75  return false;
76 }
77 
79 {
80  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
81  return;
82 
83  if ( QgsApplication::authManager()->masterPasswordIsSet() )
84  {
85  msgbar->pushMessage( QgsApplication::authManager()->authManTag(),
86  QObject::tr( "Master password already set." ),
87  Qgis::Info, timeout );
88  return;
89  }
91 }
92 
94 {
95  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
96  return;
97 
98  QString msg( QObject::tr( "Master password not cleared because it is not set." ) );
100 
101  if ( QgsApplication::authManager()->masterPasswordIsSet() )
102  {
104  msg = QObject::tr( "Master password cleared (NOTE: network connections may be cached)." );
105  if ( QgsApplication::authManager()->masterPasswordIsSet() )
106  {
107  msg = QObject::tr( "Master password FAILED to be cleared." );
108  level = Qgis::Warning;
109  }
110  }
111 
112  msgbar->pushMessage( QgsApplication::authManager()->authManTag(), msg, level, timeout );
113 }
114 
115 void QgsAuthGuiUtils::resetMasterPassword( QgsMessageBar *msgbar, int timeout, QWidget *parent )
116 {
117  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
118  return;
119 
120  QString msg( QObject::tr( "Master password reset" ) );
122 
123  // check that a master password is even set in auth db
124  if ( !QgsApplication::authManager()->masterPasswordHashInDatabase() )
125  {
126  msg = QObject::tr( "Master password reset: NO current password hash in database" );
127  msgbar->pushMessage( QgsApplication::authManager()->authManTag(), msg, Qgis::Warning, 0 );
128  return;
129  }
130 
131  // get new password via dialog; do current password verification in-dialog
132  QString newpass;
133  QString oldpass;
134  bool keepbackup = false;
135  QgsMasterPasswordResetDialog dlg( parent );
136 
137  if ( !dlg.requestMasterPasswordReset( &newpass, &oldpass, &keepbackup ) )
138  {
139  QgsDebugMsg( "Master password reset: input canceled by user" );
140  return;
141  }
142 
143  QString backuppath;
144  if ( !QgsApplication::authManager()->resetMasterPassword( newpass, oldpass, keepbackup, &backuppath ) )
145  {
146  msg = QObject::tr( "Master password FAILED to be reset" );
147  level = Qgis::Warning;
148  }
149 
150  if ( !backuppath.isEmpty() )
151  {
152  msg += QObject::tr( " (database backup: %1)" ).arg( backuppath );
153  timeout = 0; // no timeout, so user can read backup message
154  }
155 
156  msgbar->pushMessage( QgsApplication::authManager()->authManTag(), msg, level, timeout );
157 }
158 
160 {
161  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
162  return;
163 
165  QString msg = QObject::tr( "Cached authentication configurations for session cleared" );
166  msgbar->pushMessage( QgsApplication::authManager()->authManTag(), msg, Qgis::Info, timeout );
167 }
168 
169 void QgsAuthGuiUtils::removeAuthenticationConfigs( QgsMessageBar *msgbar, int timeout, QWidget *parent )
170 {
171  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
172  return;
173 
174  if ( QMessageBox::warning( parent,
175  QObject::tr( "Remove Configurations" ),
176  QObject::tr( "Are you sure you want to remove ALL authentication configurations?\n\n"
177  "Operation can NOT be undone!" ),
178  QMessageBox::Ok | QMessageBox::Cancel,
179  QMessageBox::Cancel ) == QMessageBox::Cancel )
180  {
181  return;
182  }
183 
184  QString msg( QObject::tr( "Authentication configurations removed." ) );
186 
187  if ( !QgsApplication::authManager()->removeAllAuthenticationConfigs() )
188  {
189  msg = QObject::tr( "Authentication configurations FAILED to be removed." );
190  level = Qgis::Warning;
191  }
192 
193  msgbar->pushMessage( QgsApplication::authManager()->authManTag(), msg, level, timeout );
194 }
195 
196 void QgsAuthGuiUtils::eraseAuthenticationDatabase( QgsMessageBar *msgbar, int timeout, QWidget *parent )
197 {
198  if ( QgsAuthGuiUtils::isDisabled( msgbar, timeout ) )
199  return;
200 
201  QMessageBox::StandardButton btn = QMessageBox::warning(
202  parent,
203  QObject::tr( "Erase Database" ),
204  QObject::tr( "Are you sure you want to ERASE the entire authentication database?\n\n"
205  "Operation can NOT be undone!\n\n"
206  "(Current database will be backed up and new one created.)" ),
207  QMessageBox::Ok | QMessageBox::Cancel,
208  QMessageBox::Cancel );
209 
211 
212  if ( btn == QMessageBox::Cancel )
213  {
214  return;
215  }
216 
217  QString msg( QObject::tr( "Active authentication database erased." ) );
219 
220  QString backuppath;
221  if ( !QgsApplication::authManager()->eraseAuthenticationDatabase( true, &backuppath ) )
222  {
223  msg = QObject::tr( "Authentication database FAILED to be erased." );
224  level = Qgis::Warning;
225  }
226  else
227  {
228  if ( !backuppath.isEmpty() )
229  {
230  msg += QObject::tr( " (backup: %1)" ).arg( backuppath );
231  }
232  level = Qgis::Critical;
233  }
234 
235  timeout = 0; // no timeout, so user can read restart message
236  msgbar->pushMessage( QObject::tr( "RESTART QGIS" ), msg, level, timeout );
237 }
238 
239 void QgsAuthGuiUtils::fileFound( bool found, QWidget *widget )
240 {
241  if ( !found )
242  {
243  widget->setStyleSheet( QgsAuthGuiUtils::redTextStyleSheet( QStringLiteral( "QLineEdit" ) ) );
244  widget->setToolTip( QObject::tr( "File not found" ) );
245  }
246  else
247  {
248  widget->setStyleSheet( QLatin1String( "" ) );
249  widget->setToolTip( QLatin1String( "" ) );
250  }
251 }
252 
253 QString QgsAuthGuiUtils::getOpenFileName( QWidget *parent, const QString &title, const QString &extfilter )
254 {
255  QgsSettings settings;
256  QString recentdir = settings.value( QStringLiteral( "UI/lastAuthOpenFileDir" ), QDir::homePath() ).toString();
257  QString f = QFileDialog::getOpenFileName( parent, title, recentdir, extfilter );
258  if ( !f.isEmpty() )
259  {
260  settings.setValue( QStringLiteral( "UI/lastAuthOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
261  }
262  return f;
263 }
264 
265 void QgsAuthGuiUtils::passwordHelperDelete( QgsMessageBar *msgbar, int timeout, QWidget *parent )
266 {
267  if ( QMessageBox::warning( parent,
268  QObject::tr( "Delete Password" ),
269  QObject::tr( "Do you really want to delete the master password from your %1?" )
271  QMessageBox::Ok | QMessageBox::Cancel,
272  QMessageBox::Cancel ) == QMessageBox::Cancel )
273  {
274  return;
275  }
276  QString msg;
277  Qgis::MessageLevel level;
279  {
281  level = Qgis::Warning;
282  }
283  else
284  {
285  msg = QObject::tr( "Master password was successfully deleted from your %1" )
287 
288  level = Qgis::Info;
289  }
290  msgbar->pushMessage( QObject::tr( "Password helper delete" ), msg, level, timeout );
291 }
292 
294 {
295  QString msg;
296  Qgis::MessageLevel level;
297  if ( ! QgsApplication::authManager()->masterPasswordIsSet() )
298  {
299  msg = QObject::tr( "Master password is not set and cannot be stored in your %1." )
301  level = Qgis::Warning;
302  }
304  {
306  level = Qgis::Warning;
307  }
308  else
309  {
310  msg = QObject::tr( "Master password has been successfully stored in your %1." )
312 
313  level = Qgis::Info;
314  }
315  msgbar->pushMessage( QObject::tr( "Password helper write" ), msg, level, timeout );
316 }
317 
318 void QgsAuthGuiUtils::passwordHelperEnable( bool enabled, QgsMessageBar *msgbar, int timeout )
319 {
321  QString msg = enabled ? QObject::tr( "Your %1 will be <b>used from now</b> on to store and retrieve the master password." )
323  QObject::tr( "Your %1 will <b>not be used anymore</b> to store and retrieve the master password." )
325  msgbar->pushMessage( QObject::tr( "Password helper write" ), msg, Qgis::Info, timeout );
326 }
327 
328 void QgsAuthGuiUtils::passwordHelperLoggingEnable( bool enabled, QgsMessageBar *msgbar, int timeout )
329 {
330  Q_UNUSED( msgbar );
331  Q_UNUSED( timeout );
333 }
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 resetting all exis...
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.
void setScheduledAuthDatabaseErase(bool scheduleErase)
Schedule an optional erase of authentication database, starting when mutex is lockable.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
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:45
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.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:78
static QColor orangeColor()
Orange color representing loaded component, but not stored in database.
static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME
The display name of the password helper (platform dependent)
static void passwordHelperLoggingEnable(bool enabled, QgsMessageBar *msgbar, int timeout=0)
Set password helper logging enabled (enable/disable)
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0)
Clear all cached authentication configs for session.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
static QColor greenColor()
Green color representing valid, trusted, etc. certificate.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
void setPasswordHelperEnabled(const bool enabled)
Password helper enabled setter.
static QString getOpenFileName(QWidget *parent, const QString &title, const QString &extfilter)
Open file dialog for auth associated widgets.
void setPasswordHelperLoggingEnabled(const bool enabled)
Password helper logging enabled setter.
bool requestMasterPasswordReset(QString *newpass, QString *oldpass, bool *keepbackup)
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:88
static void passwordHelperEnable(bool enabled, QgsMessageBar *msgbar, int timeout=0)
Set password helper enabled (enable/disable)
static QgsAuthManager * authManager()
Returns the application&#39;s authentication manager instance.
static void passwordHelperSync(QgsMessageBar *msgbar, int timeout=0)
Store master password into the wallet.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc. certificate.
void clearMasterPassword()
Clear supplied master password.
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
const QString passwordHelperErrorMessage()
Error message getter.
Dialog to verify current master password and initiate reset of authentication database with a new pas...
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.
static void passwordHelperDelete(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Remove master password from wallet.