QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
24 void QgsAccessControl::resolveFilterFeatures( const QList<QgsMapLayer *> &layers )
25 {
26  for ( QgsMapLayer *l : layers )
27  {
28  if ( l->type() == QgsMapLayerType::VectorLayer )
29  {
30  const QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( l );
31  mFilterFeaturesExpressions[vl->id()] = resolveFilterFeatures( vl );
32  }
33  }
34 
35  mResolved = true;
36 }
37 
38 QString QgsAccessControl::resolveFilterFeatures( const QgsVectorLayer *layer ) const
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 
62 void QgsAccessControl::filterFeatures( const QgsVectorLayer *layer, QgsFeatureRequest &featureRequest ) const
63 {
64 
65  QString expression;
66 
67  if ( mResolved && mFilterFeaturesExpressions.contains( layer->id() ) )
68  {
69  expression = mFilterFeaturesExpressions[layer->id()];
70  }
71  else
72  {
73  expression = resolveFilterFeatures( layer );
74  }
75 
76  if ( !expression.isEmpty() )
77  {
78  featureRequest.combineFilterExpression( expression );
79  }
80 }
81 
84 {
85  return new QgsAccessControl( *this );
86 }
87 
90 {
91  QStringList sqls;
92  QgsAccessControlFilterMap::const_iterator acIterator;
93  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
94  {
95  const QString sql = acIterator.value()->layerFilterSubsetString( layer );
96  if ( !sql.isEmpty() )
97  {
98  sqls.append( sql );
99  }
100  }
101  return sqls.isEmpty() ? QString() : QStringLiteral( "((" ).append( sqls.join( QLatin1String( ") AND (" ) ) ).append( "))" );
102 }
103 
106 {
107  QgsAccessControlFilterMap::const_iterator acIterator;
108  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
109  {
110  if ( !acIterator.value()->layerPermissions( layer ).canRead )
111  {
112  return false;
113  }
114  }
115  return true;
116 }
117 
120 {
121  QgsAccessControlFilterMap::const_iterator acIterator;
122  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
123  {
124  if ( !acIterator.value()->layerPermissions( layer ).canInsert )
125  {
126  return false;
127  }
128  }
129  return true;
130 }
131 
134 {
135  QgsAccessControlFilterMap::const_iterator acIterator;
136  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
137  {
138  if ( !acIterator.value()->layerPermissions( layer ).canUpdate )
139  {
140  return false;
141  }
142  }
143  return true;
144 }
145 
148 {
149  QgsAccessControlFilterMap::const_iterator acIterator;
150  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
151  {
152  if ( !acIterator.value()->layerPermissions( layer ).canDelete )
153  {
154  return false;
155  }
156  }
157  return true;
158 }
159 
161 QStringList QgsAccessControl::layerAttributes( const QgsVectorLayer *layer, const QStringList &attributes ) const
162 {
163  QStringList currentAttributes( attributes );
164  QgsAccessControlFilterMap::const_iterator acIterator;
165  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
166  {
167  currentAttributes = acIterator.value()->authorizedLayerAttributes( layer, currentAttributes );
168  }
169  return currentAttributes;
170 }
171 
173 bool QgsAccessControl::allowToEdit( const QgsVectorLayer *layer, const QgsFeature &feature ) const
174 {
175  QgsAccessControlFilterMap::const_iterator acIterator;
176  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
177  {
178  if ( !acIterator.value()->allowToEdit( layer, feature ) )
179  {
180  return false;
181  }
182  }
183  return true;
184 }
185 
187 bool QgsAccessControl::fillCacheKey( QStringList &cacheKey ) const
188 {
189  QgsAccessControlFilterMap::const_iterator acIterator;
190  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
191  {
192  const QString newKey = acIterator.value()->cacheKey();
193  if ( ! newKey.isEmpty() )
194  {
195  cacheKey.clear();
196  return false;
197  }
198  }
199  return true;
200 }
201 
204 {
205  mPluginsAccessControls->insert( priority, accessControl );
206 }
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.
void registerAccessControl(QgsAccessControlFilter *accessControl, int priority=0)
Register an access control filter.
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:56
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Represents a vector layer which manages a vector based data sets.
@ VectorLayer
Vector layer.