This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata_service_permission.py
306 lines (258 loc) · 11.8 KB
/
data_service_permission.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from flask import json
from sqlalchemy.sql import text as sql_text
from permission_query import PermissionQuery
from qgs_reader import QGSReader
class DataServicePermission(PermissionQuery):
"""DataServicePermission class
Query permissions for a data service.
"""
def __init__(self, db_engine, config_models, logger):
"""Constructor
:param DatabaseEngine db_engine: Database engine with DB connections
:param ConfigModels config_models: Helper for ORM models
:param Logger logger: Application logger
"""
super(DataServicePermission, self).__init__(config_models, logger)
self.db_engine = db_engine
def permissions(self, params, username, group, session):
"""Query permissions for editing a dataset.
Return dataset edit permissions if available and permitted.
Dataset ID can be either '<QGS name>.<Data layer name>' for a specific
QGIS project or '<Data layer name>' if the data layer name is unique.
:param obj params: Request parameters with dataset='<Dataset ID>'
:param str username: User name
:param str group: Group name
:param Session session: DB session
"""
permissions = {}
dataset = params.get('dataset', '')
parts = dataset.split('.')
if len(parts) > 1:
map_name = parts[0]
layer_name = parts[1]
else:
# no map name given
map_name = None
layer_name = dataset
data_permissions = self.data_permissions(
map_name, layer_name, username, group, session
)
if data_permissions['permitted']:
# get layer metadata from QGIS project
qgs_reader = QGSReader(self.logger)
if qgs_reader.read(data_permissions['map_name']):
permissions = qgs_reader.layer_metadata(layer_name)
if permissions:
permissions.update({
'dataset': dataset,
'writable': data_permissions['writable'],
'creatable': data_permissions['creatable'],
'readable': data_permissions['readable'],
'updatable': data_permissions['updatable'],
'deletable': data_permissions['deletable']
})
self.filter_restricted_attributes(
data_permissions['restricted_attributes'],
permissions
)
self.lookup_attribute_data_types(permissions)
return permissions
def data_permissions(self, map_name, layer_name, username, group, session):
"""Query resource permissions and return whether map and data layer are
permitted and writable (with CRUD permissions), and any restricted
attributes.
If map_name is None, the data permission with highest priority is used.
:param str map_name: Map name
:param str layer_name: Data layer name
:param str username: User name
:param str group: Group name
:param Session session: DB session
"""
Permission = self.config_models.model('permissions')
Resource = self.config_models.model('resources')
map_id = None
if map_name is None:
# find map for data layer name
data_resource_types = [
'data',
'data_create', 'data_read', 'data_update', 'data_delete'
]
data_query = self.user_permissions_query(
username, group, session
).join(Permission.resource). \
filter(Resource.type.in_(data_resource_types)). \
filter(Resource.name == layer_name). \
order_by(Permission.priority.desc()). \
distinct(Permission.priority)
# use data permission with highest priority
data_permission = data_query.first()
if data_permission is not None:
map_id = data_permission.resource.parent_id
map_query = session.query(Resource). \
filter(Resource.type == 'map'). \
filter(Resource.id == map_id)
map_obj = map_query.first()
if map_obj is not None:
map_name = map_obj.name
self.logger.info(
"No map name given, using map '%s'" % map_name
)
else:
# query map permissions
maps_query = self.user_permissions_query(
username, group, session
).join(Permission.resource).filter(Resource.type == 'map'). \
filter(Resource.name == map_name)
for map_permission in maps_query.all():
map_id = map_permission.resource.id
if map_id is None:
# map not found or not permitted
# NOTE: map without resource record cannot have data layers
return {
'permitted': False
}
# query data permissions
permitted = False
writable = False
creatable = False
readable = False
updatable = False
deletable = False
restricted_attributes = []
# NOTE: use permission with highest priority
base_query = self.user_permissions_query(username, group, session). \
join(Permission.resource). \
filter(Resource.parent_id == map_id). \
filter(Resource.name == layer_name). \
order_by(Permission.priority.desc()). \
distinct(Permission.priority)
# query 'data' permission
data_query = base_query.filter(Resource.type == 'data')
data_permission = data_query.first()
if data_permission is not None:
# 'data' permitted
permitted = True
writable = data_permission.write
creatable = writable
readable = True
updatable = writable
deletable = writable
# query attribute restrictions
attrs_query = self.resource_restrictions_query(
'attribute', username, group, session
).filter(Resource.parent_id == data_permission.resource_id)
for attr in attrs_query.all():
restricted_attributes.append(attr.name)
else:
# query detailed CRUD data permissions
create_query = base_query.filter(Resource.type == 'data_create')
creatable = create_query.first() is not None
read_query = base_query.filter(Resource.type == 'data_read')
readable = read_query.first() is not None
update_query = base_query.filter(Resource.type == 'data_update')
updatable = update_query.first() is not None
delete_query = base_query.filter(Resource.type == 'data_delete')
deletable = delete_query.first() is not None
permitted = creatable or readable or updatable or deletable
writable = creatable and readable and updatable and deletable
# TODO: restricted attributes
return {
'map_name': map_name,
'permitted': permitted,
'writable': writable,
'creatable': creatable,
'readable': readable,
'updatable': updatable,
'deletable': deletable,
'restricted_attributes': restricted_attributes
}
def filter_restricted_attributes(self, restricted_attributes, permissions):
"""Filter restricted attributes from Data service permissions.
:param list[str] restricted_attributes: List of restricted attributes
:param obj permissions: Data service permissions
"""
for attr in restricted_attributes:
if attr in permissions['attributes']:
permissions['attributes'].remove(attr)
def lookup_attribute_data_types(self, permissions):
"""Query column data types and add them to Data service permissions.
:param obj permissions: Data service permissions
"""
try:
connection_string = permissions['database']
schema = permissions['schema']
table_name = permissions['table_name']
# connect to GeoDB
geo_db = self.db_engine.db_engine(connection_string)
conn = geo_db.connect()
for attr in permissions['attributes']:
# build query SQL
sql = sql_text("""
SELECT data_type, character_maximum_length,
numeric_precision, numeric_scale
FROM information_schema.columns
WHERE table_schema = '{schema}' AND table_name = '{table}'
AND column_name = '{column}'
ORDER BY ordinal_position;
""".format(schema=schema, table=table_name, column=attr))
# execute query
data_type = None
constraints = {}
result = conn.execute(sql)
for row in result:
data_type = row['data_type']
# constraints from data type
if (data_type in ['character', 'character varying'] and
row['character_maximum_length']):
constraints = {
'maxlength': row['character_maximum_length']
}
elif data_type in ['double precision', 'real']:
# NOTE: use text field with pattern for floats
constraints['pattern'] = '[0-9]+([\\.,][0-9]+)?'
elif data_type == 'numeric' and row['numeric_precision']:
step = pow(10, -row['numeric_scale'])
max_value = pow(
10, row['numeric_precision'] - row['numeric_scale']
) - step
constraints = {
'numeric_precision': row['numeric_precision'],
'numeric_scale': row['numeric_scale'],
'min': -max_value,
'max': max_value,
'step': step
}
elif data_type == 'smallint':
constraints = {'min': -32768, 'max': 32767}
elif data_type == 'integer':
constraints = {'min': -2147483648, 'max': 2147483647}
elif data_type == 'bigint':
constraints = {
'min': '-9223372036854775808',
'max': '9223372036854775807'
}
if attr not in permissions['fields']:
permissions['fields'][attr] = {}
if data_type:
# add data type
permissions['fields'][attr]['data_type'] = data_type
else:
self.logger.warn(
"Could not find data type of column '%s' "
"of table '%s.%s'" % (attr, schema, table_name)
)
if constraints:
if 'constraints' in permissions['fields'][attr]:
# merge constraints from QGIS project
constraints.update(
permissions['fields'][attr]['constraints']
)
# add constraints
permissions['fields'][attr]['constraints'] = constraints
# close database connection
conn.close()
except Exception as e:
self.logger.error(
"Error while querying attribute data types:\n\n%s" % e
)
raise