QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
qgsauthconfig.h
qgsauthsettingswidget.h
QgsAuthSettingsWidget::setBasicText
void setBasicText(const QString &basicText)
setBasicText set the text of the warning label
Definition: qgsauthsettingswidget.cpp:59
QgsAuthSettingsWidget::setStoreUsernameChecked
void setStoreUsernameChecked(bool checked)
setStoreUsernameChecked check the "Store" checkbox for the username
Definition: qgsauthsettingswidget.cpp:142
QgsAuthSettingsWidget::convertToEncrypted
bool convertToEncrypted()
convertToEncrypted is called when the convert to encrypted button is clicked and it creates a Basic a...
Definition: qgsauthsettingswidget.cpp:167
qgsauthmanager.h
QgsAuthSettingsWidget::setUsername
void setUsername(const QString &username)
setUsername set the username
Definition: qgsauthsettingswidget.cpp:71
QgsAuthSettingsWidget::QgsAuthSettingsWidget
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,...
Definition: qgsauthsettingswidget.cpp:23
QgsAuthSettingsWidget::setDataprovider
void setDataprovider(const QString &dataprovider)
setDataprovider set the data provider key for filtering compatible authentication configurations
Definition: qgsauthsettingswidget.cpp:94
QgsAuthSettingsWidget::UserSettings
@ UserSettings
Definition: qgsauthsettingswidget.h:81
QgsAuthSettingsWidget::passwordTextChanged
void passwordTextChanged(const QString &text)
Called when password text is changed.
Definition: qgsauthsettingswidget.cpp:194
QgsAuthSettingsWidget::password
const QString password() const
password
Definition: qgsauthsettingswidget.cpp:77
QgsAuthSettingsWidget::setWarningText
void setWarningText(const QString &warningText)
setWarningText set the text of the warning label
Definition: qgsauthsettingswidget.cpp:54
QgsApplication::authManager
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Definition: qgsapplication.cpp:1263
qgsapplication.h
QgsAuthSettingsWidget::userNameTextChanged
void userNameTextChanged(const QString &text)
Called when user name text is changed.
Definition: qgsauthsettingswidget.cpp:188
QgsAuthSettingsWidget::setConfigId
void setConfigId(const QString &configId)
setConfigId set the authentication configuration id param configId the authentication configuration i...
Definition: qgsauthsettingswidget.cpp:88
QgsAuthSettingsWidget::showStoreCheckboxes
void showStoreCheckboxes(bool enabled)
showStoreCheckboxes show the "Store" checkboxes for basic auth.
Definition: qgsauthsettingswidget.cpp:128
QgsAuthSettingsWidget::storePasswordIsChecked
bool storePasswordIsChecked() const
storePassword
Definition: qgsauthsettingswidget.cpp:152
QgsAuthSettingsWidget::btnConvertToEncryptedIsEnabled
bool btnConvertToEncryptedIsEnabled() const
convertButtonEnabled, mainly useful for unit tests
Definition: qgsauthsettingswidget.cpp:123
QgsAuthSettingsWidget::configId
const QString configId() const
configId
Definition: qgsauthsettingswidget.cpp:118
QgsAuthSettingsWidget::username
const QString username() const
username
Definition: qgsauthsettingswidget.cpp:66
QgsAuthSettingsWidget::configurationTabIsSelected
bool configurationTabIsSelected()
configurationTabIsSelected
Definition: qgsauthsettingswidget.cpp:162
QgsAuthSettingsWidget::WarningType
WarningType
The WarningType enum is used to determine the text of the message shown to the user about the destina...
Definition: qgsauthsettingswidget.h:64
QgsAuthSettingsWidget::formattedWarning
static const QString formattedWarning(WarningType warning)
warning text message based upon where credentials are stored
Definition: qgsauthsettingswidget.cpp:105
QgsAuthMethodConfig::id
const QString id() const
Gets 'authcfg' 7-character alphanumeric ID of the config.
Definition: qgsauthconfig.h:73
QgsAuthSettingsWidget::dataprovider
const QString dataprovider() const
dataprovider
Definition: qgsauthsettingswidget.cpp:100
QgsAuthSettingsWidget::storeUsernameIsChecked
bool storeUsernameIsChecked() const
storeUsername
Definition: qgsauthsettingswidget.cpp:157
QgsAuthSettingsWidget::ProjectFile
@ ProjectFile
Definition: qgsauthsettingswidget.h:80
QgsAuthMethodConfig::setConfig
void setConfig(const QString &key, const QString &value)
Set a single config value per key in the map.
Definition: qgsauthconfig.cpp:113
QgsAuthMethodConfig::setName
void setName(const QString &name)
Sets name of configuration.
Definition: qgsauthconfig.h:80
QgsAuthSettingsWidget::setStorePasswordChecked
void setStorePasswordChecked(bool checked)
setStorePasswordCheched check the "Store" checkbox for the password
Definition: qgsauthsettingswidget.cpp:147
QgsAuthMethodConfig
Configuration storage class for authentication method configurations.
Definition: qgsauthconfig.h:38
QgsAuthSettingsWidget::setPassword
void setPassword(const QString &password)
setPassword set the password
Definition: qgsauthsettingswidget.cpp:82