QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #include <QStringList>
24 
25 void QgsAccessControl::resolveFilterFeatures( const QList<QgsMapLayer *> &layers )
26 {
27  for ( QgsMapLayer *l : layers )
28  {
29  if ( l->type() == QgsMapLayer::LayerType::VectorLayer )
30  {
31  const QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( l );
32  mFilterFeaturesExpressions[vl->id()] = resolveFilterFeatures( vl );
33  }
34  }
35 
36  mResolved = true;
37 }
38 
39 QString QgsAccessControl::resolveFilterFeatures( const QgsVectorLayer *layer ) const
40 {
41  QStringList expressions = QStringList();
42  QgsAccessControlFilterMap::const_iterator acIterator;
43 
44  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
45  {
46  QString expression = acIterator.value()->layerFilterExpression( layer );
47  if ( !expression.isEmpty() )
48  {
49  expressions.append( expression );
50  }
51  }
52 
53  QString expression;
54  if ( !expressions.isEmpty() )
55  {
56  expression = QStringLiteral( "((" ).append( expressions.join( QStringLiteral( ") AND (" ) ) ).append( "))" );
57  }
58 
59  return expression;
60 }
61 
63 void QgsAccessControl::filterFeatures( const QgsVectorLayer *layer, QgsFeatureRequest &featureRequest ) const
64 {
65 
66  QString expression;
67 
68  if ( mResolved && mFilterFeaturesExpressions.keys().contains( layer->id() ) )
69  {
70  expression = mFilterFeaturesExpressions[layer->id()];
71  }
72  else
73  {
74  expression = resolveFilterFeatures( layer );
75  }
76 
77  if ( !expression.isEmpty() )
78  {
79  featureRequest.setFilterExpression( expression );
80  }
81 }
82 
85 {
86  return new QgsAccessControl( *this );
87 }
88 
91 {
92  QStringList sqls;
93  QgsAccessControlFilterMap::const_iterator acIterator;
94  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
95  {
96  QString sql = acIterator.value()->layerFilterSubsetString( layer );
97  if ( !sql.isEmpty() )
98  {
99  sqls.append( sql );
100  }
101  }
102  return sqls.isEmpty() ? QString() : QStringLiteral( "((" ).append( sqls.join( QStringLiteral( ") AND (" ) ) ).append( "))" );
103 }
104 
107 {
108  QgsAccessControlFilterMap::const_iterator acIterator;
109  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
110  {
111  if ( !acIterator.value()->layerPermissions( layer ).canRead )
112  {
113  return false;
114  }
115  }
116  return true;
117 }
118 
121 {
122  QgsAccessControlFilterMap::const_iterator acIterator;
123  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
124  {
125  if ( !acIterator.value()->layerPermissions( layer ).canInsert )
126  {
127  return false;
128  }
129  }
130  return true;
131 }
132 
135 {
136  QgsAccessControlFilterMap::const_iterator acIterator;
137  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
138  {
139  if ( !acIterator.value()->layerPermissions( layer ).canUpdate )
140  {
141  return false;
142  }
143  }
144  return true;
145 }
146 
149 {
150  QgsAccessControlFilterMap::const_iterator acIterator;
151  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
152  {
153  if ( !acIterator.value()->layerPermissions( layer ).canDelete )
154  {
155  return false;
156  }
157  }
158  return true;
159 }
160 
162 QStringList QgsAccessControl::layerAttributes( const QgsVectorLayer *layer, const QStringList &attributes ) const
163 {
164  QStringList currentAttributes( attributes );
165  QgsAccessControlFilterMap::const_iterator acIterator;
166  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
167  {
168  currentAttributes = acIterator.value()->authorizedLayerAttributes( layer, currentAttributes );
169  }
170  return currentAttributes;
171 }
172 
174 bool QgsAccessControl::allowToEdit( const QgsVectorLayer *layer, const QgsFeature &feature ) const
175 {
176  QgsAccessControlFilterMap::const_iterator acIterator;
177  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
178  {
179  if ( !acIterator.value()->allowToEdit( layer, feature ) )
180  {
181  return false;
182  }
183  }
184  return true;
185 }
186 
188 bool QgsAccessControl::fillCacheKey( QStringList &cacheKey ) const
189 {
190  QgsAccessControlFilterMap::const_iterator acIterator;
191  for ( acIterator = mPluginsAccessControls->constBegin(); acIterator != mPluginsAccessControls->constEnd(); ++acIterator )
192  {
193  QString newKey = acIterator.value()->cacheKey();
194  if ( newKey.length() == 0 )
195  {
196  cacheKey.clear();
197  return false;
198  }
199  cacheKey << newKey;
200  }
201  return true;
202 }
203 
206 {
207  mPluginsAccessControls->insert( priority, accessControl );
208 }
bool fillCacheKey(QStringList &cacheKey) const
Fill the capabilities caching key.
bool layerInsertPermission(const QgsVectorLayer *layer) const
Returns the layer insert right.
Base class for all map layer types.
Definition: qgsmaplayer.h:63
bool layerUpdatePermission(const QgsVectorLayer *layer) const
Returns the layer update right.
void resolveFilterFeatures(const QList< QgsMapLayer * > &layers)
Resolve features&#39; filter of layers.
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
Class defining access control interface for QGIS Server plugins.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
Abstract interface for use by classes that filter the features of a layer.
QgsFeatureFilterProvider * clone() const override
Returns a clone of the object.
bool allowToEdit(const QgsVectorLayer *layer, const QgsFeature &feature) const
Are we authorized to modify the following geometry.
QgsAccessControl()
Constructor.
QStringList layerAttributes(const QgsVectorLayer *layer, const QStringList &attributes) const
Returns the authorized layer attributes.
void filterFeatures(const QgsVectorLayer *layer, QgsFeatureRequest &filterFeatures) const override
Filter the features of the layer.
void registerAccessControl(QgsAccessControlFilter *accessControl, int priority=0)
Register an access control filter.
bool layerDeletePermission(const QgsVectorLayer *layer) const
Returns the layer delete right.
Represents a vector layer which manages a vector based data sets.
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.