Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Server] Throw exception if bad layers are not restricted
QGIS Server throw an exception if the project has bad layers, but the user can defined restricted layers which are unpublished layers.

So restricted layers can be bad layers server side, it is not necessary an error.

This code verified that the bad layers are not restricted. If the project contains unrestricted bad layers, the server throw an exception.
  • Loading branch information
rldhont committed Jan 13, 2020
1 parent 1751d8e commit a978e35
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
7 changes: 7 additions & 0 deletions python/server/auto_generated/qgsstorebadlayerinfo.sip.in
Expand Up @@ -39,6 +39,13 @@ handleBadLayers
badLayers

:return: ids of bad layers
%End

QMap<QString, QString> badLayerNames() const;
%Docstring
Returns names of bad layers with ids.

.. versionadded:: 3.12
%End

};
Expand Down
28 changes: 24 additions & 4 deletions src/server/qgsconfigcache.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsmessagelog.h"
#include "qgsserverexception.h"
#include "qgsstorebadlayerinfo.h"
#include "qgsserverprojectutils.h"

#include <QFile>

Expand Down Expand Up @@ -48,9 +49,29 @@ const QgsProject *QgsConfigCache::project( const QString &path )
{
if ( !badLayerHandler->badLayers().isEmpty() )
{
QString errorMsg = QStringLiteral( "Layer(s) %1 not valid" ).arg( badLayerHandler->badLayers().join( ',' ) );
QgsMessageLog::logMessage( errorMsg, QStringLiteral( "Server" ), Qgis::Critical );
throw QgsServerException( QStringLiteral( "Layer(s) not valid" ) );
// if bad layers are not restricted layers so service failed
QStringList unrestrictedBadLayers;
// test bad layers through restrictedlayers
const QStringList badLayerIds = badLayerHandler->badLayers();
const QMap<QString, QString> badLayerNames = badLayerHandler->badLayerNames();
const QStringList resctrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *prj );
for ( const QString &badLayerId : badLayerIds )
{
// if this bad layer is in restricted layers
// it doesn't need to be added to unrestricted bad layers
if ( badLayerNames.contains( badLayerId ) &&
resctrictedLayers.contains( badLayerNames.value( badLayerId ) ) )
{
continue;
}
unrestrictedBadLayers.append( badLayerId );
}
if ( !unrestrictedBadLayers.isEmpty() )
{
const QString errorMsg = QStringLiteral( "Layer(s) %1 not valid" ).arg( unrestrictedBadLayers.join( ',' ) );
QgsMessageLog::logMessage( errorMsg, QStringLiteral( "Server" ), Qgis::Critical );
throw QgsServerException( QStringLiteral( "Layer(s) not valid" ) );
}
}
mProjectCache.insert( path, prj.release() );
mFileSystemWatcher.addPath( path );
Expand Down Expand Up @@ -120,4 +141,3 @@ void QgsConfigCache::removeEntry( const QString &path )
{
removeChangedEntry( path );
}

8 changes: 7 additions & 1 deletion src/server/qgsstorebadlayerinfo.cpp
Expand Up @@ -29,7 +29,13 @@ void QgsStoreBadLayerInfo::handleBadLayers( const QList<QDomNode> &layers )
QDomElement idElem = it->firstChildElement( "id" );
if ( !idElem.isNull() )
{
mBadLayerIds.append( idElem.text() );
const QString badLayerId = idElem.text();
mBadLayerIds.append( badLayerId );
const QDomElement nameElem = it->firstChildElement( "layername" );
if ( !nameElem.isNull() )
{
mBadLayerNames.insert( badLayerId, nameElem.text() );
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/server/qgsstorebadlayerinfo.h
Expand Up @@ -21,6 +21,7 @@
#include "qgsprojectbadlayerhandler.h"
#include "qgis_server.h"
#include <QStringList>
#include <QMap>

/**
* \ingroup server
Expand Down Expand Up @@ -48,8 +49,15 @@ class SERVER_EXPORT QgsStoreBadLayerInfo: public QgsProjectBadLayerHandler
*/
QStringList badLayers() const { return mBadLayerIds; }

/**
* Returns names of bad layers with ids.
* \since QGIS 3.12
*/
QMap<QString, QString> badLayerNames() const { return mBadLayerNames; }

private:
QStringList mBadLayerIds;
QMap<QString, QString> mBadLayerNames;
};

#endif // QGSSTOREBADLAYERINFO_H

0 comments on commit a978e35

Please sign in to comment.