Skip to content

Commit

Permalink
Fix ArcGIS MapServer provider does not correctly respect authenticati…
Browse files Browse the repository at this point in the history
…on configuration
  • Loading branch information
nyalldawson committed May 30, 2019
1 parent 3f9a07e commit 323fae2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/providers/arcgisrest/qgsamsprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ void QgsAmsLegendFetcher::start()
// http://resources.arcgis.com/en/help/rest/apiref/mslegend.html
// http://sampleserver5.arcgisonline.com/arcgis/rest/services/CommunityAddressing/MapServer/legend?f=pjson
QgsDataSourceUri dataSource( mProvider->dataSourceUri() );
const QString authCfg = dataSource.authConfigId();

QUrl queryUrl( dataSource.param( QStringLiteral( "url" ) ) + "/legend" );
queryUrl.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
mQuery->start( queryUrl, &mQueryReply );
mQuery->start( queryUrl, authCfg, &mQueryReply );
}

void QgsAmsLegendFetcher::handleError( const QString &errorTitle, const QString &errorMsg )
Expand Down Expand Up @@ -261,7 +263,7 @@ void QgsAmsProvider::draw( const QgsRectangle &viewExtent, int pixelWidth, int p
return;
}
QgsDataSourceUri dataSource( dataSourceUri() );
const QString authcfg = dataSource.param( QStringLiteral( "authcfg" ) );
const QString authcfg = dataSource.authConfigId();

// Use of tiles currently only implemented if service CRS is meter based
if ( mServiceInfo[QStringLiteral( "singleFusedMapCache" )].toBool() && mCrs.mapUnits() == QgsUnitTypes::DistanceMeters )
Expand Down Expand Up @@ -322,7 +324,7 @@ void QgsAmsProvider::draw( const QgsRectangle &viewExtent, int pixelWidth, int p
queries[( iy - iyStart ) * ixCount + ( ix - ixStart )] = QUrl( dataSource.param( QStringLiteral( "url" ) ) + QStringLiteral( "/tile/%1/%2/%3" ).arg( level ).arg( iy ).arg( ix ) );
}
}
QgsArcGisAsyncParallelQuery query;
QgsArcGisAsyncParallelQuery query( authcfg );
QEventLoop evLoop;
connect( &query, &QgsArcGisAsyncParallelQuery::finished, &evLoop, &QEventLoop::quit );
query.start( queries, &results, true );
Expand Down Expand Up @@ -404,7 +406,7 @@ QgsRasterIdentifyResult QgsAmsProvider::identify( const QgsPointXY &point, QgsRa
queryUrl.addQueryItem( QStringLiteral( "mapExtent" ), QStringLiteral( "%1,%2,%3,%4" ).arg( extent.xMinimum(), 0, 'f' ).arg( extent.yMinimum(), 0, 'f' ).arg( extent.xMaximum(), 0, 'f' ).arg( extent.yMaximum(), 0, 'f' ) );
queryUrl.addQueryItem( QStringLiteral( "tolerance" ), QStringLiteral( "10" ) );

const QString authcfg = dataSource.param( QStringLiteral( "authcfg" ) );
const QString authcfg = dataSource.authConfigId();
const QVariantList queryResults = QgsArcGisRestUtils::queryServiceJSON( queryUrl, authcfg, mErrorTitle, mError ).value( QStringLiteral( "results" ) ).toList();

QMap<int, QVariant> entries;
Expand Down
23 changes: 21 additions & 2 deletions src/providers/arcgisrest/qgsarcgisrestutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "qgssinglesymbolrenderer.h"
#include "qgscategorizedsymbolrenderer.h"
#include "qgsvectorlayerlabeling.h"
#include "qgsapplication.h"
#include "qgsmessagelog.h"

#include <QEventLoop>
#include <QNetworkRequest>
Expand Down Expand Up @@ -1122,10 +1124,18 @@ QgsArcGisAsyncQuery::~QgsArcGisAsyncQuery()
mReply->deleteLater();
}

void QgsArcGisAsyncQuery::start( const QUrl &url, QByteArray *result, bool allowCache )
void QgsArcGisAsyncQuery::start( const QUrl &url, const QString &authCfg, QByteArray *result, bool allowCache )
{
mResult = result;
QNetworkRequest request( url );

if ( !authCfg.isEmpty() && !QgsApplication::authManager()->updateNetworkRequest( request, authCfg ) )
{
const QString error = tr( "network request update failed for authentication config" );
emit failed( QStringLiteral( "Network" ), error );
return;
}

QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisAsyncQuery" ) );
if ( allowCache )
{
Expand Down Expand Up @@ -1167,8 +1177,9 @@ void QgsArcGisAsyncQuery::handleReply()

///////////////////////////////////////////////////////////////////////////////

QgsArcGisAsyncParallelQuery::QgsArcGisAsyncParallelQuery( QObject *parent )
QgsArcGisAsyncParallelQuery::QgsArcGisAsyncParallelQuery( const QString &authcfg, QObject *parent )
: QObject( parent )
, mAuthCfg( authcfg )
{
}

Expand All @@ -1182,6 +1193,14 @@ void QgsArcGisAsyncParallelQuery::start( const QVector<QUrl> &urls, QVector<QByt
QNetworkRequest request( urls[i] );
QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsArcGisAsyncParallelQuery" ) );
QgsSetRequestInitiatorId( request, QString::number( i ) );
if ( !mAuthCfg.isEmpty() && !QgsApplication::authManager()->updateNetworkRequest( request, mAuthCfg ) )
{
const QString error = tr( "network request update failed for authentication config" );
mErrors.append( error );
QgsMessageLog::logMessage( error, tr( "Network" ) );
continue;
}

request.setAttribute( QNetworkRequest::HttpPipeliningAllowedAttribute, true );
if ( allowCache )
{
Expand Down
5 changes: 3 additions & 2 deletions src/providers/arcgisrest/qgsarcgisrestutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class QgsArcGisAsyncQuery : public QObject
QgsArcGisAsyncQuery( QObject *parent = nullptr );
~QgsArcGisAsyncQuery() override;

void start( const QUrl &url, QByteArray *result, bool allowCache = false );
void start( const QUrl &url, const QString &authCfg, QByteArray *result, bool allowCache = false );
signals:
void finished();
void failed( QString errorTitle, QString errorName );
Expand All @@ -123,7 +123,7 @@ class QgsArcGisAsyncParallelQuery : public QObject
{
Q_OBJECT
public:
QgsArcGisAsyncParallelQuery( QObject *parent = nullptr );
QgsArcGisAsyncParallelQuery( const QString &authcfg, QObject *parent = nullptr );
void start( const QVector<QUrl> &urls, QVector<QByteArray> *results, bool allowCache = false );

signals:
Expand All @@ -135,6 +135,7 @@ class QgsArcGisAsyncParallelQuery : public QObject
QVector<QByteArray> *mResults = nullptr;
int mPendingRequests = 0;
QStringList mErrors;
QString mAuthCfg;
};

#endif // QGSARCGISRESTUTILS_H

0 comments on commit 323fae2

Please sign in to comment.