QGIS API Documentation  2.13.0-Master
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  QgsDebugMsg( "Entering." );
71  stackedWidget->setCurrentIndex( 0 );
72 
73  labelRealm->setText( realm );
74  leUsername->setText( *username );
75  lePassword->setText( *password );
76  labelMessage->setText( message );
77  labelMessage->setHidden( message.isEmpty() );
78 
79  if ( !leUsername->text().isEmpty() )
80  lePassword->setFocus();
81 
82  QWidget *activeWindow = qApp->activeWindow();
83 
84  QApplication::setOverrideCursor( Qt::ArrowCursor );
85 
86  QgsDebugMsg( "exec()" );
87  *ok = exec() == QDialog::Accepted;
88  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
89 
91 
92  if ( activeWindow )
93  activeWindow->raise();
94 
95  if ( *ok )
96  {
97  *username = leUsername->text();
98  *password = lePassword->text();
99  }
100 }
101 
103 {
104  bool ok;
105  if ( qApp->thread() != QThread::currentThread() )
106  {
107  QgsDebugMsg( "emitting signal" );
108  emit credentialsRequestedMasterPassword( &password, stored, &ok );
109  }
110  else
111  {
112  requestCredentialsMasterPassword( &password, stored, &ok );
113  }
114  return ok;
115 }
116 
117 void QgsCredentialDialog::requestCredentialsMasterPassword( QString * password, bool stored , bool *ok )
118 {
119  QgsDebugMsg( "Entering." );
120  stackedWidget->setCurrentIndex( 1 );
121 
122  QString titletxt( stored ? tr( "Enter CURRENT master authentication password" ) : tr( "Set NEW master authentication password" ) );
123  lblPasswordTitle->setText( titletxt );
124 
125  leMasterPassVerify->setVisible( !stored );
126  lblDontForget->setVisible( !stored );
127 
128  QApplication::setOverrideCursor( Qt::ArrowCursor );
129 
130  grpbxPassAttempts->setVisible( false );
131  int passfailed = 0;
132  while ( true )
133  {
134  mOkButton->setEnabled( false );
135  // TODO: have the number of attempted passwords configurable in auth settings?
136  if ( passfailed >= 3 )
137  {
138  lblSavedForSession->setVisible( false );
139  grpbxPassAttempts->setTitle( tr( "Password attempts: %1" ).arg( passfailed ) );
140  grpbxPassAttempts->setVisible( true );
141  }
142 
143  // resize vertically to fit contents
144  QSize s = sizeHint();
145  s.setWidth( width() );
146  resize( s );
147 
148  QgsDebugMsg( "exec()" );
149  *ok = exec() == QDialog::Accepted;
150  QgsDebugMsg( QString( "exec(): %1" ).arg( *ok ? "true" : "false" ) );
151 
152  if ( *ok )
153  {
154  bool passok = !leMasterPass->text().isEmpty();
155  if ( passok && stored && !chkbxEraseAuthDb->isChecked() )
156  {
157  passok = QgsAuthManager::instance()->verifyMasterPassword( leMasterPass->text() );
158  }
159 
160  if ( passok && !stored )
161  {
162  passok = ( leMasterPass->text() == leMasterPassVerify->text() );
163  }
164 
165  if ( passok || chkbxEraseAuthDb->isChecked() )
166  {
167  if ( stored && chkbxEraseAuthDb->isChecked() )
168  {
170  }
171  else
172  {
173  *password = leMasterPass->text();
174  }
175  break;
176  }
177  else
178  {
179  if ( stored )
180  ++passfailed;
181 
182  leMasterPass->setStyleSheet( invalidStyle_() );
183  if ( leMasterPassVerify->isVisible() )
184  {
185  leMasterPassVerify->setStyleSheet( invalidStyle_() );
186  }
187  }
188  }
189  else
190  {
191  break;
192  }
193 
194  if ( passfailed >= 5 )
195  {
196  break;
197  }
198  }
199 
200  // don't leave master password in singleton's text field, or the ability to show it
201  leMasterPass->clear();
202  chkMasterPassShow->setChecked( false );
203  leMasterPassVerify->clear();
204 
205  chkbxEraseAuthDb->setChecked( false );
206  lblSavedForSession->setVisible( true );
207 
208  // re-enable OK button or non-master-password requests will be blocked
209  // needs to come after leMasterPass->clear() or textChanged auto-slot with disable it again
210  mOkButton->setEnabled( true );
211 
213 
214  if ( passfailed >= 5 )
215  {
216  close();
217  }
218 }
219 
220 void QgsCredentialDialog::on_chkMasterPassShow_stateChanged( int state )
221 {
222  leMasterPass->setEchoMode(( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
223  leMasterPassVerify->setEchoMode(( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
224 }
225 
226 void QgsCredentialDialog::on_leMasterPass_textChanged( const QString &pass )
227 {
228  leMasterPass->setStyleSheet( "" );
229  bool passok = !pass.isEmpty(); // regardless of new or comparing existing, empty password disallowed
230  if ( leMasterPassVerify->isVisible() )
231  {
232  leMasterPassVerify->setStyleSheet( "" );
233  passok = passok && ( leMasterPass->text() == leMasterPassVerify->text() );
234  }
235  mOkButton->setEnabled( passok );
236 
237  if ( leMasterPassVerify->isVisible() && !passok )
238  {
239  leMasterPass->setStyleSheet( invalidStyle_() );
240  leMasterPassVerify->setStyleSheet( invalidStyle_() );
241  }
242 }
243 
244 void QgsCredentialDialog::on_leMasterPassVerify_textChanged( const QString &pass )
245 {
246  if ( leMasterPassVerify->isVisible() )
247  {
248  leMasterPass->setStyleSheet( "" );
249  leMasterPassVerify->setStyleSheet( "" );
250 
251  // empty password disallowed
252  bool passok = !pass.isEmpty() && ( leMasterPass->text() == leMasterPassVerify->text() );
253  mOkButton->setEnabled( passok );
254  if ( !passok )
255  {
256  leMasterPass->setStyleSheet( invalidStyle_() );
257  leMasterPassVerify->setStyleSheet( invalidStyle_() );
258  }
259  }
260 }
261 
262 void QgsCredentialDialog::on_chkbxEraseAuthDb_toggled( bool checked )
263 {
264  if ( checked )
265  mOkButton->setEnabled( true );
266 }
267 
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)
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