QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  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( QStringLiteral( ") 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.keys().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.setFilterExpression( 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  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( QStringLiteral( ") 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  QString newKey = acIterator.value()->cacheKey();
193  if ( newKey.length() == 0 )
194  {
195  cacheKey.clear();
196  return false;
197  }
198  cacheKey << newKey;
199  }
200  return true;
201 }
202 
205 {
206  mPluginsAccessControls->insert( priority, accessControl );
207 }
bool layerInsertPermission(const QgsVectorLayer *layer) const
Returns the layer insert right.
bool fillCacheKey(QStringList &cacheKey) const
Fill the capabilities caching key.
void resolveFilterFeatures(const QList< QgsMapLayer *> &layers)
Resolve features&#39; filter of layers.
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QString extraSubsetString(const QgsVectorLayer *layer) const
Returns an additional subset string (typically SQL) filter.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool layerDeletePermission(const QgsVectorLayer *layer) const
Returns the layer delete right.
Class defining access control interface for QGIS Server plugins.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Abstract interface for use by classes that filter the features of a layer.
QgsFeatureFilterProvider * clone() const override
Returns a clone of the object.
QgsAccessControl()
Constructor.
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.
void filterFeatures(const QgsVectorLayer *layer, QgsFeatureRequest &filterFeatures) const override
Filter the features of the layer.
bool layerUpdatePermission(const QgsVectorLayer *layer) const
Returns the layer update right.
void registerAccessControl(QgsAccessControlFilter *accessControl, int priority=0)
Register an access control filter.
Represents a vector layer which manages a vector based data sets.
QStringList layerAttributes(const QgsVectorLayer *layer, const QStringList &attributes) const
Returns the authorized layer attributes.
bool allowToEdit(const QgsVectorLayer *layer, const QgsFeature &feature) const
Are we authorized to modify the following geometry.