2020-05-07 19:51:06 +01:00
|
|
|
"""Policy base models"""
|
2020-05-20 08:17:06 +01:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2020-05-07 19:51:06 +01:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-16 17:07:00 +01:00
|
|
|
from model_utils.managers import InheritanceManager
|
2020-05-07 19:51:06 +01:00
|
|
|
|
2020-05-28 20:45:54 +01:00
|
|
|
from passbook.lib.models import (
|
|
|
|
CreatedUpdatedModel,
|
|
|
|
InheritanceAutoManager,
|
|
|
|
InheritanceForeignKey,
|
|
|
|
)
|
2020-05-16 17:07:00 +01:00
|
|
|
from passbook.policies.exceptions import PolicyException
|
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
2020-05-07 19:51:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingModel(models.Model):
|
2020-05-14 12:51:05 +01:00
|
|
|
"""Base Model for objects that have policies applied to them."""
|
2020-05-07 19:51:06 +01:00
|
|
|
|
2020-05-20 08:17:06 +01:00
|
|
|
pbm_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-08 17:45:53 +01:00
|
|
|
policies = models.ManyToManyField(
|
2020-05-16 18:00:43 +01:00
|
|
|
"Policy", through="PolicyBinding", related_name="bindings", blank=True
|
2020-05-08 17:45:53 +01:00
|
|
|
)
|
2020-05-07 19:51:06 +01:00
|
|
|
|
2020-05-16 18:55:59 +01:00
|
|
|
objects = InheritanceManager()
|
|
|
|
|
2020-05-08 13:33:14 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Policy Binding Model")
|
|
|
|
verbose_name_plural = _("Policy Binding Models")
|
|
|
|
|
2020-05-07 19:51:06 +01:00
|
|
|
|
2020-05-20 08:17:06 +01:00
|
|
|
class PolicyBinding(models.Model):
|
2020-05-07 19:51:06 +01:00
|
|
|
"""Relationship between a Policy and a PolicyBindingModel."""
|
|
|
|
|
2020-05-20 08:17:06 +01:00
|
|
|
policy_binding_uuid = models.UUIDField(
|
|
|
|
primary_key=True, editable=False, default=uuid4
|
|
|
|
)
|
|
|
|
|
2020-05-07 19:51:06 +01:00
|
|
|
enabled = models.BooleanField(default=True)
|
|
|
|
|
2020-05-28 20:45:54 +01:00
|
|
|
policy = InheritanceForeignKey("Policy", on_delete=models.CASCADE, related_name="+")
|
2020-05-07 19:51:06 +01:00
|
|
|
target = models.ForeignKey(
|
|
|
|
PolicyBindingModel, on_delete=models.CASCADE, related_name="+"
|
|
|
|
)
|
2020-05-28 20:45:54 +01: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 19:51:06 +01:00
|
|
|
|
2020-05-28 20:45:54 +01:00
|
|
|
order = models.IntegerField()
|
2020-05-07 19:51:06 +01:00
|
|
|
|
2020-05-08 13:33:14 +01:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"PolicyBinding policy={self.policy} target={self.target} order={self.order}"
|
|
|
|
|
2020-05-07 19:51:06 +01:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _("Policy Binding")
|
|
|
|
verbose_name_plural = _("Policy Bindings")
|
2020-05-28 20:45:54 +01:00
|
|
|
unique_together = ("policy", "target", "order")
|
2020-05-16 17:07:00 +01:00
|
|
|
|
|
|
|
|
2020-05-20 08:17:06 +01:00
|
|
|
class Policy(CreatedUpdatedModel):
|
2020-05-16 17:07:00 +01: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 08:17:06 +01:00
|
|
|
policy_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-16 17:07:00 +01:00
|
|
|
name = models.TextField(blank=True, null=True)
|
|
|
|
|
2020-05-28 20:45:54 +01:00
|
|
|
objects = InheritanceAutoManager()
|
2020-05-16 17:07:00 +01: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 20:45:54 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
base_manager_name = "objects"
|
|
|
|
|
|
|
|
verbose_name = _("Policy")
|
|
|
|
verbose_name_plural = _("Policies")
|