2020-05-07 18:51:06 +00:00
|
|
|
"""Flow models"""
|
2020-05-09 19:31:29 +00:00
|
|
|
from typing import Optional
|
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
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-08 17:46:39 +00:00
|
|
|
from model_utils.managers import InheritanceManager
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
from passbook.core.types import UIUserSettings
|
2020-05-07 18:51:06 +00:00
|
|
|
from passbook.policies.models import PolicyBindingModel
|
|
|
|
|
|
|
|
|
2020-05-09 18:54:56 +00:00
|
|
|
class FlowDesignation(models.TextChoices):
|
2020-05-07 18:51:06 +00:00
|
|
|
"""Designation of what a Flow should be used for. At a later point, this
|
|
|
|
should be replaced by a database entry."""
|
|
|
|
|
|
|
|
AUTHENTICATION = "authentication"
|
2020-05-12 12:50:00 +00:00
|
|
|
INVALIDATION = "invalidation"
|
2020-05-07 18:51:06 +00:00
|
|
|
ENROLLMENT = "enrollment"
|
2020-05-12 12:50:00 +00:00
|
|
|
UNRENOLLMENT = "unenrollment"
|
2020-05-07 18:51:06 +00:00
|
|
|
RECOVERY = "recovery"
|
2020-05-08 17:46:39 +00:00
|
|
|
PASSWORD_CHANGE = "password_change" # nosec # noqa
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
class Stage(models.Model):
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Stage is an instance of a component used in a flow. This can verify the user,
|
|
|
|
enroll the user or offer a way of recovery"""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
stage_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
name = models.TextField()
|
|
|
|
|
|
|
|
objects = InheritanceManager()
|
|
|
|
type = ""
|
|
|
|
form = ""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ui_user_settings(self) -> Optional[UIUserSettings]:
|
|
|
|
"""Entrypoint to integrate with User settings. Can either return None if no
|
|
|
|
user settings are available, or an instanace of UIUserSettings."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"Stage {self.name}"
|
|
|
|
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
class Flow(PolicyBindingModel):
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Flow describes how a series of Stages should be executed to authenticate/enroll/recover
|
2020-05-07 18:51:06 +00:00
|
|
|
a user. Additionally, policies can be applied, to specify which users
|
|
|
|
have access to this flow."""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
flow_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
name = models.TextField()
|
|
|
|
slug = models.SlugField(unique=True)
|
|
|
|
|
2020-05-09 18:54:56 +00:00
|
|
|
designation = models.CharField(max_length=100, choices=FlowDesignation.choices)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
stages = models.ManyToManyField(Stage, through="FlowStageBinding", blank=True)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
pbm = models.OneToOneField(
|
|
|
|
PolicyBindingModel, parent_link=True, on_delete=models.CASCADE, related_name="+"
|
|
|
|
)
|
|
|
|
|
2020-05-10 16:14:10 +00:00
|
|
|
def related_flow(self, designation: str) -> Optional["Flow"]:
|
|
|
|
"""Get a related flow with `designation`. Currently this only queries
|
|
|
|
Flows by `designation`, but will eventually use `self` for related lookups."""
|
|
|
|
return Flow.objects.filter(designation=designation).first()
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Flow {self.name} ({self.slug})"
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _("Flow")
|
|
|
|
verbose_name_plural = _("Flows")
|
|
|
|
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
class FlowStageBinding(PolicyBindingModel):
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Relationship between Flow and Stage. Order is required and unique for
|
|
|
|
each flow-stage Binding. Additionally, policies can be specified, which determine if
|
2020-05-07 18:51:06 +00:00
|
|
|
this Binding applies to the current user"""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
fsb_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
flow = models.ForeignKey("Flow", on_delete=models.CASCADE)
|
2020-05-08 17:46:39 +00:00
|
|
|
stage = models.ForeignKey(Stage, on_delete=models.CASCADE)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-07 19:30:52 +00:00
|
|
|
re_evaluate_policies = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
help_text=_(
|
|
|
|
"When this option is enabled, the planner will re-evaluate policies bound to this."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
order = models.IntegerField()
|
|
|
|
|
2020-05-16 17:55:59 +00:00
|
|
|
objects = InheritanceManager()
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
def __str__(self) -> str:
|
2020-05-08 17:46:39 +00:00
|
|
|
return f"Flow Stage Binding #{self.order} {self.flow} -> {self.stage}"
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2020-05-08 12:33:14 +00:00
|
|
|
ordering = ["order", "flow"]
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
verbose_name = _("Flow Stage Binding")
|
|
|
|
verbose_name_plural = _("Flow Stage Bindings")
|
|
|
|
unique_together = (("flow", "stage", "order"),)
|