QGIS API Documentation 3.41.0-Master (57ec4277f5e)
Loading...
Searching...
No Matches
qgsauthsettingswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthsettingswidget.cpp - QgsAuthSettingsWidget
3
4 ---------------------
5 begin : 28.9.2017
6 copyright : (C) 2017 by Alessandro Pasotti
7 email : apasotti at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17#include "moc_qgsauthsettingswidget.cpp"
18#include "qgsauthmanager.h"
19#include "qgsauthconfig.h"
20#include "qgsapplication.h"
21
22#include <QDateTime>
23
24QgsAuthSettingsWidget::QgsAuthSettingsWidget( QWidget *parent, const QString &configId, const QString &username, const QString &password, const QString &dataprovider )
25 : QWidget( parent )
26 , mDataprovider( dataprovider )
27{
28 setupUi( this );
29 txtPassword->setText( password );
30 txtUserName->setText( username );
31 if ( !dataprovider.isEmpty() )
32 {
33 mAuthConfigSelect->setDataProviderKey( dataprovider );
34 }
35 if ( !configId.isEmpty() )
36 {
37 mAuthConfigSelect->setConfigId( configId );
38 }
39 setBasicText( "" );
40 // default to warning about basic settings stored in project file
42 connect( btnConvertToEncrypted, &QPushButton::clicked, this, &QgsAuthSettingsWidget::convertToEncrypted );
43 connect( txtUserName, &QLineEdit::textChanged, this, &QgsAuthSettingsWidget::userNameTextChanged );
44 connect( txtPassword, &QLineEdit::textChanged, this, &QgsAuthSettingsWidget::passwordTextChanged );
46
47 // Hide store password and username by default
48 showStoreCheckboxes( false );
49 updateSelectedTab();
50 updateConvertBtnState();
51}
52
53void QgsAuthSettingsWidget::setWarningText( const QString &warningText )
54{
55 lblWarning->setText( warningText );
56}
57
58void QgsAuthSettingsWidget::setBasicText( const QString &basicText )
59{
60 lblBasic->setText( basicText );
61 // hide unused widget so its word wrapping does not add to parent widget's height
62 lblBasic->setVisible( !basicText.isEmpty() );
63}
64
66{
67 return txtUserName->text();
68}
69
70void QgsAuthSettingsWidget::setUsername( const QString &username )
71{
72 txtUserName->setText( username );
73 updateSelectedTab();
74}
75
77{
78 return txtPassword->text();
79}
80
81void QgsAuthSettingsWidget::setPassword( const QString &password )
82{
83 txtPassword->setText( password );
84 updateSelectedTab();
85}
86
87void QgsAuthSettingsWidget::setConfigId( const QString &configId )
88{
89 mAuthConfigSelect->setConfigId( configId );
90 updateSelectedTab();
91}
92
93void QgsAuthSettingsWidget::setDataprovider( const QString &dataprovider )
94{
95 mDataprovider = dataprovider;
96 mAuthConfigSelect->setDataProviderKey( dataprovider );
97}
98
100{
101 return mDataprovider;
102}
103
105{
106 const QString out = tr( "Warning: credentials stored as plain text in %1." );
107 switch ( warning )
108 {
109 case ProjectFile:
110 return out.arg( tr( "project file" ) );
111 case UserSettings:
112 return out.arg( tr( "user settings" ) );
113 }
114 return QString(); // no build warnings
115}
116
118{
119 return mAuthConfigSelect->configId();
120}
121
123{
124 return btnConvertToEncrypted->isEnabled();
125}
126
128{
129 if ( enabled )
130 {
131 cbStorePassword->show();
132 cbStoreUsername->show();
133 }
134 else
135 {
136 cbStorePassword->hide();
137 cbStoreUsername->hide();
138 }
139}
140
142{
143 cbStoreUsername->setChecked( checked );
144}
145
147{
148 cbStorePassword->setChecked( checked );
149}
150
152{
153 return cbStorePassword->isChecked();
154}
155
157{
158 return cbStoreUsername->isChecked();
159}
160
162{
163 return tabAuth->currentIndex() == tabAuth->indexOf( tabConfigurations );
164}
165
167{
168 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
169 QgsAuthMethodConfig config( QStringLiteral( "Basic" ) );
170 config.setName( tr( "Converted config %1" ).arg( QDateTime::currentDateTime().toString() ) );
171 config.setConfig( QStringLiteral( "username" ), txtUserName->text() );
172 config.setConfig( QStringLiteral( "password" ), txtPassword->text() );
173 if ( !QgsApplication::authManager()->storeAuthenticationConfig( config ) )
174 {
175 mAuthConfigSelect->showMessage( tr( "Couldn't create a Basic authentication configuration!" ) );
176 return false;
177 }
178 else
179 {
180 txtUserName->setText( QString() );
181 txtPassword->setText( QString() );
182 mAuthConfigSelect->setConfigId( config.id() );
183 return true;
184 }
185}
186
187void QgsAuthSettingsWidget::userNameTextChanged( const QString &text )
188{
189 Q_UNUSED( text )
190 updateConvertBtnState();
191 emit usernameChanged();
192}
193
194void QgsAuthSettingsWidget::passwordTextChanged( const QString &text )
195{
196 Q_UNUSED( text )
197 updateConvertBtnState();
198 emit passwordChanged();
199}
200
201void QgsAuthSettingsWidget::updateConvertBtnState()
202{
203 btnConvertToEncrypted->setEnabled( !txtUserName->text().isEmpty() || !txtPassword->text().isEmpty() );
204}
205
206void QgsAuthSettingsWidget::updateSelectedTab()
207{
208 if ( !mAuthConfigSelect->configId().isEmpty() )
209 {
210 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
211 }
212 else if ( !( txtUserName->text().isEmpty() && txtPassword->text().isEmpty() ) )
213 {
214 tabAuth->setCurrentIndex( tabAuth->indexOf( tabBasic ) );
215 }
216}
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void selectedConfigIdChanged(const QString &authcfg)
Emitted when authentication config is changed or missing.
Configuration storage class for authentication method configurations.
void setName(const QString &name)
Sets name of configuration.
const QString id() const
Gets 'authcfg' 7-character alphanumeric ID of the config.
void setConfig(const QString &key, const QString &value)
Set a single config value per key in the map.
bool storeUsernameIsChecked() const
storeUsername
static QString formattedWarning(WarningType warning)
warning text message based upon where credentials are stored
void usernameChanged()
Emitted when the plain text username defined in the dialog is changed.
void passwordChanged()
Emitted when the plain text password defined in the dialog is changed.
QString dataprovider() const
dataprovider
void setStoreUsernameChecked(bool checked)
setStoreUsernameChecked check the "Store" checkbox for the username
QgsAuthSettingsWidget(QWidget *parent=nullptr, const QString &configId=QString(), const QString &username=QString(), const QString &password=QString(), const QString &dataprovider=QString())
Create a dialog for setting an associated authentication config, either from existing configs,...
void setDataprovider(const QString &dataprovider)
setDataprovider set the data provider key for filtering compatible authentication configurations
void setStorePasswordChecked(bool checked)
setStorePasswordChecked check the "Store" checkbox for the password
bool configurationTabIsSelected()
configurationTabIsSelected
bool convertToEncrypted()
convertToEncrypted is called when the convert to encrypted button is clicked and it creates a Basic a...
QString username() const
username
void setBasicText(const QString &basicText)
setBasicText set the text of the warning label
QString configId() const
configId
WarningType
The WarningType enum is used to determine the text of the message shown to the user about the destina...
void setWarningText(const QString &warningText)
setWarningText set the text of the warning label
void setPassword(const QString &password)
setPassword set the password
bool storePasswordIsChecked() const
storePassword
void showStoreCheckboxes(bool enabled)
showStoreCheckboxes show the "Store" checkboxes for basic auth.
void configIdChanged()
Emitted when the auth configuration ID selected in the dialog is changed.
void setUsername(const QString &username)
setUsername set the username
void setConfigId(const QString &configId)
setConfigId set the authentication configuration id param configId the authentication configuration i...
bool btnConvertToEncryptedIsEnabled() const
convertButtonEnabled, mainly useful for unit tests
QString password() const
password