QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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  ***************************************************************************/
16 #include "qgsauthsettingswidget.h"
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 );
48  // Hide store password and username by default
49  showStoreCheckboxes( false );
50  updateSelectedTab();
51  updateConvertBtnState();
52 }
53 
54 void QgsAuthSettingsWidget::setWarningText( const QString &warningText )
55 {
56  lblWarning->setText( warningText );
57 }
58 
59 void QgsAuthSettingsWidget::setBasicText( const QString &basicText )
60 {
61  lblBasic->setText( basicText );
62  // hide unused widget so its word wrapping does not add to parent widget's height
63  lblBasic->setVisible( ! basicText.isEmpty() );
64 }
65 
66 const QString QgsAuthSettingsWidget::username() const
67 {
68  return txtUserName->text();
69 }
70 
71 void QgsAuthSettingsWidget::setUsername( const QString &username )
72 {
73  txtUserName->setText( username );
74  updateSelectedTab();
75 }
76 
77 const QString QgsAuthSettingsWidget::password() const
78 {
79  return txtPassword->text();
80 }
81 
82 void QgsAuthSettingsWidget::setPassword( const QString &password )
83 {
84  txtPassword->setText( password );
85  updateSelectedTab();
86 }
87 
88 void QgsAuthSettingsWidget::setConfigId( const QString &configId )
89 {
90  mAuthConfigSelect->setConfigId( configId );
91  updateSelectedTab();
92 }
93 
94 void QgsAuthSettingsWidget::setDataprovider( const QString &dataprovider )
95 {
96  mDataprovider = dataprovider;
97  mAuthConfigSelect->setDataProviderKey( dataprovider );
98 }
99 
101 {
102  return mDataprovider;
103 }
104 
106 {
107  QString out = tr( "Warning: credentials stored as plain text in %1." );
108  switch ( warning )
109  {
110  case ProjectFile:
111  return out.arg( tr( "project file" ) );
112  case UserSettings:
113  return out.arg( tr( "user settings" ) );
114  }
115  return QString(); // no build warnings
116 }
117 
118 const QString QgsAuthSettingsWidget::configId() const
119 {
120  return mAuthConfigSelect->configId();
121 }
122 
124 {
125  return btnConvertToEncrypted->isEnabled( );
126 }
127 
129 {
130  if ( enabled )
131  {
132  cbStorePassword->show();
133  cbStoreUsername->show();
134  }
135  else
136  {
137  cbStorePassword->hide();
138  cbStoreUsername->hide();
139  }
140 }
141 
143 {
144  cbStoreUsername->setChecked( checked );
145 }
146 
148 {
149  cbStorePassword->setChecked( checked );
150 }
151 
153 {
154  return cbStorePassword->isChecked( );
155 }
156 
158 {
159  return cbStoreUsername->isChecked( );
160 }
161 
163 {
164  return tabAuth->currentIndex( ) == tabAuth->indexOf( tabConfigurations );
165 }
166 
168 {
169  tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
170  QgsAuthMethodConfig config( QStringLiteral( "Basic" ) );
171  config.setName( tr( "Converted config %1" ).arg( QDateTime::currentDateTime().toString( ) ) );
172  config.setConfig( QStringLiteral( "username" ), txtUserName->text() );
173  config.setConfig( QStringLiteral( "password" ), txtPassword->text() );
174  if ( ! QgsApplication::authManager()->storeAuthenticationConfig( config ) )
175  {
176  mAuthConfigSelect->showMessage( tr( "Couldn't create a Basic authentication configuration!" ) );
177  return false;
178  }
179  else
180  {
181  txtUserName->setText( QString( ) );
182  txtPassword->setText( QString( ) );
183  mAuthConfigSelect->setConfigId( config.id( ) );
184  return true;
185  }
186 }
187 
189 {
190  Q_UNUSED( text )
191  updateConvertBtnState();
192 }
193 
195 {
196  Q_UNUSED( text )
197  updateConvertBtnState();
198 }
199 
200 void QgsAuthSettingsWidget::updateConvertBtnState()
201 {
202  btnConvertToEncrypted->setEnabled( ! txtUserName->text().isEmpty() || ! txtPassword->text().isEmpty() );
203 }
204 
205 void QgsAuthSettingsWidget::updateSelectedTab()
206 {
207  if ( ! mAuthConfigSelect->configId().isEmpty( ) )
208  {
209  tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
210  }
211  else if ( !( txtUserName->text( ).isEmpty() && txtPassword->text( ).isEmpty( ) ) )
212  {
213  tabAuth->setCurrentIndex( tabAuth->indexOf( tabBasic ) );
214  }
215 }
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Configuration storage class for authentication method configurations.
Definition: qgsauthconfig.h:39
void setName(const QString &name)
Sets name of configuration.
Definition: qgsauthconfig.h:66
const QString id() const
Gets 'authcfg' 7-character alphanumeric ID of the config.
Definition: qgsauthconfig.h:59
void setConfig(const QString &key, const QString &value)
Set a single config value per key in the map.
const QString configId() const
configId
bool storeUsernameIsChecked() const
storeUsername
static const QString formattedWarning(WarningType warning)
warning text message based upon where credentials are stored
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)
setStorePasswordCheched 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...
void passwordTextChanged(const QString &text)
Called when password text is changed.
void setBasicText(const QString &basicText)
setBasicText set the text of the warning label
void userNameTextChanged(const QString &text)
Called when user name text is changed.
const QString username() const
username
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 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
const QString dataprovider() const
dataprovider
const QString password() const
password