QGIS API Documentation  3.14.0-Pi (9f7028fd23)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 }
qgsfeaturerequest.h
QgsMapLayerType::VectorLayer
@ VectorLayer
QgsAccessControl::filterFeatures
void filterFeatures(const QgsVectorLayer *layer, QgsFeatureRequest &filterFeatures) const override
Filter the features of the layer.
Definition: qgsaccesscontrol.cpp:62
QgsAccessControl::registerAccessControl
void registerAccessControl(QgsAccessControlFilter *accessControl, int priority=0)
Register an access control filter.
Definition: qgsaccesscontrol.cpp:204
QgsAccessControl::clone
QgsFeatureFilterProvider * clone() const override
Returns a clone of the object.
Definition: qgsaccesscontrol.cpp:83
QgsAccessControl::layerReadPermission
bool layerReadPermission(const QgsMapLayer *layer) const
Returns the layer read right.
Definition: qgsaccesscontrol.cpp:105
QgsAccessControl::QgsAccessControl
QgsAccessControl()
Constructor.
Definition: qgsaccesscontrol.h:44
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:129
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsAccessControl::layerUpdatePermission
bool layerUpdatePermission(const QgsVectorLayer *layer) const
Returns the layer update right.
Definition: qgsaccesscontrol.cpp:133
QgsAccessControl::allowToEdit
bool allowToEdit(const QgsVectorLayer *layer, const QgsFeature &feature) const
Are we authorized to modify the following geometry.
Definition: qgsaccesscontrol.cpp:173
QgsAccessControl::resolveFilterFeatures
void resolveFilterFeatures(const QList< QgsMapLayer * > &layers)
Resolve features' filter of layers.
Definition: qgsaccesscontrol.cpp:24
QgsAccessControl::fillCacheKey
bool fillCacheKey(QStringList &cacheKey) const
Fill the capabilities caching key.
Definition: qgsaccesscontrol.cpp:187
QgsAccessControlFilter
Class defining access control interface for QGIS Server plugins.
Definition: qgsaccesscontrolfilter.h:50
qgsmaplayer.h
QgsAccessControl::layerAttributes
QStringList layerAttributes(const QgsVectorLayer *layer, const QStringList &attributes) const
Returns the authorized layer attributes.
Definition: qgsaccesscontrol.cpp:161
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsAccessControl::layerInsertPermission
bool layerInsertPermission(const QgsVectorLayer *layer) const
Returns the layer insert right.
Definition: qgsaccesscontrol.cpp:119
qgsvectorlayer.h
qgsaccesscontrol.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsAccessControl::layerDeletePermission
bool layerDeletePermission(const QgsVectorLayer *layer) const
Returns the layer delete right.
Definition: qgsaccesscontrol.cpp:147
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsFeatureFilterProvider
Definition: qgsfeaturefilterprovider.h:40
QgsFeature
Definition: qgsfeature.h:55
QgsAccessControl::extraSubsetString
QString extraSubsetString(const QgsVectorLayer *layer) const
Returns an additional subset string (typically SQL) filter.
Definition: qgsaccesscontrol.cpp:89