QGIS API Documentation  3.0.2-Girona (307d082)
qgscredentialdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscredentialdialog.cpp - description
3  -------------------
4  begin : February 2010
5  copyright : (C) 2010 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgscredentialdialog.h"
19 
20 #include "qgsauthmanager.h"
21 #include "qgslogger.h"
22 #include "qgssettings.h"
23 #include "qgsapplication.h"
24 
25 #include <QPushButton>
26 #include <QThread>
27 
28 static QString invalidStyle_( const QString &selector = QStringLiteral( "QLineEdit" ) )
29 {
30  return QStringLiteral( "%1{color: rgb(200, 0, 0);}" ).arg( selector );
31 }
32 
33 QgsCredentialDialog::QgsCredentialDialog( QWidget *parent, Qt::WindowFlags fl )
34  : QDialog( parent, fl )
35 
36 {
37  setupUi( this );
38  connect( leMasterPass, &QgsPasswordLineEdit::textChanged, this, &QgsCredentialDialog::leMasterPass_textChanged );
39  connect( leMasterPassVerify, &QgsPasswordLineEdit::textChanged, this, &QgsCredentialDialog::leMasterPassVerify_textChanged );
40  connect( chkbxEraseAuthDb, &QCheckBox::toggled, this, &QgsCredentialDialog::chkbxEraseAuthDb_toggled );
41  setInstance( this );
43  this, &QgsCredentialDialog::requestCredentials,
44  Qt::BlockingQueuedConnection );
46  this, &QgsCredentialDialog::requestCredentialsMasterPassword,
47  Qt::BlockingQueuedConnection );
48  mOkButton = buttonBox->button( QDialogButtonBox::Ok );
49  leMasterPass->setPlaceholderText( tr( "Required" ) );
50  chkbxPasswordHelperEnable->setText( tr( "Store/update the master password in your %1" )
52  leUsername->setFocus();
53 }
54 
55 bool QgsCredentialDialog::request( const QString &realm, QString &username, QString &password, const QString &message )
56 {
57  bool ok;
58  if ( qApp->thread() != QThread::currentThread() )
59  {
60  QgsDebugMsg( "emitting signal" );
61  emit credentialsRequested( realm, &username, &password, message, &ok );
62  QgsDebugMsg( QString( "signal returned %1 (username=%2, password=%3)" ).arg( ok ? "true" : "false", username, password ) );
63  }
64  else
65  {
66  requestCredentials( realm, &username, &password, message, &ok );
67  }
68  return ok;
69 }
70 
71 void QgsCredentialDialog::requestCredentials( const QString &realm, QString *username, QString *password, const QString &message, bool *ok )
72 {
73  Q_ASSERT( qApp->thread() == thread() && thread() == QThread::currentThread() );
74  QgsDebugMsg( "Entering." );
75  stackedWidget->setCurrentIndex( 0 );
76 
77  chkbxPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
78  labelRealm->setText( realm );
79  leUsername->setText( *username );
80  lePassword->setText( *password );
81  labelMessage->setText( message );
82  labelMessage->setHidden( message.isEmpty() );
83 
84  if ( !leUsername->text().isEmpty() )
85  lePassword->setFocus();
86 
87  QWidget *activeWindow = qApp->activeWindow();
88 
89  QApplication::setOverrideCursor( Qt::ArrowCursor );
90 
91  QgsDebugMsg( "exec()" );
92  *ok = exec() == QDialog::Accepted;
93  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
94 
95  QApplication::restoreOverrideCursor();
96 
97  if ( activeWindow )
98  activeWindow->raise();
99 
100  if ( *ok )
101  {
102  *username = leUsername->text();
103  *password = lePassword->text();
104  }
105 }
106 
107 bool QgsCredentialDialog::requestMasterPassword( QString &password, bool stored )
108 {
109  bool ok;
110  if ( qApp->thread() != QThread::currentThread() )
111  {
112  QgsDebugMsg( "emitting signal" );
113  emit credentialsRequestedMasterPassword( &password, stored, &ok );
114  }
115  else
116  {
117  requestCredentialsMasterPassword( &password, stored, &ok );
118  }
119  return ok;
120 }
121 
122 void QgsCredentialDialog::requestCredentialsMasterPassword( QString *password, bool stored, bool *ok )
123 {
124  QgsDebugMsg( "Entering." );
125  stackedWidget->setCurrentIndex( 1 );
126  leMasterPass->setFocus();
127 
128  QString titletxt( stored ? tr( "Enter CURRENT master authentication password" ) : tr( "Set NEW master authentication password" ) );
129  lblPasswordTitle->setText( titletxt );
130 
131  chkbxPasswordHelperEnable->setChecked( QgsApplication::authManager()->passwordHelperEnabled() );
132 
133  leMasterPassVerify->setVisible( !stored );
134  lblDontForget->setVisible( !stored );
135 
136  QApplication::setOverrideCursor( Qt::ArrowCursor );
137 
138  grpbxPassAttempts->setVisible( false );
139  int passfailed = 0;
140  while ( true )
141  {
142  mOkButton->setEnabled( false );
143  // TODO: have the number of attempted passwords configurable in auth settings?
144  if ( passfailed >= 3 )
145  {
146  lblSavedForSession->setVisible( false );
147  grpbxPassAttempts->setTitle( tr( "Password attempts: %1" ).arg( passfailed ) );
148  grpbxPassAttempts->setVisible( true );
149  }
150 
151  // resize vertically to fit contents
152  QSize s = sizeHint();
153  s.setWidth( width() );
154  resize( s );
155 
156  QgsDebugMsg( "exec()" );
157  *ok = exec() == QDialog::Accepted;
158  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
159 
160  if ( *ok )
161  {
162  bool passok = !leMasterPass->text().isEmpty();
163  if ( passok && stored && !chkbxEraseAuthDb->isChecked() )
164  {
165  passok = QgsApplication::authManager()->verifyMasterPassword( leMasterPass->text() );
166  }
167 
168  if ( passok && !stored )
169  {
170  passok = ( leMasterPass->text() == leMasterPassVerify->text() );
171  }
172 
173  if ( passok || chkbxEraseAuthDb->isChecked() )
174  {
175  if ( stored && chkbxEraseAuthDb->isChecked() )
176  {
178  }
179  else
180  {
181  *password = leMasterPass->text();
182  // Let's store user's preferences to use the password helper
183  if ( chkbxPasswordHelperEnable->isChecked() != QgsApplication::authManager()->passwordHelperEnabled() )
184  {
185  QgsApplication::authManager()->setPasswordHelperEnabled( chkbxPasswordHelperEnable->isChecked() );
186  }
187  }
188  break;
189  }
190  else
191  {
192  if ( stored )
193  ++passfailed;
194 
195  leMasterPass->setStyleSheet( invalidStyle_() );
196  if ( leMasterPassVerify->isVisible() )
197  {
198  leMasterPassVerify->setStyleSheet( invalidStyle_() );
199  }
200  }
201  }
202  else
203  {
204  break;
205  }
206 
207  if ( passfailed >= 5 )
208  {
209  break;
210  }
211  }
212 
213  // don't leave master password in singleton's text field, or the ability to show it
214  leMasterPass->clear();
215  leMasterPassVerify->clear();
216 
217  chkbxEraseAuthDb->setChecked( false );
218  lblSavedForSession->setVisible( true );
219 
220  // re-enable OK button or non-master-password requests will be blocked
221  // needs to come after leMasterPass->clear() or textChanged auto-slot with disable it again
222  mOkButton->setEnabled( true );
223 
224  QApplication::restoreOverrideCursor();
225 
226  if ( passfailed >= 5 )
227  {
228  close();
229  }
230 }
231 
232 void QgsCredentialDialog::leMasterPass_textChanged( const QString &pass )
233 {
234  leMasterPass->setStyleSheet( QLatin1String( "" ) );
235  bool passok = !pass.isEmpty(); // regardless of new or comparing existing, empty password disallowed
236  if ( leMasterPassVerify->isVisible() )
237  {
238  leMasterPassVerify->setStyleSheet( QLatin1String( "" ) );
239  passok = passok && ( leMasterPass->text() == leMasterPassVerify->text() );
240  }
241  mOkButton->setEnabled( passok );
242 
243  if ( leMasterPassVerify->isVisible() && !passok )
244  {
245  leMasterPass->setStyleSheet( invalidStyle_() );
246  leMasterPassVerify->setStyleSheet( invalidStyle_() );
247  }
248 }
249 
250 void QgsCredentialDialog::leMasterPassVerify_textChanged( const QString &pass )
251 {
252  if ( leMasterPassVerify->isVisible() )
253  {
254  leMasterPass->setStyleSheet( QLatin1String( "" ) );
255  leMasterPassVerify->setStyleSheet( QLatin1String( "" ) );
256 
257  // empty password disallowed
258  bool passok = !pass.isEmpty() && ( leMasterPass->text() == leMasterPassVerify->text() );
259  mOkButton->setEnabled( passok );
260  if ( !passok )
261  {
262  leMasterPass->setStyleSheet( invalidStyle_() );
263  leMasterPassVerify->setStyleSheet( invalidStyle_() );
264  }
265  }
266 }
267 
268 void QgsCredentialDialog::chkbxEraseAuthDb_toggled( bool checked )
269 {
270  if ( checked )
271  mOkButton->setEnabled( true );
272 }
273 
bool passwordHelperEnabled() const
Password helper enabled getter.
void setScheduledAuthDatabaseErase(bool scheduleErase)
Schedule an optional erase of authentication database, starting when mutex is lockable.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void setInstance(QgsCredentials *instance)
register instance
static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME
The display name of the password helper (platform dependent)
void credentialsRequested(const QString &, QString *, QString *, const QString &, bool *)
void setPasswordHelperEnabled(const bool enabled)
Password helper enabled setter.
static QgsAuthManager * authManager()
Returns the application&#39;s authentication manager instance.
bool request(const QString &realm, QString &username, QString &password, const QString &message=QString()) override
request a password
QgsCredentialDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
QgsCredentialDialog constructor.
bool requestMasterPassword(QString &password, bool stored=false) override
request a master password
void credentialsRequestedMasterPassword(QString *, bool, bool *)
bool verifyMasterPassword(const QString &compare=QString())
Verify the supplied master password against any existing hash in authentication database.