Skip to content

Commit 11ea28a

Browse files
committedDec 14, 2018
[FEATURE][processing] Add a new parameter type for authentication config
This adds a new available parameter type for processing algorithms, QgsProcessingParameterAuthConfig, allowing selection from available authentication configurations (and creation of new ones). It allows creation of processing algorithm which can fully take advantage of QGIS' mature authentication handling, avoiding the need to use insecure string parameters for users to input sensitive logon credentials. QgsProcessingParameterAuthConfig parameters are evaluated using QgsProcessingAlgorithm.parameterAsString(), which returns the selected authentication configuration ID.
1 parent 8f06b5a commit 11ea28a

14 files changed

+519
-15
lines changed
 

‎python/core/auto_generated/processing/qgsprocessingparameters.sip.in

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ their acceptable ranges, defaults, etc.
161161
sipType = sipType_QgsProcessingParameterString;
162162
else if ( sipCpp->type() == QgsProcessingParameterExpression::typeName() )
163163
sipType = sipType_QgsProcessingParameterExpression;
164+
else if ( sipCpp->type() == QgsProcessingParameterAuthConfig::typeName() )
165+
sipType = sipType_QgsProcessingParameterAuthConfig;
164166
else if ( sipCpp->type() == QgsProcessingParameterVectorLayer::typeName() )
165167
sipType = sipType_QgsProcessingParameterVectorLayer;
166168
else if ( sipCpp->type() == QgsProcessingParameterField::typeName() )
@@ -1767,6 +1769,50 @@ Creates a new parameter using the definition from a script code.
17671769

17681770
};
17691771

1772+
1773+
class QgsProcessingParameterAuthConfig : QgsProcessingParameterDefinition
1774+
{
1775+
%Docstring
1776+
A string parameter for authentication configuration configuration ID values.
1777+
1778+
This parameter allows for users to select from available authentication configurations,
1779+
or create new authentication configurations as required.
1780+
1781+
QgsProcessingParameterAuthConfig should be evaluated by calling :py:func:`QgsProcessingAlgorithm.parameterAsString()`
1782+
1783+
.. versionadded:: 3.6
1784+
%End
1785+
1786+
%TypeHeaderCode
1787+
#include "qgsprocessingparameters.h"
1788+
%End
1789+
public:
1790+
1791+
QgsProcessingParameterAuthConfig( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
1792+
bool optional = false );
1793+
%Docstring
1794+
Constructor for QgsProcessingParameterAuthConfig.
1795+
%End
1796+
1797+
static QString typeName();
1798+
%Docstring
1799+
Returns the type name for the parameter class.
1800+
%End
1801+
virtual QgsProcessingParameterDefinition *clone() const /Factory/;
1802+
1803+
virtual QString type() const;
1804+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
1805+
1806+
virtual QString asScriptCode() const;
1807+
1808+
1809+
static QgsProcessingParameterAuthConfig *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
1810+
%Docstring
1811+
Creates a new parameter using the definition from a script code.
1812+
%End
1813+
1814+
};
1815+
17701816
class QgsProcessingParameterExpression : QgsProcessingParameterDefinition
17711817
{
17721818
%Docstring

‎python/processing/algfactory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from qgis.core import (QgsProcessingParameterDefinition,
3232
QgsProcessingAlgorithm,
3333
QgsProcessingParameterString,
34+
QgsProcessingParameterAuthConfig,
3435
QgsProcessingParameterNumber,
3536
QgsProcessingParameterDistance,
3637
QgsProcessingParameterFeatureSource,
@@ -222,7 +223,7 @@ def _get_parameter_value(self, parm, parameters, name, context):
222223
"""
223224
Extract the real value from the parameter.
224225
"""
225-
if isinstance(parm, QgsProcessingParameterString):
226+
if isinstance(parm, (QgsProcessingParameterString, QgsProcessingParameterAuthConfig)):
226227
value = self.parameterAsString(parameters, name, context)
227228
return value
228229
elif isinstance(parm, QgsProcessingParameterNumber):
@@ -312,6 +313,7 @@ class ProcessingAlgFactory():
312313
MATRIX = "MATRIX",
313314
POINT = "POINT",
314315
RANGE = "RANGE",
316+
AUTH_CFG = "AUTH_CFG"
315317

316318
def __init__(self):
317319
self._current = None
@@ -436,6 +438,7 @@ def input(self, type, *args, **kwargs):
436438
alg.POINT: QgsProcessingParameterPoint
437439
alg.RANGE: QgsProcessingParameterRange
438440
alg.VECTOR_LAYER: QgsProcessingParameterVectorLayer
441+
alg.AUTH_CFG: QgsProcessingParameterAuthConfig
439442
440443
441444
:param type: The type of the input. This should be a type define on `alg` like alg.STRING, alg.DISTANCE
@@ -481,6 +484,7 @@ def dec(f):
481484
ProcessingAlgFactory.POINT: QgsProcessingParameterPoint,
482485
ProcessingAlgFactory.RANGE: QgsProcessingParameterRange,
483486
ProcessingAlgFactory.VECTOR_LAYER: QgsProcessingParameterVectorLayer,
487+
ProcessingAlgFactory.AUTH_CFG: QgsProcessingParameterAuthConfig,
484488
}
485489

486490
output_type_mapping = {

‎src/core/processing/models/qgsprocessingmodelalgorithm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
452452
<< QgsProcessingParameterBoolean::typeName()
453453
<< QgsProcessingParameterExpression::typeName()
454454
<< QgsProcessingParameterField::typeName()
455-
<< QgsProcessingParameterString::typeName(),
455+
<< QgsProcessingParameterString::typeName()
456+
<< QgsProcessingParameterAuthConfig::typeName(),
456457
QStringList() << QgsProcessingOutputNumber::typeName()
457458
<< QgsProcessingOutputString::typeName() );
458459
Q_FOREACH ( const QgsProcessingModelChildParameterSource &source, sources )

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
14631463
def.reset( new QgsProcessingParameterEnum( name ) );
14641464
else if ( type == QgsProcessingParameterString::typeName() )
14651465
def.reset( new QgsProcessingParameterString( name ) );
1466+
else if ( type == QgsProcessingParameterAuthConfig::typeName() )
1467+
def.reset( new QgsProcessingParameterAuthConfig( name ) );
14661468
else if ( type == QgsProcessingParameterExpression::typeName() )
14671469
def.reset( new QgsProcessingParameterExpression( name ) );
14681470
else if ( type == QgsProcessingParameterVectorLayer::typeName() )
@@ -1543,6 +1545,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
15431545
return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
15441546
else if ( type == QStringLiteral( "string" ) )
15451547
return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
1548+
else if ( type == QStringLiteral( "authcfg" ) )
1549+
return QgsProcessingParameterAuthConfig::fromScriptCode( name, description, isOptional, definition );
15461550
else if ( type == QStringLiteral( "expression" ) )
15471551
return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
15481552
else if ( type == QStringLiteral( "field" ) )
@@ -3008,6 +3012,62 @@ QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( cons
30083012
return new QgsProcessingParameterString( name, description, defaultValue, multiLine, isOptional );
30093013
}
30103014

3015+
//
3016+
// QgsProcessingParameterAuthConfig
3017+
//
3018+
3019+
QgsProcessingParameterAuthConfig::QgsProcessingParameterAuthConfig( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
3020+
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3021+
{
3022+
3023+
}
3024+
3025+
QgsProcessingParameterDefinition *QgsProcessingParameterAuthConfig::clone() const
3026+
{
3027+
return new QgsProcessingParameterAuthConfig( *this );
3028+
}
3029+
3030+
QString QgsProcessingParameterAuthConfig::valueAsPythonString( const QVariant &value, QgsProcessingContext & ) const
3031+
{
3032+
if ( !value.isValid() )
3033+
return QStringLiteral( "None" );
3034+
3035+
QString s = value.toString();
3036+
return QgsProcessingUtils::stringToPythonLiteral( s );
3037+
}
3038+
3039+
QString QgsProcessingParameterAuthConfig::asScriptCode() const
3040+
{
3041+
QString code = QStringLiteral( "##%1=" ).arg( mName );
3042+
if ( mFlags & FlagOptional )
3043+
code += QStringLiteral( "optional " );
3044+
code += QStringLiteral( "authcfg " );
3045+
3046+
code += mDefault.toString();
3047+
return code.trimmed();
3048+
}
3049+
3050+
QgsProcessingParameterAuthConfig *QgsProcessingParameterAuthConfig::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3051+
{
3052+
QString def = definition;
3053+
3054+
if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
3055+
def = def.mid( 1 );
3056+
if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
3057+
def.chop( 1 );
3058+
3059+
QVariant defaultValue = def;
3060+
if ( def == QStringLiteral( "None" ) )
3061+
defaultValue = QVariant();
3062+
3063+
return new QgsProcessingParameterAuthConfig( name, description, defaultValue, isOptional );
3064+
}
3065+
3066+
3067+
//
3068+
// QgsProcessingParameterExpression
3069+
//
3070+
30113071
QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
30123072
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
30133073
, mParentLayerParameterName( parentLayerParameterName )

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/***************************************************************************
2-
qgsprocessingparameters.h
3-
-------------------------
4-
begin : April 2017
5-
copyright : (C) 2017 by Nyall Dawson
6-
email : nyall dot dawson at gmail dot com
7-
***************************************************************************/
2+
qgsprocessingparameters.h
3+
-------------------------
4+
begin : April 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
88

99
/***************************************************************************
1010
* *
@@ -234,6 +234,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
234234
sipType = sipType_QgsProcessingParameterString;
235235
else if ( sipCpp->type() == QgsProcessingParameterExpression::typeName() )
236236
sipType = sipType_QgsProcessingParameterExpression;
237+
else if ( sipCpp->type() == QgsProcessingParameterAuthConfig::typeName() )
238+
sipType = sipType_QgsProcessingParameterAuthConfig;
237239
else if ( sipCpp->type() == QgsProcessingParameterVectorLayer::typeName() )
238240
sipType = sipType_QgsProcessingParameterVectorLayer;
239241
else if ( sipCpp->type() == QgsProcessingParameterField::typeName() )
@@ -1723,6 +1725,45 @@ class CORE_EXPORT QgsProcessingParameterString : public QgsProcessingParameterDe
17231725

17241726
};
17251727

1728+
1729+
/**
1730+
* \class QgsProcessingParameterAuthConfig
1731+
* \ingroup core
1732+
* A string parameter for authentication configuration configuration ID values.
1733+
*
1734+
* This parameter allows for users to select from available authentication configurations,
1735+
* or create new authentication configurations as required.
1736+
*
1737+
* QgsProcessingParameterAuthConfig should be evaluated by calling QgsProcessingAlgorithm::parameterAsString().
1738+
*
1739+
* \since QGIS 3.6
1740+
*/
1741+
class CORE_EXPORT QgsProcessingParameterAuthConfig : public QgsProcessingParameterDefinition
1742+
{
1743+
public:
1744+
1745+
/**
1746+
* Constructor for QgsProcessingParameterAuthConfig.
1747+
*/
1748+
QgsProcessingParameterAuthConfig( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
1749+
bool optional = false );
1750+
1751+
/**
1752+
* Returns the type name for the parameter class.
1753+
*/
1754+
static QString typeName() { return QStringLiteral( "authcfg" ); }
1755+
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
1756+
QString type() const override { return typeName(); }
1757+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
1758+
QString asScriptCode() const override;
1759+
1760+
/**
1761+
* Creates a new parameter using the definition from a script code.
1762+
*/
1763+
static QgsProcessingParameterAuthConfig *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
1764+
1765+
};
1766+
17261767
/**
17271768
* \class QgsProcessingParameterExpression
17281769
* \ingroup core

‎src/core/processing/qgsprocessingparametertypeimpl.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,41 @@ class CORE_EXPORT QgsProcessingParameterTypeString : public QgsProcessingParamet
744744
}
745745
};
746746

747+
/**
748+
* A authentication configuration parameter for processing algorithms.
749+
*
750+
* \ingroup core
751+
* \note No Python bindings available. Get your copy from QgsApplication.processingRegistry().parameterType('authcfg')
752+
* \since QGIS 3.6
753+
*/
754+
class CORE_EXPORT QgsProcessingParameterTypeAuthConfig : public QgsProcessingParameterType
755+
{
756+
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
757+
{
758+
return new QgsProcessingParameterAuthConfig( name );
759+
}
760+
761+
QString description() const override
762+
{
763+
return QCoreApplication::translate( "Processing", "A authentication configuration parameter." );
764+
}
765+
766+
QString name() const override
767+
{
768+
return QCoreApplication::translate( "Processing", "Authentication Configuration" );
769+
}
770+
771+
QString id() const override
772+
{
773+
return QStringLiteral( "authcfg" );
774+
}
775+
776+
QStringList acceptedPythonTypes() const override
777+
{
778+
return QStringList() << QStringLiteral( "str" );
779+
}
780+
};
781+
747782
/**
748783
* A parameter for processing algorithms which accepts multiple map layers.
749784
*

‎src/core/processing/qgsprocessingregistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
4040
addParameterType( new QgsProcessingParameterTypeFileDestination() );
4141
addParameterType( new QgsProcessingParameterTypeFolderDestination() );
4242
addParameterType( new QgsProcessingParameterTypeString() );
43+
addParameterType( new QgsProcessingParameterTypeAuthConfig() );
4344
addParameterType( new QgsProcessingParameterTypeMultipleLayers() );
4445
addParameterType( new QgsProcessingParameterTypeFeatureSource() );
4546
addParameterType( new QgsProcessingParameterTypeNumber() );

‎src/gui/processing/qgsprocessingguiregistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
3333
addParameterWidgetFactory( new QgsProcessingNumericWidgetWrapper() );
3434
addParameterWidgetFactory( new QgsProcessingDistanceWidgetWrapper() );
3535
addParameterWidgetFactory( new QgsProcessingRangeWidgetWrapper() );
36+
addParameterWidgetFactory( new QgsProcessingAuthConfigWidgetWrapper() );
3637
}
3738

3839
QgsProcessingGuiRegistry::~QgsProcessingGuiRegistry()

0 commit comments

Comments
 (0)
Please sign in to comment.