-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #490 from cisagov/nmb/user-invitations
Invite new users to manage domains
- Loading branch information
Showing
23 changed files
with
445 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Generated by Django 4.1.6 on 2023-03-24 16:56 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django_fsm # type: ignore | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("registrar", "0015_remove_domain_owners_userdomainrole_user_domains_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="DomainInvitation", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("email", models.EmailField(max_length=254)), | ||
( | ||
"status", | ||
django_fsm.FSMField( | ||
choices=[("sent", "sent"), ("retrieved", "retrieved")], | ||
default="sent", | ||
max_length=50, | ||
protected=True, | ||
), | ||
), | ||
( | ||
"domain", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="invitations", | ||
to="registrar.domain", | ||
), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""People are invited by email to administer domains.""" | ||
|
||
import logging | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.db import models | ||
|
||
from django_fsm import FSMField, transition # type: ignore | ||
|
||
from .utility.time_stamped_model import TimeStampedModel | ||
from .user_domain_role import UserDomainRole | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DomainInvitation(TimeStampedModel): | ||
INVITED = "invited" | ||
RETRIEVED = "retrieved" | ||
|
||
email = models.EmailField( | ||
null=False, | ||
blank=False, | ||
) | ||
|
||
domain = models.ForeignKey( | ||
"registrar.Domain", | ||
on_delete=models.CASCADE, # delete domain, then get rid of invitations | ||
null=False, | ||
related_name="invitations", | ||
) | ||
|
||
status = FSMField( | ||
choices=[ | ||
(INVITED, INVITED), | ||
(RETRIEVED, RETRIEVED), | ||
], | ||
default=INVITED, | ||
protected=True, # can't alter state except through transition methods! | ||
) | ||
|
||
def __str__(self): | ||
return f"Invitation for {self.email} on {self.domain} is {self.status}" | ||
|
||
@transition(field="status", source=INVITED, target=RETRIEVED) | ||
def retrieve(self): | ||
"""When an invitation is retrieved, create the corresponding permission. | ||
Raises: | ||
RuntimeError if no matching user can be found. | ||
""" | ||
|
||
# get a user with this email address | ||
User = get_user_model() | ||
try: | ||
user = User.objects.get(email=self.email) | ||
except User.DoesNotExist: | ||
# should not happen because a matching user should exist before | ||
# we retrieve this invitation | ||
raise RuntimeError( | ||
"Cannot find the user to retrieve this domain invitation." | ||
) | ||
|
||
# and create a role for that user on this domain | ||
_, created = UserDomainRole.objects.get_or_create( | ||
user=user, domain=self.domain, role=UserDomainRole.Roles.ADMIN | ||
) | ||
if not created: | ||
# something strange happened and this role already existed when | ||
# the invitation was retrieved. Log that this occurred. | ||
logger.warn( | ||
"Invitation %s was retrieved for a role that already exists.", self | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
You have been invited to manage the domain {{ domain.name }} on get.gov, | ||
the registrar for .gov domain names. | ||
|
||
To accept your invitation, go to <{{ domain_url }}>. | ||
|
||
You will need to log in with a Login.gov account using this email address. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
You are invited to manage {{ domain.name }} on get.gov |
1 change: 1 addition & 0 deletions
1
src/registrar/templates/emails/submission_confirmation_subject.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Thank you for applying for a .gov domain |
Oops, something went wrong.