Repository navigation
Description
GNIP 105 - Per-user layer filtering in MapStore2 viewer based on group permissions
Overview
Currently, when a GeoNode map contains layers with different access levels, MapStore2 displays all layers in the TOC regardless of the logged-in user's group membership. A user without permission to view a restricted layer still sees it in the layer tree (with a load error), instead of not seeing it at all.
This proposal adds an opt-in, configurable per-user layer filtering mechanism to the map viewer endpoints so that the TOC reflects only the layers each user is authorized to see, leveraging GeoNode's existing guardian permission system.
Proposed By
@vlasvlasvlas — Vladimiro Bellini
Assigned to Release
This proposal is for GeoNode 5.x
State
- Under Discussion
- In Progress
- Completed
- Rejected
- Deferred
Motivation
GeoNode's guardian permission system correctly restricts access to datasets at
the catalog, search, and dataset API level. However, this restrictio
applied when serving the layer list for a map viewer. The current design assumes:
"if you can access the map, you can see all its layers."
This assumption breaks down in any deployment where a single map/vie
users with different data access levels — a common scenario in government,
municipal, and enterprise contexts where one portal serves multiple
with different data confidentiality requirements.
Current workaround: Custom proxy views that intercept the map API endpoints
and filter layers manually. This works but lives outside GeoNode's c
requires manual maintenance for each new restricted layer.
Benefits of a native opt-in implementation:
- Off by default — zero impact on existing deployments
- When enabled, administrators manage layer visibility through GeoNode's
existing UI (Dataset → Share → assign group) with no code changes - Consistent behavior: the same permission that restricts a dataset in the
catalog also restricts it in the viewer TOC - GetFeatureInfo restriction follows automatically — hidden layers are never
loaded in the client, so MapStore2 never sends identify requests f - Scales to N layers × N groups with no additional configuration
- Multi-group users see the union of all layers their groups are ent
Proposal
Configuration
Two new settings in settings.py:
# Enable per-user layer filtering in MapStore2 viewer (default: False)
MAPSTORE_LAYER_GROUP_FILTER = False
# Groups considered "public" — layers visible to these groups are no
# Deployments with custom group conventions can override this list.
MAPSTORE_PUBLIC_GROUPS = ['anonymous', 'registered-members', 'contriWhen MAPSTORE_LAYER_GROUP_FILTER = False (the default), behavior is
to the current implementation. No existing deployment is affected.
When set to True, the map viewer endpoints dynamically filter the layer list
based on the requesting user's group membership and guardian permiss
Root cause
MapStore2 initializes the layer tree by calling two endpoints:
GET /api/v2/maps// → response.data.map.maplayers
GET /api/v2/maps//maplayers/ → response.data (layer list)
Both return the full map configuration without filtering by the requesting
user's permissions (MapViewSet.retrieve and MapViewSet.maplayers).
Implementation
When MAPSTORE_LAYER_GROUP_FILTER = True, add a filtering step to bot
endpoints that reads guardian.GroupObjectPermission to determine which
datasets are restricted and to which groups.
Convention: a dataset with view_dataset assigned to at least one gro
not in MAPSTORE_PUBLIC_GROUPS is treated as restricted to those groups.
Datasets with no such assignments are public and visible to all.
from django.conf import settings
from django.core.cache import cache
from django.db.models.signals import post_save, post_delete
from guardian.models import GroupObjectPermission
_CACHE_KEY = 'geonode_mapstore_restricted_layers'
_CACHE_TTL = 300 # seconds
def _get_restricted_layers():
"""
Read from guardian which datasets have view_dataset assigned to
non-public groups. Result is cached and auto-invalidated on perm changes.
"""
public_groups = set(
getattr(settings, 'MAPSTORE_PUBLIC_GROUPS',
['anonymous', 'registered-members', 'contributors'])
)
cached = cache.get(_CACHE_KEY)
if cached is not None:
return cached
from geonode.layers.models import Dataset
from guardian.shortcuts import get_groups_with_perms
result = {}
for ds in Dataset.objects.filter(is_published=True).iterator():
groups_with_view = {
g.name
for g, perms in get_groups_with_perms(ds, attach_perms=T
if 'view_dataset' in perms and g.name not in public_groups
}
if groups_with_view:
result[ds.alternate] = list(groups_with_view)
cache.set(_CACHE_KEY, result, _CACHE_TTL)
return result
def _invalidate_layer_cache(sender, **kwargs):
cache.delete(_CACHE_KEY)
post_save.connect(_invalidate_layer_cache, sender=GroupObjectPermiss
post_delete.connect(_invalidate_layer_cache, sender=GroupObjectPermission)
def _can_see_layer(user, layer_name, restricted):
if layer_name not in restricted:
return True # not restricted → public
if getattr(user, 'is_superuser', False) or getattr(user, 'is_staff', False):
return True # superuser/staff always see everything
allowed = set(restricted[layer_name])
user_groups = set(user.groups.values_list('name', flat=True))
return bool(user_groups.intersection(allowed))
# In MapViewSet.retrieve and MapViewSet.maplayers:
if getattr(settings, 'MAPSTORE_LAYER_GROUP_FILTER', False):
restricted = _get_restricted_layers()
layers_to_hide = {
name for name in restricted
if not _can_see_layer(request.user, name, restricted)
}
# filter response maplayers accordingly
Related bug: stale OAuth2 token overrides valid session
During implementation a related issue was found: when a user's OAuth2 Bearer
token expires, MapStore2 continues sending it in the Authorization h
If the server removes the stale token and unconditionally sets
request.user = AnonymousUser(), it overrides a still-valid Django se
causing authenticated users (including superusers) to lose access to restricted
layers until they re-login.
Fix: check for an active Django session before falling back to Anony
if not valid_token:
request.META.pop('HTTP_AUTHORIZATION', None)
already_auth = (
request.user is not None
and not isinstance(request.user, AnonymousUser)
and bool(getattr(request.user, 'is_authenticated', False))
)
if not already_auth:
request.user = AnonymousUser()
Backwards Compatibility
Fully backwards compatible. MAPSTORE_LAYER_GROUP_FILTER defaults to
preserving current behavior for all existing deployments. The feature activates
only when explicitly enabled and when an administrator assigns group
view_dataset permissions to specific datasets — both are deliberate actions.
Future evolution
- Per-map setting: expose the filter as a per-map configuration option
in addition to the global setting, allowing mixed behavior within
GeoNode instance. - GeoServer security rules sync: automatically propagate GeoNode gro
permissions to GeoServer layer security rules, closing the remaining bypass
vector for users who construct WMS/WFS URLs manually. - Real-time cache invalidation: replace TTL-based cache with signal-driven
invalidation for immediate effect when permissions change in high-
deployments.
Feedback
(to be updated)
Voting
Project Steering Committee:
- Alessio Fabiani:
- Francesco Bartoli:
- Giovanni Allegri:
- Stefano Bovio
Links
- Email Discussion
- Pull Request
- Mail Discussion
- Linked Issue
Activity
giohappy commentedon Sep 24, 2026
Thanks, @vlasvlasvlas, for your proposal.
The idea can be discussed, and I'm personally not against it.
The proposed implementation must be extended and refined. First of all, permissions on layers need to go through the Permissions registry methods, rather than using Guardian directly.
And I'm not sure that just filtering mapLayers is enough to remove layers from the TOC. The TOC is built from the Map config. Permissions are only used to activate or not certain tools (editing, download, etc.).