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