Skip to content
Permalink
Browse files
Added ability to add additional headers to the API Header authenticat…
…ion method
  • Loading branch information
mo-tomcummins-roweit authored and nyalldawson committed Oct 22, 2021
1 parent ad842be commit 033bcd1c10759a53cc37833c6d6baa6857a1ac39
@@ -19,6 +19,7 @@
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgsmessagelog.h"

#ifdef HAVE_GUI
#include "qgsauthapiheaderedit.h"
@@ -72,13 +73,28 @@ bool QgsAuthApiHeaderMethod::updateNetworkRequest( QNetworkRequest &request, con
return false;
}

const QString headerKey = mconfig.config( QStringLiteral( "headerKey" ) );
const QString headerValue = mconfig.config( QStringLiteral( "headerValue" ) );

if ( !headerKey.isEmpty() && !headerValue.isEmpty() )
QMapIterator<QString, QString> i( mconfig.configMap() );
while ( i.hasNext() )
{
request.setRawHeader( QStringLiteral( "%1" ).arg( headerKey ).toLocal8Bit(), QStringLiteral( "%1" ).arg( headerValue ).toLocal8Bit() );
i.next();

const QString headerKey = i.key();
const QString headerValue = i.value();

QgsDebugMsg( QStringLiteral( "HTTP Header: %1=%2" ).arg( headerKey ).arg( headerValue ) );

if ( !headerKey.isEmpty() )
{
request.setRawHeader( QStringLiteral( "%1" ).arg( headerKey ).toLocal8Bit(),
QStringLiteral( "%1" ).arg( headerValue ).toLocal8Bit() );
}
else
{
const QString msg = "The header key was empty, we shouldn't have empty header keys at this point";
QgsMessageLog::logMessage( msg, AUTH_METHOD_KEY, Qgis::MessageLevel::Warning );
}
}

return true;
}

@@ -17,17 +17,24 @@
#include "qgsauthapiheaderedit.h"
#include "ui_qgsauthapiheaderedit.h"

#include "qgsapplication.h"

QgsAuthApiHeaderEdit::QgsAuthApiHeaderEdit( QWidget *parent )
: QgsAuthMethodEdit( parent )
{
setupUi( this );
connect( headerKeyEdit, &QLineEdit::textChanged, this, &QgsAuthApiHeaderEdit::textChanged );

connect( tblwdgHeaderPairs, &QTableWidget::itemSelectionChanged, this, &QgsAuthApiHeaderEdit::headerTableSelectionChanged );
connect( tblwdgHeaderPairs, &QTableWidget::cellChanged, this, &QgsAuthApiHeaderEdit::headerTableCellChanged );

connect( btnAddHeaderPair, &QToolButton::clicked, this, &QgsAuthApiHeaderEdit::addHeaderPair );
connect( btnRemoveHeaderPair, &QToolButton::clicked, this, &QgsAuthApiHeaderEdit::removeHeaderPair );
}


