2020-07-01 19:18:05 +00:00
|
|
|
"""user field matcher models"""
|
2020-07-20 13:58:48 +00:00
|
|
|
from typing import Type
|
|
|
|
|
2020-07-01 19:18:05 +00:00
|
|
|
from django.db import models
|
2020-07-20 13:58:48 +00:00
|
|
|
from django.forms import ModelForm
|
2020-07-01 19:18:05 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-08-21 22:42:15 +00:00
|
|
|
from rest_framework.serializers import BaseSerializer
|
2020-07-01 19:18:05 +00:00
|
|
|
|
|
|
|
from passbook.core.models import Group
|
|
|
|
from passbook.policies.models import Policy
|
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
|
|
|
|
|
|
|
|
|
|
|
class GroupMembershipPolicy(Policy):
|
|
|
|
"""Check that the user is member of the selected group."""
|
|
|
|
|
|
|
|
group = models.ForeignKey(Group, null=True, blank=True, on_delete=models.SET_NULL)
|
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
@property
|
|
|
|
def serializer(self) -> BaseSerializer:
|
|
|
|
from passbook.policies.group_membership.api import (
|
|
|
|
GroupMembershipPolicySerializer,
|
|
|
|
)
|
|
|
|
|
|
|
|
return GroupMembershipPolicySerializer
|
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:58:48 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.policies.group_membership.forms import GroupMembershipPolicyForm
|
|
|
|
|
|
|
|
return GroupMembershipPolicyForm
|
2020-07-01 19:18:05 +00:00
|
|
|
|
|
|
|
def passes(self, request: PolicyRequest) -> PolicyResult:
|
2020-09-15 20:37:31 +00:00
|
|
|
return PolicyResult(self.group.users.filter(pk=request.user.pk).exists())
|
2020-07-01 19:18:05 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _("Group Membership Policy")
|
|
|
|
verbose_name_plural = _("Group Membership Policies")
|