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
/
config_service.py
167 lines (137 loc) · 5.54 KB
/
config_service.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
from datetime import datetime
import os
from qwc_config_db.config_models import ConfigModels
from qwc_services_core.database import DatabaseEngine
from data_service_permission import DataServicePermission
from ogc_service_permission import OGCServicePermission
from qwc2_viewer_permission import QWC2ViewerPermission
from resource_permission import ResourcePermission
class ConfigService:
"""ConfigService class
Query service specific permissions.
"""
def __init__(self, logger):
"""Constructor
:param Logger logger: Application logger
"""
self.logger = logger
self.db_engine = DatabaseEngine()
self.config_models = ConfigModels(self.db_engine)
default_allow = os.environ.get(
'DEFAULT_ALLOW', 'True') == 'True'
data_permission_handler = DataServicePermission(
self.db_engine, self.config_models, logger
)
self.project_settings_cache = {}
ogc_permission_handler = OGCServicePermission(
default_allow, self.config_models, logger, self.project_settings_cache
)
qwc_permission_handler = QWC2ViewerPermission(
ogc_permission_handler, data_permission_handler,
default_allow, self.config_models, logger,
self.project_settings_cache
)
self.permission_handlers = {
'data': data_permission_handler,
'ogc': ogc_permission_handler,
'qwc': qwc_permission_handler
}
self.resource_permission_handler = ResourcePermission(
self.config_models, logger
)
# get path to QWC2 themes config from ENV
qwc2_path = os.environ.get('QWC2_PATH', 'qwc2/')
self.themes_config_path = os.environ.get(
'QWC2_THEMES_CONFIG', os.path.join(qwc2_path, 'themesConfig.json')
)
if os.environ.get("__QWC_CONFIG_SERVICE_PROJECT_SETTINGS_STARTUP_CACHE", "0") == "1":
self.cache_project_settings()
def last_update(self):
"""Return UTC timestamp of last permissions update."""
# get modification time of QWC2 themes config file
config_updated_at = None
if os.path.isfile(self.themes_config_path):
config_updated_at = datetime.utcfromtimestamp(
os.path.getmtime(self.themes_config_path)
)
# create session for ConfigDB
session = self.config_models.session()
# query timestamp
LastUpdate = self.config_models.model('last_update')
query = session.query(LastUpdate.updated_at)
last_update = query.first()
if last_update is not None:
if config_updated_at is not None:
# use latest of both timestamps
updated_at = max(last_update.updated_at, config_updated_at)
else:
# use timestamp from ConfigDB
updated_at = last_update.updated_at
else:
# no entry in ConfigDB, use config timestamp or now
updated_at = config_updated_at or datetime.utcnow()
# close session
session.close()
return {
'permissions_updated_at': updated_at.strftime("%Y-%m-%d %H:%M:%S")
}
def service_permissions(self, service, params, username, group):
"""Return permissions for a service and a dataset.
:param str service: Service type
:param obj params: Service specific request parameters
:param str username: User name
:param str group: Group name
"""
permission_handler = self.permission_handlers.get(service, None)
if permission_handler is not None:
# create session for ConfigDB
session = self.config_models.session()
# query permissions
permissions = permission_handler.permissions(
params, username, group, session
)
# close session
session.close()
return {
'permissions': permissions
}
else:
return {'error': "Service type '%s' not found" % service}
def resource_permissions(self, resource_type, params, username, group):
"""Return permitted resources for a resource type.
:param str resource_type: Resource type
:param obj params: Request parameters
:param str username: User name
:param str group: Group name
"""
# create session for ConfigDB
session = self.config_models.session()
# query permitted resources
permissions = self.resource_permission_handler.permissions(
resource_type, params, username, group, session
)
# close session
session.close()
return {
'permissions': permissions
}
def resource_restrictions(self, resource_type, params, username, group):
"""Return restricted resources for a resource type.
:param str resource_type: Resource type
:param obj params: Request parameters
:param str username: User name
:param str group: Group name
"""
# create session for ConfigDB
session = self.config_models.session()
# query restricted resources
restrictions = self.resource_permission_handler.restrictions(
resource_type, params, username, group, session
)
# close session
session.close()
return {
'restrictions': restrictions
}
def cache_project_settings(self):
return self.permission_handlers["ogc"].cache_project_settings()