QGIS API Documentation 3.43.0-Master (87898417f79)
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
54{
55 tabAuth->removeTab( tabAuth->indexOf( tabBasic ) );
56 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
57}
58
59void QgsAuthSettingsWidget::setWarningText( const QString &warningText )
60{
61 lblWarning->setText( warningText );
62}
63
64void QgsAuthSettingsWidget::setBasicText( const QString &basicText )
65{
66 lblBasic->setText( basicText );
67 // hide unused widget so its word wrapping does not add to parent widget's height
68 lblBasic->setVisible( !basicText.isEmpty() );
69}
70
72{
73 return txtUserName->text();
74}
75
76void QgsAuthSettingsWidget::setUsername( const QString &username )
77{
78 txtUserName->setText( username );
79 updateSelectedTab();
80}
81
83{
84 return txtPassword->text();
85}
86
87void QgsAuthSettingsWidget::setPassword( const QString &password )
88{
89 txtPassword->setText( password );
90 updateSelectedTab();
91}
92
93void QgsAuthSettingsWidget::setConfigId( const QString &configId )
94{
95 mAuthConfigSelect->setConfigId( configId );
96 updateSelectedTab();
97}
98
99void QgsAuthSettingsWidget::setDataprovider( const QString &dataprovider )
100{
101 mDataprovider = dataprovider;
102 mAuthConfigSelect->setDataProviderKey( dataprovider );
103}
104
106{
107 return mDataprovider;
108}
109
111{
112 const QString out = tr( "Warning: credentials stored as plain text in %1." );
113 switch ( warning )
114 {
115 case ProjectFile:
116 return out.arg( tr( "project file" ) );
117 case UserSettings:
118 return out.arg( tr( "user settings" ) );
119 }
120 return QString(); // no build warnings
121}
122
124{
125 return mAuthConfigSelect->configId();
126}
127
129{
130 return btnConvertToEncrypted->isEnabled();
131}
132
134{
135 if ( enabled )
136 {
137 cbStorePassword->show();
138 cbStoreUsername->show();
139 }
140 else
141 {
142 cbStorePassword->hide();
143 cbStoreUsername->hide();
144 }
145}
146
148{
149 cbStoreUsername->setChecked( checked );
150}
151
153{
154 cbStorePassword->setChecked( checked );
155}
156
158{
159 return cbStorePassword->isChecked();
160}
161
163{
164 return cbStoreUsername->isChecked();
165}
166
168{
169 return tabAuth->currentIndex() == tabAuth->indexOf( tabConfigurations );
170}
171
173{
174 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
175 QgsAuthMethodConfig config( QStringLiteral( "Basic" ) );
176 config.setName( tr( "Converted config %1" ).arg( QDateTime::currentDateTime().toString() ) );
177 config.setConfig( QStringLiteral( "username" ), txtUserName->text() );
178 config.setConfig( QStringLiteral( "password" ), txtPassword->text() );
179 if ( !QgsApplication::authManager()->storeAuthenticationConfig( config ) )
180 {
181 mAuthConfigSelect->showMessage( tr( "Couldn't create a Basic authentication configuration!" ) );
182 return false;
183 }
184 else
185 {
186 txtUserName->setText( QString() );
187 txtPassword->setText( QString() );
188 mAuthConfigSelect->setConfigId( config.id() );
189 return true;
190 }
191}
192
193void QgsAuthSettingsWidget::userNameTextChanged( const QString &text )
194{
195 Q_UNUSED( text )
196 updateConvertBtnState();
197 emit usernameChanged();
198}
199
200void QgsAuthSettingsWidget::passwordTextChanged( const QString &text )
201{
202 Q_UNUSED( text )
203 updateConvertBtnState();
204 emit passwordChanged();
205}
206
207void QgsAuthSettingsWidget::updateConvertBtnState()
208{
209 btnConvertToEncrypted->setEnabled( !txtUserName->text().isEmpty() || !txtPassword->text().isEmpty() );
210}
211
212void QgsAuthSettingsWidget::updateSelectedTab()
213{
214 if ( !mAuthConfigSelect->configId().isEmpty() )
215 {
216 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
217 }
218 else if ( !( txtUserName->text().isEmpty() && txtPassword->text().isEmpty() ) )
219 {
220 tabAuth->setCurrentIndex( tabAuth->indexOf( tabBasic ) );
221 }
222}
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.
void removeBasicSettings()
Removes the basic authentication tab from the widget.
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