bool QgsAuthApiHeaderEdit::validateConfig()
{
const bool curvalid = !headerKeyEdit->text().isEmpty();
const bool curvalid = !emptyHeadersKeysPresent() && tblwdgHeaderPairs->rowCount() != 0;
if ( mValid != curvalid )
{
mValid = curvalid;
@@ -36,39 +43,144 @@ bool QgsAuthApiHeaderEdit::validateConfig()
return curvalid;
}


QgsStringMap QgsAuthApiHeaderEdit::configMap() const
{
QgsStringMap config;
config.insert( QStringLiteral( "headerKey" ), headerKeyEdit->text() );
config.insert( QStringLiteral( "headerValue" ), headerValueEdit->toPlainText() );

return config;
return headerPairs();
}


void QgsAuthApiHeaderEdit::loadConfig( const QgsStringMap &configmap )
{
clearConfig();

mConfigMap = configmap;
headerKeyEdit->setText( configmap.value( QStringLiteral( "headerKey" ) ) );
headerValueEdit->setPlainText( configmap.value( QStringLiteral( "headerValue" ) ) );

populateHeaderPairs( configmap );

validateConfig();
}


void QgsAuthApiHeaderEdit::resetConfig()
{
loadConfig( mConfigMap );
}


void QgsAuthApiHeaderEdit::clearConfig()
{
headerKeyEdit->clear();
headerValueEdit->clear();
clearHeaderPairs();
}


void QgsAuthApiHeaderEdit::addHeaderPair()
{
addHeaderPairRow( QString(), QString() );
tblwdgHeaderPairs->setFocus();
tblwdgHeaderPairs->setCurrentCell( tblwdgHeaderPairs->rowCount() - 1, 0 );
tblwdgHeaderPairs->edit( tblwdgHeaderPairs->currentIndex() );
}


void QgsAuthApiHeaderEdit::removeHeaderPair()
{
tblwdgHeaderPairs->removeRow( tblwdgHeaderPairs->currentRow() );
validateConfig();
}


void QgsAuthApiHeaderEdit::clearHeaderPairs()
{
for ( int i = tblwdgHeaderPairs->rowCount(); i > 0 ; --i )
{
tblwdgHeaderPairs->removeRow( i - 1 );
}
validateConfig();
}


void QgsAuthApiHeaderEdit::populateHeaderPairs( const QgsStringMap &headerpairs, bool append )
{
if ( !append )
{
clearHeaderPairs();
}

QgsStringMap::const_iterator i = headerpairs.constBegin();
while ( i != headerpairs.constEnd() )
{
addHeaderPairRow( i.key(), i.value() );
++i;
}
}


void QgsAuthApiHeaderEdit::headerTableSelectionChanged()
{
const bool hasPair = tblwdgHeaderPairs->selectedItems().count() > 0;
btnRemoveHeaderPair->setEnabled( hasPair );
validateConfig();
}


void QgsAuthApiHeaderEdit::headerTableCellChanged( const int row, const int column )
{
QgsDebugMsg( QStringLiteral( "Cell updated: row %1 column %2" ).arg( row ).arg( column ) );
validateConfig();
}


bool QgsAuthApiHeaderEdit::emptyHeadersKeysPresent()
{
const int rowCount = tblwdgHeaderPairs->rowCount();
QgsDebugMsg( QStringLiteral( "Validate header table contains valid header keys for %1 rows" ).arg( rowCount ) );

for ( int i = 0; i < rowCount; ++i )
{
if ( tblwdgHeaderPairs->item( i, 0 )->text().isEmpty() )
{
return true;
}
}

return false;
}

void QgsAuthApiHeaderEdit::textChanged( const QString &txt )

void QgsAuthApiHeaderEdit::addHeaderPairRow( const QString &key, const QString &val )
{
Q_UNUSED( txt )
const int rowCnt = tblwdgHeaderPairs->rowCount();
tblwdgHeaderPairs->insertRow( rowCnt );

const Qt::ItemFlags itmFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
| Qt::ItemIsEditable | Qt::ItemIsDropEnabled;

QTableWidgetItem *keyItm = new QTableWidgetItem( key );
keyItm->setFlags( itmFlags );
tblwdgHeaderPairs->setItem( rowCnt, 0, keyItm );

QTableWidgetItem *valItm = new QTableWidgetItem( val );
keyItm->setFlags( itmFlags );
tblwdgHeaderPairs->setItem( rowCnt, 1, valItm );

validateConfig();
}


QgsStringMap QgsAuthApiHeaderEdit::headerPairs() const
{
QgsStringMap headerPairs;
for ( int i = 0; i < tblwdgHeaderPairs->rowCount(); ++i )
{
if ( tblwdgHeaderPairs->item( i, 0 )->text().isEmpty() )
{
continue;
}

const QString headerKey = tblwdgHeaderPairs->item( i, 0 )->text();
const QString headerValue = tblwdgHeaderPairs->item( i, 1 )->text();
headerPairs.insert( headerKey, headerValue );
}
return headerPairs;
}
@@ -43,11 +43,27 @@ class QgsAuthApiHeaderEdit : public QgsAuthMethodEdit, private Ui::QgsAuthApiHea
void clearConfig() override;

private slots:
void textChanged( const QString &txt );
void addHeaderPair();

void removeHeaderPair();

void clearHeaderPairs();

void populateHeaderPairs( const QgsStringMap &headerpairs, bool append = false );

void headerTableSelectionChanged();

void headerTableCellChanged( const int row, const int column );

private:
QgsStringMap mConfigMap;
bool mValid = false;

bool emptyHeadersKeysPresent();

void addHeaderPairRow( const QString &key, const QString &val );

QgsStringMap headerPairs() const;
};

#endif // QGSAUTHAPIHEADEREDIT_H

0 comments on commit 033bcd1

Please sign in to comment.