QGIS API Documentation
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 
23 #include <QPushButton>
24 #include <QSettings>
25 #include <QThread>
26 
27 static QString invalidStyle_( const QString& selector = "QLineEdit" )
28 {
29  return QString( "%1{color: rgb(200, 0, 0);}" ).arg( selector );
30 }
31 
33  : QDialog( parent, fl )
34  , mOkButton( nullptr )
35 {
36  setupUi( this );
37  setInstance( this );
38  connect( this, SIGNAL( credentialsRequested( QString, QString *, QString *, QString, bool * ) ),
39  this, SLOT( requestCredentials( QString, QString *, QString *, QString, bool * ) ),
40  Qt::BlockingQueuedConnection );
41  connect( this, SIGNAL( credentialsRequestedMasterPassword( QString *, bool, bool * ) ),
42  this, SLOT( requestCredentialsMasterPassword( QString *, bool, bool * ) ),
43  Qt::BlockingQueuedConnection );
44  mOkButton = buttonBox->button( QDialogButtonBox::Ok );
45  leMasterPass->setPlaceholderText( tr( "Required" ) );
46 }
47 
49 {
50 }
51 
52 bool QgsCredentialDialog::request( const QString& realm, QString &username, QString &password, const QString& message )
53 {
54  bool ok;
55  if ( qApp->thread() != QThread::currentThread() )
56  {
57  QgsDebugMsg( "emitting signal" );
58  emit credentialsRequested( realm, &username, &password, message, &ok );
59  QgsDebugMsg( QString( "signal returned %1 (username=%2, password=%3)" ).arg( ok ? "true" : "false", username, password ) );
60  }
61  else
62  {
63  requestCredentials( realm, &username, &password, message, &ok );
64  }
65  return ok;
66 }
67 
68 void QgsCredentialDialog::requestCredentials( const QString& realm, QString *username, QString *password, const QString& message, bool *ok )
69 {
70  Q_ASSERT( qApp->thread() == thread() && thread() == QThread::currentThread() );
71  QgsDebugMsg( "Entering." );
72  stackedWidget->setCurrentIndex( 0 );
73 
74  labelRealm->setText( realm );
75  leUsername->setText( *username );
76  lePassword->setText( *password );
77  labelMessage->setText( message );
78  labelMessage->setHidden( message.isEmpty() );
79 
80  if ( !leUsername->text().isEmpty() )
81  lePassword->setFocus();
82 
83  QWidget *activeWindow = qApp->activeWindow();
84 
85  QApplication::setOverrideCursor( Qt::ArrowCursor );
86 
87  QgsDebugMsg( "exec()" );
88  *ok = exec() == QDialog::Accepted;
89  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
90 
92 
93  if ( activeWindow )
94  activeWindow->raise();
95 
96  if ( *ok )
97  {
98  *username = leUsername->text();
99  *password = lePassword->text();
100  }
101 }
102 
104 {
105  bool ok;
106  if ( qApp->thread() != QThread::currentThread() )
107  {
108  QgsDebugMsg( "emitting signal" );
109  emit credentialsRequestedMasterPassword( &password, stored, &ok );
110  }
111  else
112  {
113  requestCredentialsMasterPassword( &password, stored, &ok );
114  }
115  return ok;
116 }
117 
118 void QgsCredentialDialog::requestCredentialsMasterPassword( QString * password, bool stored , bool *ok )
119 {
120  QgsDebugMsg( "Entering." );
121  stackedWidget->setCurrentIndex( 1 );
122 
123  QString titletxt( stored ? tr( "Enter CURRENT master authentication password" ) : tr( "Set NEW master authentication password" ) );
124  lblPasswordTitle->setText( titletxt );
125 
126  leMasterPassVerify->setVisible( !stored );
127  lblDontForget->setVisible( !stored );
128 
129  QApplication::setOverrideCursor( Qt::ArrowCursor );
130 
131  grpbxPassAttempts->setVisible( false );
132  int passfailed = 0;
133  while ( true )
134  {
135  mOkButton->setEnabled( false );
136  // TODO: have the number of attempted passwords configurable in auth settings?
137  if ( passfailed >= 3 )
138  {
139  lblSavedForSession->setVisible( false );
140  grpbxPassAttempts->setTitle( tr( "Password attempts: %1" ).arg( passfailed ) );
141  grpbxPassAttempts->setVisible( true );
142  }
143 
144  // resize vertically to fit contents
145  QSize s = sizeHint();
146  s.setWidth( width() );
147  resize( s );
148 
149  QgsDebugMsg( "exec()" );
150  *ok = exec() == QDialog::Accepted;
151  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
152 
153  if ( *ok )
154  {
155  bool passok = !leMasterPass->text().isEmpty();
156  if ( passok && stored && !chkbxEraseAuthDb->isChecked() )
157  {
158  passok = QgsAuthManager::instance()->verifyMasterPassword( leMasterPass->text() );
159  }
160 
161  if ( passok && !stored )
162  {
163  passok = ( leMasterPass->text() == leMasterPassVerify->text() );
164  }
165 
166  if ( passok || chkbxEraseAuthDb->isChecked() )
167  {
168  if ( stored && chkbxEraseAuthDb->isChecked() )
169  {
171  }
172  else
173  {
174  *password = leMasterPass->text();
175  }
176  break;
177  }
178  else
179  {
180  if ( stored )
181  ++passfailed;
182 
183  leMasterPass->setStyleSheet( invalidStyle_() );
184  if ( leMasterPassVerify->isVisible() )
185  {
186  leMasterPassVerify->setStyleSheet( invalidStyle_() );
187  }
188  }
189  }
190  else
191  {
192  break;
193  }
194 
195  if ( passfailed >= 5 )
196  {
197  break;
198  }
199  }
200 
201  // don't leave master password in singleton's text field, or the ability to show it
202  leMasterPass->clear();
203  chkMasterPassShow->setChecked( false );
204  leMasterPassVerify->clear();
205 
206  chkbxEraseAuthDb->setChecked( false );
207  lblSavedForSession->setVisible( true );
208 
209  // re-enable OK button or non-master-password requests will be blocked
210  // needs to come after leMasterPass->clear() or textChanged auto-slot with disable it again
211  mOkButton->setEnabled( true );
212 
214 
215  if ( passfailed >= 5 )
216  {
217  close();
218  }
219 }
220 
221 void QgsCredentialDialog::on_chkMasterPassShow_stateChanged( int state )
222 {
223  leMasterPass->setEchoMode(( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
224  leMasterPassVerify->setEchoMode(( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
225 }
226 
227 void QgsCredentialDialog::on_leMasterPass_textChanged( const QString &pass )
228 {
229  leMasterPass->setStyleSheet( "" );
230  bool passok = !pass.isEmpty(); // regardless of new or comparing existing, empty password disallowed
231  if ( leMasterPassVerify->isVisible() )
232  {
233  leMasterPassVerify->setStyleSheet( "" );
234  passok = passok && ( leMasterPass->text() == leMasterPassVerify->text() );
235  }
236  mOkButton->setEnabled( passok );
237 
238  if ( leMasterPassVerify->isVisible() && !passok )
239  {
240  leMasterPass->setStyleSheet( invalidStyle_() );
241  leMasterPassVerify->setStyleSheet( invalidStyle_() );
242  }
243 }
244 
245 void QgsCredentialDialog::on_leMasterPassVerify_textChanged( const QString &pass )
246 {
247  if ( leMasterPassVerify->isVisible() )
248  {
249  leMasterPass->setStyleSheet( "" );
250  leMasterPassVerify->setStyleSheet( "" );
251 
252  // empty password disallowed
253  bool passok = !pass.isEmpty() && ( leMasterPass->text() == leMasterPassVerify->text() );
254  mOkButton->setEnabled( passok );
255  if ( !passok )
256  {
257  leMasterPass->setStyleSheet( invalidStyle_() );
258  leMasterPassVerify->setStyleSheet( invalidStyle_() );
259  }
260  }
261 }
262 
263 void QgsCredentialDialog::on_chkbxEraseAuthDb_toggled( bool checked )
264 {
265  if ( checked )
266  mOkButton->setEnabled( true );
267 }
268 
bool close()
void setupUi(QWidget *widget)
static QgsAuthManager * instance()
Enforce singleton pattern.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
int exec()
QgsCredentialDialog(QWidget *parent=nullptr, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags)
QThread * thread() const
QString tr(const char *sourceText, const char *disambiguation, int n)
void clear()
int width() const
void resize(int w, int h)
void setEnabled(bool)
void credentialsRequested(const QString &, QString *, QString *, const QString &, bool *)
void setWidth(int width)
void raise()
bool isEmpty() const
static QString invalidStyle_(const QString &selector="QLineEdit")
void setOverrideCursor(const QCursor &cursor)
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString::null) override
request a password
void restoreOverrideCursor()
virtual QSize sizeHint() const
void setScheduledAuthDbErase(bool scheduleErase)
Schedule an optional erase of authentication database, starting when mutex is lockable.
QThread * currentThread()
bool verifyMasterPassword(const QString &compare=QString::null)
Verify the supplied master password against any existing hash in authentication database.
typedef WindowFlags
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual bool requestMasterPassword(QString &password, bool stored=false) override
request a master password
void credentialsRequestedMasterPassword(QString *, bool, bool *)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setInstance(QgsCredentials *theInstance)
register instance