Verified Commit 977e9254 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add admin group to NextCloud api

parent 54be451b
...@@ -3,6 +3,8 @@ from django.urls import path ...@@ -3,6 +3,8 @@ from django.urls import path
from activemembers.api import views from activemembers.api import views
urlpatterns = [ urlpatterns = [
path('activemembers/nextcloud/users', views.NextCloudUsersView.as_view()), path('activemembers/nextcloud/users/',
path('activemembers/nextcloud/groups', views.NextCloudGroupsView.as_view()), views.NextCloudUsersView.as_view()),
path('activemembers/nextcloud/groups/',
views.NextCloudGroupsView.as_view()),
] ]
from django.contrib.auth.models import Permission
from django.db.models import Q
from django.utils import timezone
from rest_framework.generics import ListAPIView from rest_framework.generics import ListAPIView
from activemembers.api.permissions import NextCloudPermission from activemembers.api.permissions import NextCloudPermission
...@@ -11,14 +14,48 @@ from members.models import Member ...@@ -11,14 +14,48 @@ from members.models import Member
class NextCloudUsersView(ListAPIView): class NextCloudUsersView(ListAPIView):
permission_classes = [NextCloudPermission] permission_classes = [NextCloudPermission]
queryset = Member.current_members.filter( queryset = Member.current_members.all()
pk__in=MemberGroupMembership.active_objects.values_list(
'member_id', flat=True)
)
serializer_class = NextCloudMemberSerializer serializer_class = NextCloudMemberSerializer
def get_queryset(self):
perm = Permission.objects.get(content_type__app_label='auth',
codename='nextcloud_admin')
return super().get_queryset().filter(
Q(pk__in=MemberGroupMembership.active_objects.values_list(
'member_id', flat=True)) |
Q(is_superuser=True) |
Q(groups__permissions=perm) |
Q(user_permissions=perm) |
(Q(membergroup__permissions=perm) &
(Q(membergroupmembership__until=None) |
Q(membergroupmembership__until__gte=timezone.now())))
).distinct()
class NextCloudGroupsView(ListAPIView): class NextCloudGroupsView(ListAPIView):
permission_classes = [NextCloudPermission] permission_classes = [NextCloudPermission]
queryset = MemberGroup.objects.all() queryset = MemberGroup.objects.all()
serializer_class = NextCloudGroupSerializer serializer_class = NextCloudGroupSerializer
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
perm = Permission.objects.get(content_type__app_label='auth',
codename='nextcloud_admin')
users = Member.current_members.filter(
Q(is_superuser=True) |
Q(groups__permissions=perm) |
Q(user_permissions=perm) |
(Q(membergroup__permissions=perm) &
(Q(membergroupmembership__until=None) |
Q(membergroupmembership__until__gte=timezone.now())))
).distinct().values_list('username', flat=True)
if users.count() > 0:
response.data = list(response.data) + [
{
'pk': -1,
'name': 'admin',
'members': users
}
]
return response
...@@ -7,8 +7,8 @@ msgid "" ...@@ -7,8 +7,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-11-14 19:57+0100\n" "POT-Creation-Date: 2018-12-17 15:31+0100\n"
"PO-Revision-Date: 2018-11-14 19:58+0100\n" "PO-Revision-Date: 2018-12-17 15:32+0100\n"
"Last-Translator: Thom Wiggers <thom@thomwiggers.nl>\n" "Last-Translator: Thom Wiggers <thom@thomwiggers.nl>\n"
"Language-Team: \n" "Language-Team: \n"
"Language: nl\n" "Language: nl\n"
...@@ -150,6 +150,10 @@ msgstr "E-mailadres" ...@@ -150,6 +150,10 @@ msgstr "E-mailadres"
msgid "Access the Sentry backend" msgid "Access the Sentry backend"
msgstr "Toegang tot the Sentry backend" msgstr "Toegang tot the Sentry backend"
#: models.py
msgid "Access NextCloud as admin"
msgstr "Toegang tot NextCloud als admin"
#: models.py #: models.py
msgid "Is this user currently active" msgid "Is this user currently active"
msgstr "Is deze user op dit moment actief" msgstr "Is deze user op dit moment actief"
......
# Generated by Django 2.1.4 on 2018-12-17 14:18
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('members', '0027_auto_20181024_2000'),
]
operations = [
migrations.AlterModelOptions(
name='member',
options={'ordering': ('first_name', 'last_name'), 'permissions': (('sentry_access', 'Access the Sentry backend'), ('nextcloud_admin', 'Access NextCloud as admin'))},
),
]
...@@ -2,8 +2,10 @@ import logging ...@@ -2,8 +2,10 @@ import logging
import operator import operator
import os import os
import uuid import uuid
from PIL import Image
from datetime import timedelta from datetime import timedelta
from functools import reduce
from PIL import Image
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User, UserManager from django.contrib.auth.models import User, UserManager
from django.core import validators from django.core import validators
...@@ -13,7 +15,6 @@ from django.db.models import Q ...@@ -13,7 +15,6 @@ from django.db.models import Q
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import pgettext_lazy, gettext_lazy as _ from django.utils.translation import pgettext_lazy, gettext_lazy as _
from functools import reduce
from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES
from localflavor.generic.models import IBANField from localflavor.generic.models import IBANField
...@@ -97,6 +98,7 @@ class Member(User): ...@@ -97,6 +98,7 @@ class Member(User):
ordering = ('first_name', 'last_name') ordering = ('first_name', 'last_name')
permissions = ( permissions = (
('sentry_access', _("Access the Sentry backend")), ('sentry_access', _("Access the Sentry backend")),
('nextcloud_admin', _("Access NextCloud as admin")),
) )
objects = MemberManager() objects = MemberManager()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment