QGIS API Documentation 3.41.0-Master (57ec4277f5e)
Loading...
Searching...
No Matches
qgsaccesscontrol.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsaccesscontrol.cpp
3 --------------------
4 begin : 22-05-2015
5 copyright : (C) 2008 by Stéphane Brunner
6 email : stephane dot brunner at camptocamp dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsaccesscontrol.h"
19#include "qgsfeaturerequest.h"
20#include "qgsmaplayer.h"
21#include "qgsvectorlayer.h"
22
23
24void QgsAccessControl::resolveFilterFeatures( const QList<QgsMapLayer *> &layers )
25{
26 for ( QgsMapLayer *l : layers )
27 {
28 if ( l->type() == Qgis::LayerType::Vector )
29 {
30 const QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( l );
31 mFilterFeaturesExpressions[vl->id()] = resolveFilterFeatures( vl );
32 }
33 }
34
35 mResolved = true;
36}
37
39{
40 QStringList expressions = QStringList();
41 QgsAccessControlFilterMap::const_iterator acIterator;
42
43 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
44 {
45 const QString expression = acIterator.value()->layerFilterExpression( layer );
46 if ( !expression.isEmpty() )
47 {
48 expressions.append( expression );
49 }
50 }
51
52 QString expression;
53 if ( !expressions.isEmpty() )
54 {
55 expression = QStringLiteral( "((" ).append( expressions.join( QLatin1String( ") AND (" ) ) ).append( "))" );
56 }
57
58 return expression;
59}
60
63{
64 mFilterFeaturesExpressions.clear();
65 mResolved = false;
66}
67
69void QgsAccessControl::filterFeatures( const QgsVectorLayer *layer, QgsFeatureRequest &featureRequest ) const
70{
71 QString expression;
72
73 if ( mResolved && mFilterFeaturesExpressions.contains( layer->id() ) )
74 {
75 expression = mFilterFeaturesExpressions[layer->id()];
76 }
77 else
78 {
79 expression = resolveFilterFeatures( layer );
80 }
81
82 if ( !expression.isEmpty() )
83 {
84 featureRequest.combineFilterExpression( expression );
85 }
86}
87
90{
91 return new QgsAccessControl( *this );
92}
93
96{
97 QStringList sqls;
98 QgsAccessControlFilterMap::const_iterator acIterator;
99 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
100 {
101 const QString sql = acIterator.value()->layerFilterSubsetString( layer );
102 if ( !sql.isEmpty() )
103 {
104 sqls.append( sql );
105 }
106 }
107 return sqls.isEmpty() ? QString() : QStringLiteral( "((" ).append( sqls.join( QLatin1String( ") AND (" ) ) ).append( "))" );
108}
109
112{
113 QgsAccessControlFilterMap::const_iterator acIterator;
114 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
115 {
116 if ( !acIterator.value()->layerPermissions( layer ).canRead )
117 {
118 return false;
119 }
120 }
121 return true;
122}
123
126{
127 QgsAccessControlFilterMap::const_iterator acIterator;
128 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
129 {
130 if ( !acIterator.value()->layerPermissions( layer ).canInsert )
131 {
132 return false;
133 }
134 }
135 return true;
136}
137
140{
141 QgsAccessControlFilterMap::const_iterator acIterator;
142 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
143 {
144 if ( !acIterator.value()->layerPermissions( layer ).canUpdate )
145 {
146 return false;
147 }
148 }
149 return true;
150}
151
154{
155 QgsAccessControlFilterMap::const_iterator acIterator;
156 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
157 {
158 if ( !acIterator.value()->layerPermissions( layer ).canDelete )
159 {
160 return false;
161 }
162 }
163 return true;
164}
165
167QStringList QgsAccessControl::layerAttributes( const QgsVectorLayer *layer, const QStringList &attributes ) const
168{
169 QStringList currentAttributes( attributes );
170 QgsAccessControlFilterMap::const_iterator acIterator;
171 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
172 {
173 currentAttributes = acIterator.value()->authorizedLayerAttributes( layer, currentAttributes );
174 }
175 return currentAttributes;
176}
177
179bool QgsAccessControl::allowToEdit( const QgsVectorLayer *layer, const QgsFeature &feature ) const
180{
181 QgsAccessControlFilterMap::const_iterator acIterator;
182 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
183 {
184 if ( !acIterator.value()->allowToEdit( layer, feature ) )
185 {
186 return false;
187 }
188 }
189 return true;
190}
191
193bool QgsAccessControl::fillCacheKey( QStringList &cacheKey ) const
194{
195 QgsAccessControlFilterMap::const_iterator acIterator;
196 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
197 {
198 const QString newKey = acIterator.value()->cacheKey();
199 if ( !newKey.isEmpty() )
200 {
201 cacheKey.clear();
202 return false;
203 }
204 }
205 return true;
206}
207
210{
211 mPluginsAccessControls->insert( priority, accessControl );
212}
@ Vector
Vector layer.
Class defining access control interface for QGIS Server plugins.
QStringList layerAttributes(const QgsVectorLayer *layer, const QStringList &attributes) const override
Returns the authorized layer attributes.
bool layerUpdatePermission(const QgsVectorLayer *layer) const
Returns the layer update right.
void filterFeatures(const QgsVectorLayer *layer, QgsFeatureRequest &filterFeatures) const override
Filter the features of the layer.
bool layerInsertPermission(const QgsVectorLayer *layer) const
Returns the layer insert right.
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.
bool fillCacheKey(QStringList &cacheKey) const
Fill the capabilities caching key.
bool allowToEdit(const QgsVectorLayer *layer, const QgsFeature &feature) const
Are we authorized to modify the following geometry.
QgsFeatureFilterProvider * clone() const override
Returns a clone of the object.
bool layerDeletePermission(const QgsVectorLayer *layer) const
Returns the layer delete right.
void resolveFilterFeatures(const QList< QgsMapLayer * > &layers)
Resolve features' filter of layers The method fetch filter's expressions returned from access control...
void registerAccessControl(QgsAccessControlFilter *accessControl, int priority=0)
Register an access control filter.
void unresolveFilterFeatures()
Clear expression's cache computed from resolveFilterFeatures
QgsAccessControl()
Constructor.
QString extraSubsetString(const QgsVectorLayer *layer) const
Returns an additional subset string (typically SQL) filter.
Abstract interface for use by classes that filter the features or attributes of a layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & combineFilterExpression(const QString &expression)
Modifies the existing filter expression to add an additional expression filter.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Base class for all map layer types.
Definition qgsmaplayer.h:76
QString id
Definition qgsmaplayer.h:79
Represents a vector layer which manages a vector based data sets.