diff --git a/_1327/documents/utils.py b/_1327/documents/utils.py index 07e029fe..6dc6f589 100644 --- a/_1327/documents/utils.py +++ b/_1327/documents/utils.py @@ -4,10 +4,14 @@ from django.conf import settings +from django.contrib.auth.models import Group from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import SuspiciousOperation from django.db import transaction +from django.shortcuts import Http404 from django.utils import timezone +from guardian.core import ObjectPermissionChecker from reversion import revisions from reversion.models import Version @@ -174,3 +178,40 @@ def delete_cascade_to_json(cascade): "name": str(cascade_item), }) return items + + +def get_permitted_documents(documents, request, groupid): + groupid = int(groupid) + try: + group = Group.objects.get(id=groupid) + except ObjectDoesNotExist: + raise Http404 + + own_group = request.user.is_superuser or group in request.user.groups.all() + + # Prefetch group permissions + group_checker = ObjectPermissionChecker(group) + group_checker.prefetch_perms(documents) + + # Prefetch user permissions + user_checker = ObjectPermissionChecker(request.user) + user_checker.prefetch_perms(documents) + + # Prefetch ip-range group permissions + ip_range_group_name = getattr(request.user, '_ip_range_group_name', None) + if ip_range_group_name: + ip_range_group = Group.objects.get(name=ip_range_group_name) + ip_range_group_checker = ObjectPermissionChecker(ip_range_group) + + permitted_documents = [] + for document in documents: + # we show all documents for which the requested group has edit permissions + # e.g. if you request FSR documents, all documents for which the FSR group has edit rights will be shown + if not group_checker.has_perm(document.edit_permission_name, document): + continue + # we only show documents for which the user has view permissions + if not user_checker.has_perm(Document.get_view_permission(), document) and (not ip_range_group_name or not ip_range_group_checker.has_perm(Document.get_view_permission(), document)): + continue + permitted_documents.append(document) + + return permitted_documents, own_group diff --git a/_1327/information_pages/migrations/0004_auto_20180813_2050_squashed_0005_auto_20180813_2102.py b/_1327/information_pages/migrations/0004_auto_20180813_2050_squashed_0005_auto_20180813_2102.py new file mode 100644 index 00000000..8e98c443 --- /dev/null +++ b/_1327/information_pages/migrations/0004_auto_20180813_2050_squashed_0005_auto_20180813_2102.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.15 on 2018-08-13 19:06 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + replaces = [('information_pages', '0004_auto_20180813_2050'), ('information_pages', '0005_auto_20180813_2102')] + + dependencies = [ + ('information_pages', '0003_auto_20180201_2301'), + ] + + operations = [ + migrations.AlterModelOptions( + name='informationdocument', + options={'base_manager_name': 'objects', 'permissions': (('view_informationdocument', 'User/Group is allowed to view that document'),), 'verbose_name': 'Information document', 'verbose_name_plural': 'Information documents'}, + ), + ] diff --git a/_1327/main/templates/menu_item_edit.html b/_1327/main/templates/menu_item_edit.html index b528f610..d767bbc5 100644 --- a/_1327/main/templates/menu_item_edit.html +++ b/_1327/main/templates/menu_item_edit.html @@ -13,7 +13,7 @@ {% endblock %} {% block content %} -