QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
72 QString expression;
73
74 if ( mResolved && mFilterFeaturesExpressions.contains( layer->id() ) )
75 {
76 expression = mFilterFeaturesExpressions[layer->id()];
77 }
78 else
79 {
80 expression = resolveFilterFeatures( layer );
81 }
82
83 if ( !expression.isEmpty() )
84 {
85 featureRequest.combineFilterExpression( expression );
86 }
87}
88
91{
92 return new QgsAccessControl( *this );
93}
94
97{
98 QStringList sqls;
99 QgsAccessControlFilterMap::const_iterator acIterator;
100 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
101 {
102 const QString sql = acIterator.value()->layerFilterSubsetString( layer );
103 if ( !sql.isEmpty() )
104 {
105 sqls.append( sql );
106 }
107 }
108 return sqls.isEmpty() ? QString() : QStringLiteral( "((" ).append( sqls.join( QLatin1String( ") AND (" ) ) ).append( "))" );
109}
110
113{
114 QgsAccessControlFilterMap::const_iterator acIterator;
115 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
116 {
117 if ( !acIterator.value()->layerPermissions( layer ).canRead )
118 {
119 return false;
120 }
121 }
122 return true;
123}
124
127{
128 QgsAccessControlFilterMap::const_iterator acIterator;
129 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
130 {
131 if ( !acIterator.value()->layerPermissions( layer ).canInsert )
132 {
133 return false;
134 }
135 }
136 return true;
137}
138
141{
142 QgsAccessControlFilterMap::const_iterator acIterator;
143 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
144 {
145 if ( !acIterator.value()->layerPermissions( layer ).canUpdate )
146 {
147 return false;
148 }
149 }
150 return true;
151}
152
155{
156 QgsAccessControlFilterMap::const_iterator acIterator;
157 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
158 {
159 if ( !acIterator.value()->layerPermissions( layer ).canDelete )
160 {
161 return false;
162 }
163 }
164 return true;
165}
166
168QStringList QgsAccessControl::layerAttributes( const QgsVectorLayer *layer, const QStringList &attributes ) const
169{
170 QStringList currentAttributes( attributes );
171 QgsAccessControlFilterMap::const_iterator acIterator;
172 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
173 {
174 currentAttributes = acIterator.value()->authorizedLayerAttributes( layer, currentAttributes );
175 }
176 return currentAttributes;
177}
178
180bool QgsAccessControl::allowToEdit( const QgsVectorLayer *layer, const QgsFeature &feature ) const
181{
182 QgsAccessControlFilterMap::const_iterator acIterator;
183 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
184 {
185 if ( !acIterator.value()->allowToEdit( layer, feature ) )
186 {
187 return false;
188 }
189 }
190 return true;
191}
192
194bool QgsAccessControl::fillCacheKey( QStringList &cacheKey ) const
195{
196 QgsAccessControlFilterMap::const_iterator acIterator;
197 for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
198 {
199 const QString newKey = acIterator.value()->cacheKey();
200 if ( ! newKey.isEmpty() )
201 {
202 cacheKey.clear();
203 return false;
204 }
205 }
206 return true;
207}
208
211{
212 mPluginsAccessControls->insert( priority, accessControl );
213}
@ 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.