2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2020-03-18 13:42:35 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-07-18 11:51:56 +00:00
|
|
|
import re
|
|
|
|
|
2020-03-18 13:42:35 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2020-09-02 17:00:04 +00:00
|
|
|
from django_filters.rest_framework import DjangoFilterBackend
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from rest_framework.viewsets import ReadOnlyModelViewSet, ModelViewSet
|
2020-03-20 13:43:35 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-09-02 17:00:04 +00:00
|
|
|
from note.models import Alias
|
2020-03-20 00:46:59 +00:00
|
|
|
|
2022-03-10 15:11:01 +00:00
|
|
|
from .filters import RegexSafeSearchFilter
|
2020-09-02 17:00:04 +00:00
|
|
|
from .serializers import UserSerializer, ContentTypeSerializer
|
2020-03-18 13:42:35 +00:00
|
|
|
|
2020-09-02 17:00:04 +00:00
|
|
|
|
2024-07-18 11:51:56 +00:00
|
|
|
def is_regex(pattern):
|
|
|
|
try:
|
|
|
|
re.compile(pattern)
|
|
|
|
return True
|
|
|
|
except (re.error, TypeError):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-02 17:00:04 +00:00
|
|
|
class ReadProtectedModelViewSet(ModelViewSet):
|
2020-03-18 13:42:35 +00:00
|
|
|
"""
|
|
|
|
Protect a ModelViewSet by filtering the objects that the user cannot see.
|
|
|
|
"""
|
|
|
|
|
2020-03-20 00:46:59 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-07-30 13:07:30 +00:00
|
|
|
self.model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2021-06-15 12:40:32 +00:00
|
|
|
return self.queryset.filter(PermissionBackend.filter_queryset(self.request, self.model, "view")).distinct()
|
2020-03-18 13:42:35 +00:00
|
|
|
|
|
|
|
|
2020-09-02 17:00:04 +00:00
|
|
|
class ReadOnlyProtectedModelViewSet(ReadOnlyModelViewSet):
|
2020-03-18 13:42:35 +00:00
|
|
|
"""
|
|
|
|
Protect a ReadOnlyModelViewSet by filtering the objects that the user cannot see.
|
|
|
|
"""
|
|
|
|
|
2020-03-20 00:46:59 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-07-30 13:07:30 +00:00
|
|
|
self.model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2021-06-15 12:40:32 +00:00
|
|
|
return self.queryset.filter(PermissionBackend.filter_queryset(self.request, self.model, "view")).distinct()
|
2020-09-02 17:00:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserViewSet(ReadProtectedModelViewSet):
|
|
|
|
"""
|
|
|
|
REST API View set.
|
|
|
|
The djangorestframework plugin will get all `User` objects, serialize it to JSON with the given serializer,
|
2020-12-23 13:54:21 +00:00
|
|
|
then render it on /api/user/
|
2020-09-02 17:00:04 +00:00
|
|
|
"""
|
2020-12-23 13:54:21 +00:00
|
|
|
queryset = User.objects
|
2020-09-02 17:00:04 +00:00
|
|
|
serializer_class = UserSerializer
|
|
|
|
filter_backends = [DjangoFilterBackend]
|
2020-12-23 13:54:21 +00:00
|
|
|
filterset_fields = ['id', 'username', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active',
|
|
|
|
'note__alias__name', 'note__alias__normalized_name', ]
|
2020-09-02 17:00:04 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
# Sqlite doesn't support ORDER BY in subqueries
|
|
|
|
queryset = queryset.order_by("username") \
|
|
|
|
if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' else queryset
|
|
|
|
|
|
|
|
if "search" in self.request.GET:
|
|
|
|
pattern = self.request.GET["search"]
|
2024-07-18 11:51:56 +00:00
|
|
|
# Check if this is a valid regex. If not, we won't check regex
|
|
|
|
valid_regex = is_regex(pattern)
|
|
|
|
suffix = "__iregex" if valid_regex else "__istartswith"
|
|
|
|
prefix = "^" if valid_regex else ""
|
2020-09-03 19:21:09 +00:00
|
|
|
|
2020-09-03 19:47:08 +00:00
|
|
|
# Filter with different rules
|
|
|
|
# We use union-all to keep each filter rule sorted in result
|
2020-09-02 17:00:04 +00:00
|
|
|
queryset = queryset.filter(
|
2020-09-03 19:47:08 +00:00
|
|
|
# Match without normalization
|
2024-07-18 11:51:56 +00:00
|
|
|
Q(**{f"note__alias__name{suffix}": prefix + pattern})
|
2020-09-03 19:47:08 +00:00
|
|
|
).union(
|
|
|
|
queryset.filter(
|
|
|
|
# Match with normalization
|
2024-07-18 11:51:56 +00:00
|
|
|
Q(**{f"note__alias__normalized_name{suffix}": prefix + Alias.normalize(pattern)})
|
|
|
|
& ~Q(**{f"note__alias__name{suffix}": prefix + pattern})
|
2020-09-03 19:47:08 +00:00
|
|
|
),
|
|
|
|
all=True,
|
|
|
|
).union(
|
|
|
|
queryset.filter(
|
|
|
|
# Match on lower pattern
|
2024-07-18 11:51:56 +00:00
|
|
|
Q(**{f"note__alias__normalized_name{suffix}": prefix + pattern.lower()})
|
|
|
|
& ~Q(**{f"note__alias__normalized_name{suffix}": prefix + Alias.normalize(pattern)})
|
|
|
|
& ~Q(**{f"note__alias__name{suffix}": prefix + pattern})
|
2020-09-03 19:47:08 +00:00
|
|
|
),
|
|
|
|
all=True,
|
|
|
|
).union(
|
|
|
|
queryset.filter(
|
|
|
|
# Match on firstname or lastname
|
2024-07-18 11:51:56 +00:00
|
|
|
(Q(**{f"last_name{suffix}": prefix + pattern}) | Q(**{f"first_name{suffix}": prefix + pattern}))
|
|
|
|
& ~Q(**{f"note__alias__normalized_name{suffix}": prefix + pattern.lower()})
|
|
|
|
& ~Q(**{f"note__alias__normalized_name{suffix}": prefix + Alias.normalize(pattern)})
|
|
|
|
& ~Q(**{f"note__alias__name{suffix}": prefix + pattern})
|
2020-09-03 19:47:08 +00:00
|
|
|
),
|
|
|
|
all=True,
|
|
|
|
)
|
2020-09-02 17:00:04 +00:00
|
|
|
|
|
|
|
queryset = queryset if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' \
|
|
|
|
else queryset.order_by("username")
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
# This ViewSet is the only one that is accessible from all authenticated users!
|
|
|
|
class ContentTypeViewSet(ReadOnlyModelViewSet):
|
|
|
|
"""
|
|
|
|
REST API View set.
|
|
|
|
The djangorestframework plugin will get all `User` objects, serialize it to JSON with the given serializer,
|
2020-12-23 13:54:21 +00:00
|
|
|
then render it on /api/models/
|
2020-09-02 17:00:04 +00:00
|
|
|
"""
|
2020-12-23 13:54:21 +00:00
|
|
|
queryset = ContentType.objects.order_by('id')
|
2020-09-02 17:00:04 +00:00
|
|
|
serializer_class = ContentTypeSerializer
|
2022-03-10 15:11:01 +00:00
|
|
|
filter_backends = [DjangoFilterBackend, RegexSafeSearchFilter]
|
2020-12-23 13:54:21 +00:00
|
|
|
filterset_fields = ['id', 'app_label', 'model', ]
|
|
|
|
search_fields = ['$app_label', '$model', ]
|