25 #include <QPushButton> 28 static QString invalidStyle_(
const QString &selector = QStringLiteral(
"QLineEdit" ) )
30 return QStringLiteral(
"%1{color: rgb(200, 0, 0);}" ).arg( selector );
34 : QDialog( parent, fl )
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 );
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();
58 if ( qApp->thread() != QThread::currentThread() )
62 QgsDebugMsg( QString(
"signal returned %1 (username=%2, password=%3)" ).arg( ok ?
"true" :
"false", username, password ) );
66 requestCredentials( realm, &username, &password, message, &ok );
71 void QgsCredentialDialog::requestCredentials(
const QString &realm, QString *username, QString *password,
const QString &message,
bool *ok )
73 Q_ASSERT( qApp->thread() == thread() && thread() == QThread::currentThread() );
75 stackedWidget->setCurrentIndex( 0 );
78 labelRealm->setText( realm );
79 leUsername->setText( *username );
80 lePassword->setText( *password );
81 labelMessage->setText( message );
82 labelMessage->setHidden( message.isEmpty() );
84 if ( !leUsername->text().isEmpty() )
85 lePassword->setFocus();
87 QWidget *activeWindow = qApp->activeWindow();
89 QApplication::setOverrideCursor( Qt::ArrowCursor );
92 *ok = exec() == QDialog::Accepted;
93 QgsDebugMsg( QString(
"exec(): %1" ).arg( *ok ?
"true" :
"false" ) );
95 QApplication::restoreOverrideCursor();
98 activeWindow->raise();
102 *username = leUsername->text();
103 *password = lePassword->text();
110 if ( qApp->thread() != QThread::currentThread() )
117 requestCredentialsMasterPassword( &password, stored, &ok );
122 void QgsCredentialDialog::requestCredentialsMasterPassword( QString *password,
bool stored,
bool *ok )
125 stackedWidget->setCurrentIndex( 1 );
126 leMasterPass->setFocus();
128 QString titletxt( stored ? tr(
"Enter CURRENT master authentication password" ) : tr(
"Set NEW master authentication password" ) );
129 lblPasswordTitle->setText( titletxt );
133 leMasterPassVerify->setVisible( !stored );
134 lblDontForget->setVisible( !stored );
136 QApplication::setOverrideCursor( Qt::ArrowCursor );
138 grpbxPassAttempts->setVisible(
false );
142 mOkButton->setEnabled(
false );
144 if ( passfailed >= 3 )
146 lblSavedForSession->setVisible(
false );
147 grpbxPassAttempts->setTitle( tr(
"Password attempts: %1" ).arg( passfailed ) );
148 grpbxPassAttempts->setVisible(
true );
152 QSize s = sizeHint();
153 s.setWidth( width() );
157 *ok = exec() == QDialog::Accepted;
158 QgsDebugMsg( QString(
"exec(): %1" ).arg( *ok ?
"true" :
"false" ) );
162 bool passok = !leMasterPass->text().isEmpty();
163 if ( passok && stored && !chkbxEraseAuthDb->isChecked() )
168 if ( passok && !stored )
170 passok = ( leMasterPass->text() == leMasterPassVerify->text() );
173 if ( passok || chkbxEraseAuthDb->isChecked() )
175 if ( stored && chkbxEraseAuthDb->isChecked() )
181 *password = leMasterPass->text();
195 leMasterPass->setStyleSheet( invalidStyle_() );
196 if ( leMasterPassVerify->isVisible() )
198 leMasterPassVerify->setStyleSheet( invalidStyle_() );
207 if ( passfailed >= 5 )
214 leMasterPass->clear();
215 leMasterPassVerify->clear();
217 chkbxEraseAuthDb->setChecked(
false );
218 lblSavedForSession->setVisible(
true );
222 mOkButton->setEnabled(
true );
224 QApplication::restoreOverrideCursor();
226 if ( passfailed >= 5 )
232 void QgsCredentialDialog::leMasterPass_textChanged(
const QString &pass )
234 leMasterPass->setStyleSheet( QLatin1String(
"" ) );
235 bool passok = !pass.isEmpty();
236 if ( leMasterPassVerify->isVisible() )
238 leMasterPassVerify->setStyleSheet( QLatin1String(
"" ) );
239 passok = passok && ( leMasterPass->text() == leMasterPassVerify->text() );
241 mOkButton->setEnabled( passok );
243 if ( leMasterPassVerify->isVisible() && !passok )
245 leMasterPass->setStyleSheet( invalidStyle_() );
246 leMasterPassVerify->setStyleSheet( invalidStyle_() );
250 void QgsCredentialDialog::leMasterPassVerify_textChanged(
const QString &pass )
252 if ( leMasterPassVerify->isVisible() )
254 leMasterPass->setStyleSheet( QLatin1String(
"" ) );
255 leMasterPassVerify->setStyleSheet( QLatin1String(
"" ) );
258 bool passok = !pass.isEmpty() && ( leMasterPass->text() == leMasterPassVerify->text() );
259 mOkButton->setEnabled( passok );
262 leMasterPass->setStyleSheet( invalidStyle_() );
263 leMasterPassVerify->setStyleSheet( invalidStyle_() );
268 void QgsCredentialDialog::chkbxEraseAuthDb_toggled(
bool checked )
271 mOkButton->setEnabled(
true );
bool passwordHelperEnabled() const
Password helper enabled getter.
void setScheduledAuthDatabaseErase(bool scheduleErase)
Schedule an optional erase of authentication database, starting when mutex is lockable.
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'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.