Add UserDomainRole table and helpers

This commit is contained in:
Neil Martinsen-Burrell 2023-03-06 12:03:29 -06:00
parent 22eb49c004
commit 49b4f078e8
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
9 changed files with 211 additions and 7 deletions

View file

@ -0,0 +1,49 @@
from django.db import models
from .utility.time_stamped_model import TimeStampedModel
class UserDomainRole(TimeStampedModel):
"""This is a linking table that connects a user with a role on a domain."""
class Roles(models.TextChoices):
"""The possible roles are listed here.
Implementation of the named roles for allowing particular operations happens
elsewhere.
"""
ADMIN = "admin"
user = models.ForeignKey(
"registrar.User",
null=False,
on_delete=models.CASCADE, # when a user is deleted, their permissions will be too
related_name="permissions",
)
domain = models.ForeignKey(
"registrar.Domain",
null=False,
on_delete=models.CASCADE, # when a domain is deleted, permissions are too
related_name="permissions"
)
role = models.TextField(
choices=Roles.choices,
null=False,
blank=False,
)
def __str__(self):
return "User {} is {} on domain {}".format(self.user, self.role, self.domain)
class Meta:
constraints = [
# a user can have only one role on a given domain, that is, there can
# be only a single row with a certain (user, domain) pair.
models.UniqueConstraint(
fields=['user', 'domain'], name='unique_user_domain_role'
)
]