This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2019-10-07 14:33:48 +00:00
|
|
|
"""passbook group models models"""
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from passbook.core.models import Group, Policy
|
|
|
|
from passbook.policies.struct import PolicyRequest, PolicyResult
|
|
|
|
|
|
|
|
|
|
|
|
class GroupMembershipPolicy(Policy):
|
|
|
|
"""Policy to check if the user is member in a certain group"""
|
|
|
|
|
|
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
form = "passbook.policies.group.forms.GroupMembershipPolicyForm"
|
2019-10-07 14:33:48 +00:00
|
|
|
|
|
|
|
def passes(self, request: PolicyRequest) -> PolicyResult:
|
|
|
|
return PolicyResult(self.group.user_set.filter(pk=request.user.pk).exists())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Group Membership Policy")
|
|
|
|
verbose_name_plural = _("Group Membership Policies")
|