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