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