QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgsapplication.h"
19#include "qgsauthconfig.h"
20#include "qgsauthmanager.h"
21
22#include <QDateTime>
23#include <QString>
24
25#include "moc_qgsauthsettingswidget.cpp"
26
27using namespace Qt::StringLiterals;
28
29QgsAuthSettingsWidget::QgsAuthSettingsWidget( QWidget *parent, const QString &configId, const QString &username, const QString &password, const QString &dataprovider )
30 : QWidget( parent )
31 , mDataprovider( dataprovider )
32{
33 setupUi( this );
34 txtPassword->setText( password );
35 txtUserName->setText( username );
36 if ( !dataprovider.isEmpty() )
37 {
38 mAuthConfigSelect->setDataProviderKey( dataprovider );
39 }
40 if ( !configId.isEmpty() )
41 {
42 mAuthConfigSelect->setConfigId( configId );
43 }
44 setBasicText( "" );
45 // default to warning about basic settings stored in project file
47 connect( btnConvertToEncrypted, &QPushButton::clicked, this, &QgsAuthSettingsWidget::convertToEncrypted );
48 connect( txtUserName, &QLineEdit::textChanged, this, &QgsAuthSettingsWidget::userNameTextChanged );
49 connect( txtPassword, &QLineEdit::textChanged, this, &QgsAuthSettingsWidget::passwordTextChanged );
51
52 // Hide store password and username by default
53 showStoreCheckboxes( false );
54 updateSelectedTab();
55 updateConvertBtnState();
56}
57
59{
60 tabAuth->removeTab( tabAuth->indexOf( tabBasic ) );
61 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
62}
63
64void QgsAuthSettingsWidget::setWarningText( const QString &warningText )
65{
66 lblWarning->setText( warningText );
67}
68
69void QgsAuthSettingsWidget::setBasicText( const QString &basicText )
70{
71 lblBasic->setText( basicText );
72 // hide unused widget so its word wrapping does not add to parent widget's height
73 lblBasic->setVisible( !basicText.isEmpty() );
74}
75
77{
78 return txtUserName->text();
79}
80
82{
83 txtUserName->setText( username );
84 updateSelectedTab();
85}
86
88{
89 return txtPassword->text();
90}
91
93{
94 txtPassword->setText( password );
95 updateSelectedTab();
96}
97
99{
100 mAuthConfigSelect->setConfigId( configId );
101 updateSelectedTab();
102}
103
105{
106 mDataprovider = dataprovider;
107 mAuthConfigSelect->setDataProviderKey( dataprovider );
108}
109
111{
112 return mDataprovider;
113}
114
116{
117 const QString out = tr( "Warning: credentials stored as plain text in %1." );
118 switch ( warning )
119 {
120 case ProjectFile:
121 return out.arg( tr( "project file" ) );
122 case UserSettings:
123 return out.arg( tr( "user settings" ) );
124 }
125 return QString(); // no build warnings
126}
127
129{
130 return mAuthConfigSelect->configId();
131}
132
134{
135 return btnConvertToEncrypted->isEnabled();
136}
137
139{
140 if ( enabled )
141 {
142 cbStorePassword->show();
143 cbStoreUsername->show();
144 }
145 else
146 {
147 cbStorePassword->hide();
148 cbStoreUsername->hide();
149 }
150}
151
153{
154 cbStoreUsername->setChecked( checked );
155}
156
158{
159 cbStorePassword->setChecked( checked );
160}
161
163{
164 return cbStorePassword->isChecked();
165}
166
168{
169 return cbStoreUsername->isChecked();
170}
171
173{
174 return tabAuth->currentIndex() == tabAuth->indexOf( tabConfigurations );
175}
176
178{
179 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
180 QgsAuthMethodConfig config( u"Basic"_s );
181 config.setName( tr( "Converted config %1" ).arg( QDateTime::currentDateTime().toString() ) );
182 config.setConfig( u"username"_s, txtUserName->text() );
183 config.setConfig( u"password"_s, txtPassword->text() );
184 if ( !QgsApplication::authManager()->storeAuthenticationConfig( config ) )
185 {
186 mAuthConfigSelect->showMessage( tr( "Couldn't create a Basic authentication configuration!" ) );
187 return false;
188 }
189 else
190 {
191 txtUserName->setText( QString() );
192 txtPassword->setText( QString() );
193 mAuthConfigSelect->setConfigId( config.id() );
194 return true;
195 }
196}
197
198void QgsAuthSettingsWidget::userNameTextChanged( const QString &text )
199{
200 Q_UNUSED( text )
201 updateConvertBtnState();
202 emit usernameChanged();
203}
204
205void QgsAuthSettingsWidget::passwordTextChanged( const QString &text )
206{
207 Q_UNUSED( text )
208 updateConvertBtnState();
209 emit passwordChanged();
210}
211
212void QgsAuthSettingsWidget::updateConvertBtnState()
213{
214 btnConvertToEncrypted->setEnabled( !txtUserName->text().isEmpty() || !txtPassword->text().isEmpty() );
215}
216
217void QgsAuthSettingsWidget::updateSelectedTab()
218{
219 if ( !mAuthConfigSelect->configId().isEmpty() )
220 {
221 tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
222 }
223 else if ( !( txtUserName->text().isEmpty() && txtPassword->text().isEmpty() ) )
224 {
225 tabAuth->setCurrentIndex( tabAuth->indexOf( tabBasic ) );
226 }
227}
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