Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 334bd0a

Browse files
committed
add support for themes config per viewer
1 parent 3534a8c commit 334bd0a

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The `write` flag is only used for `data` resources and sets whether a data layer
6161
Based on the user's identity (user name and/or group name), all corresponding roles and their permissions and restrictions are collected.
6262
The service configurations are then modified according to these permissions and restrictions.
6363

64-
Using the `DEFAULT_ALLOW` environment variable, some resources can be set to be permitted or restricted by default if no permissions are set (default: `False`). Affected resources are `map`, `layer`, `print_template` and `viewer_task`.
64+
Using the `DEFAULT_ALLOW` environment variable, some resources can be set to be permitted or restricted by default if no permissions are set (default: `True`). Affected resources are `map`, `layer`, `print_template` and `viewer_task`.
6565

6666
e.g. `DEFAULT_ALLOW=True`: all maps and layers are permitted by default
6767
e.g. `DEFAULT_ALLOW=False`: maps and layers are only available if their resources and permissions are explicitly configured
@@ -125,6 +125,7 @@ Set the `QGIS_RESOURCES_PATH` environment variable to your QGIS project files pa
125125

126126
Set the `QWC2_PATH` environment variable to your QWC2 files path.
127127
Set the `QWC2_THEMES_CONFIG` environment variable to your QWC2 `themesConfig.json` path if it is not located in `$QWC2_PATH`.
128+
Set the `QWC2_VIEWERS_PATH` environment variable to your QWC2 custom viewers path (default: `$QWC2_PATH/viewers/`) (see [Custom viewer configurations](https://github.com/qwc-services/qwc-map-viewer#custom-viewer-configurations)).
128129

129130
Base URL:
130131

qwc2_viewer_permission.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from flask import json
2+
from flask import json, safe_join
33
from werkzeug.urls import url_parse
44

55
from permission_query import PermissionQuery
@@ -64,6 +64,11 @@ def __init__(self, ogc_permission_handler, data_permission_handler,
6464
'QWC2_THEMES_CONFIG', os.path.join(qwc2_path, 'themesConfig.json')
6565
)
6666

67+
# get path for custom viewer themes configs from ENV
68+
self.viewers_path = os.environ.get(
69+
'QWC2_VIEWERS_PATH', os.path.join(qwc2_path, 'viewers')
70+
)
71+
6772
# get internal QGIS server URL from ENV
6873
qgis_server_url = os.environ.get('QGIS_SERVER_URL',
6974
'http://localhost/wms/').rstrip('/') + '/'
@@ -79,20 +84,53 @@ def permissions(self, params, username, group, session):
7984
:param str group: Group name
8085
:param Session session: DB session
8186
'''
87+
viewer = params.get('viewer')
88+
89+
# get viewer permissions
90+
viewer_permissions = self.viewer_permissions(username, group, session)
91+
8292
# get themes from QWC2 themes config
83-
with open(self.themes_config_path, encoding='utf-8') as fh:
84-
config = json.load(fh)
93+
themes_config_path = None
94+
config = None
95+
if viewer and viewer in viewer_permissions:
96+
# try to load custom viewer themes config '<viewer>.json'
97+
filename = '%s.json' % viewer
98+
themes_config_path = safe_join(self.viewers_path, '%s' % filename)
99+
try:
100+
self.logger.debug(
101+
"Using custom viewer themes config '%s'" % filename
102+
)
103+
with open(themes_config_path, encoding='utf-8') as fh:
104+
config = json.load(fh)
105+
except Exception as e:
106+
self.logger.error(
107+
"Could not load custom viewer themes config '%s':\n%s" %
108+
(filename, e)
109+
)
110+
# fallback to default themes config
111+
112+
if config is None:
113+
# load default themes config
114+
try:
115+
themes_config_path = self.themes_config_path
116+
with open(themes_config_path, encoding='utf-8') as fh:
117+
config = json.load(fh)
118+
except Exception as e:
119+
self.logger.error(
120+
"Could not load default themes config:\n%s" % e
121+
)
122+
return jsonify({"error": "Unable to read themesConfig.json"})
85123

86124
# query WMS permissions for each theme
87125
permissions = {}
88126
self.themes_group_permissions(
89127
config.get('themes', {}), permissions, username, group, session
90128
)
91129

92-
result = genThemes(self.themes_config_path, permissions)
130+
result = genThemes(themes_config_path, permissions)
93131

94132
# add viewer permissions
95-
result['viewers'] = self.viewer_permissions(username, group, session)
133+
result['viewers'] = viewer_permissions
96134

97135
# add viewer task permissions
98136
result['viewer_tasks'] = self.viewer_task_permissions(

0 commit comments

Comments
 (0)