mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-02 17:23:32 +02:00
Add UserDomainRole table and helpers
This commit is contained in:
parent
22eb49c004
commit
49b4f078e8
9 changed files with 211 additions and 7 deletions
49
src/registrar/models/user_domain_role.py
Normal file
49
src/registrar/models/user_domain_role.py
Normal 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'
|
||||
)
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue