2020-05-07 18:51:06 +00:00
|
|
|
"""Policy base models"""
|
2020-07-20 16:17:14 +00:00
|
|
|
from typing import Type
|
2020-05-20 07:17:06 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
from django.db import models
|
2020-07-20 16:17:14 +00:00
|
|
|
from django.forms import ModelForm
|
2020-05-07 18:51:06 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-16 16:07:00 +00:00
|
|
|
from model_utils.managers import InheritanceManager
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
from passbook.lib.models import (
|
|
|
|
CreatedUpdatedModel,
|
|
|
|
InheritanceAutoManager,
|
|
|
|
InheritanceForeignKey,
|
|
|
|
)
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.policies.exceptions import PolicyException
|
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingModel(models.Model):
|
2020-05-14 11:51:05 +00:00
|
|
|
"""Base Model for objects that have policies applied to them."""
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
pbm_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-08 16:45:53 +00:00
|
|
|
policies = models.ManyToManyField(
|
2020-05-16 17:00:43 +00:00
|
|
|
"Policy", through="PolicyBinding", related_name="bindings", blank=True
|
2020-05-08 16:45:53 +00:00
|
|
|
)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-16 17:55:59 +00:00
|
|
|
objects = InheritanceManager()
|
|
|
|
|
2020-05-08 12:33:14 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Policy Binding Model")
|
|
|
|
verbose_name_plural = _("Policy Binding Models")
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
class PolicyBinding(models.Model):
|
2020-05-07 18:51:06 +00:00
|
|
|
"""Relationship between a Policy and a PolicyBindingModel."""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
policy_binding_uuid = models.UUIDField(
|
|
|
|
primary_key=True, editable=False, default=uuid4
|
|
|
|
)
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
enabled = models.BooleanField(default=True)
|
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
policy = InheritanceForeignKey("Policy", on_delete=models.CASCADE, related_name="+")
|
2020-05-07 18:51:06 +00:00
|
|
|
target = models.ForeignKey(
|
|
|
|
PolicyBindingModel, on_delete=models.CASCADE, related_name="+"
|
|
|
|
)
|
2020-05-28 19:45:54 +00:00
|
|
|
negate = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
help_text=_("Negates the outcome of the policy. Messages are unaffected."),
|
|
|
|
)
|
|
|
|
timeout = models.IntegerField(
|
|
|
|
default=30, help_text=_("Timeout after which Policy execution is terminated.")
|
|
|
|
)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
order = models.IntegerField()
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-08 12:33:14 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"PolicyBinding policy={self.policy} target={self.target} order={self.order}"
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _("Policy Binding")
|
|
|
|
verbose_name_plural = _("Policy Bindings")
|
2020-05-28 19:45:54 +00:00
|
|
|
unique_together = ("policy", "target", "order")
|
2020-05-16 16:07:00 +00:00
|
|
|
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
class Policy(CreatedUpdatedModel):
|
2020-05-16 16:07:00 +00:00
|
|
|
"""Policies which specify if a user is authorized to use an Application. Can be overridden by
|
|
|
|
other types to add other fields, more logic, etc."""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
policy_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-16 16:07:00 +00:00
|
|
|
name = models.TextField(blank=True, null=True)
|
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
objects = InheritanceAutoManager()
|
2020-05-16 16:07:00 +00:00
|
|
|
|
2020-07-20 16:17:14 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
"""Return Form class used to edit this object"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-05-16 16:07:00 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"Policy {self.name}"
|
|
|
|
|
|
|
|
def passes(self, request: PolicyRequest) -> PolicyResult:
|
|
|
|
"""Check if user instance passes this policy"""
|
|
|
|
raise PolicyException()
|
2020-05-28 19:45:54 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
base_manager_name = "objects"
|
|
|
|
|
|
|
|
verbose_name = _("Policy")
|
|
|
|
verbose_name_plural = _("Policies")
|