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( QStringLiteral(
"signal returned %1 (username=%2)" ).arg( ok ?
"true" :
"false", username ) );
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 leUsername->setFocus();
87 lePassword->setFocus();
89 QWidget *activeWindow = qApp->activeWindow();
91 QApplication::setOverrideCursor( Qt::ArrowCursor );
94 *ok = exec() == QDialog::Accepted;
95 QgsDebugMsg( QStringLiteral(
"exec(): %1" ).arg( *ok ?
"true" :
"false" ) );
97 QApplication::restoreOverrideCursor();
100 activeWindow->raise();
104 *username = leUsername->text();
105 *password = lePassword->text();
112 if ( qApp->thread() != QThread::currentThread() )
114 QgsDebugMsg( QStringLiteral(
"emitting signal" ) );
119 requestCredentialsMasterPassword( &password, stored, &ok );
124 void QgsCredentialDialog::requestCredentialsMasterPassword( QString *password,
bool stored,
bool *ok )
127 stackedWidget->setCurrentIndex( 1 );
128 leMasterPass->setFocus();
130 QString titletxt( stored ? tr(
"Enter CURRENT master authentication password" ) : tr(
"Set NEW master authentication password" ) );
131 lblPasswordTitle->setText( titletxt );
135 leMasterPassVerify->setVisible( !stored );
136 lblDontForget->setVisible( !stored );
138 QApplication::setOverrideCursor( Qt::ArrowCursor );
140 grpbxPassAttempts->setVisible(
false );
144 mOkButton->setEnabled(
false );
146 if ( passfailed >= 3 )
148 lblSavedForSession->setVisible(
false );
149 grpbxPassAttempts->setTitle( tr(
"Password attempts: %1" ).arg( passfailed ) );
150 grpbxPassAttempts->setVisible(
true );
154 QSize s = sizeHint();
155 s.setWidth( width() );
159 *ok = exec() == QDialog::Accepted;
160 QgsDebugMsg( QStringLiteral(
"exec(): %1" ).arg( *ok ?
"true" :
"false" ) );
164 bool passok = !leMasterPass->text().isEmpty();
165 if ( passok && stored && !chkbxEraseAuthDb->isChecked() )
170 if ( passok && !stored )
172 passok = ( leMasterPass->text() == leMasterPassVerify->text() );
175 if ( passok || chkbxEraseAuthDb->isChecked() )
177 if ( stored && chkbxEraseAuthDb->isChecked() )
183 *password = leMasterPass->text();
197 leMasterPass->setStyleSheet( invalidStyle_() );
198 if ( leMasterPassVerify->isVisible() )
200 leMasterPassVerify->setStyleSheet( invalidStyle_() );
209 if ( passfailed >= 5 )
216 leMasterPass->clear();
217 leMasterPassVerify->clear();
219 chkbxEraseAuthDb->setChecked(
false );
220 lblSavedForSession->setVisible(
true );
224 mOkButton->setEnabled(
true );
226 QApplication::restoreOverrideCursor();
228 if ( passfailed >= 5 )
234 void QgsCredentialDialog::leMasterPass_textChanged(
const QString &pass )
236 leMasterPass->setStyleSheet( QString() );
237 bool passok = !pass.isEmpty();
238 if ( leMasterPassVerify->isVisible() )
240 leMasterPassVerify->setStyleSheet( QString() );
241 passok = passok && ( leMasterPass->text() == leMasterPassVerify->text() );
243 mOkButton->setEnabled( passok );
245 if ( leMasterPassVerify->isVisible() && !passok )
247 leMasterPass->setStyleSheet( invalidStyle_() );
248 leMasterPassVerify->setStyleSheet( invalidStyle_() );
252 void QgsCredentialDialog::leMasterPassVerify_textChanged(
const QString &pass )
254 if ( leMasterPassVerify->isVisible() )
256 leMasterPass->setStyleSheet( QString() );
257 leMasterPassVerify->setStyleSheet( QString() );
260 bool passok = !pass.isEmpty() && ( leMasterPass->text() == leMasterPassVerify->text() );
261 mOkButton->setEnabled( passok );
264 leMasterPass->setStyleSheet( invalidStyle_() );
265 leMasterPassVerify->setStyleSheet( invalidStyle_() );
270 void QgsCredentialDialog::chkbxEraseAuthDb_toggled(
bool checked )
273 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 *)
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
void setPasswordHelperEnabled(bool enabled)
Password helper enabled setter.
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.