-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#2729: Update Add a Domain Manager page - [ES] #2857
Changes from 7 commits
caad00d
42de7f2
8b61eb1
b0fe698
d710696
d66ff33
3d1781c
117900c
2059c9f
09944e4
467b7a9
17b5f36
e396534
9425d4c
2f2c4e1
6a01e56
889c0a2
aec1a4f
76fc713
dd29cbf
4393b5a
961a289
3cb341d
62b3cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,15 @@ class InvalidDomainError(ValueError): | |
pass | ||
|
||
|
||
class OutsideOrgMemberError(ValueError): | ||
""" | ||
Error raised when an org member tries adding a user from a different .gov org. | ||
To be deleted when users can be members of multiple orgs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. love the comment here too! great! |
||
""" | ||
|
||
pass | ||
|
||
|
||
class ActionNotAllowed(Exception): | ||
"""User accessed an action that is not | ||
allowed by the current state""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,10 @@ | |
DomainRequest, | ||
DomainInformation, | ||
DomainInvitation, | ||
PortfolioInvitation, | ||
User, | ||
UserDomainRole, | ||
UserPortfolioPermission, | ||
PublicContact, | ||
) | ||
from registrar.utility.enums import DefaultEmail | ||
|
@@ -35,9 +37,11 @@ | |
DsDataErrorCodes, | ||
SecurityEmailError, | ||
SecurityEmailErrorCodes, | ||
OutsideOrgMemberError, | ||
) | ||
from registrar.models.utility.contact_error import ContactError | ||
from registrar.views.utility.permission_views import UserDomainRolePermissionDeleteView | ||
from registrar.utility.waffle import flag_is_active_for_user | ||
|
||
from ..forms import ( | ||
SeniorOfficialContactForm, | ||
|
@@ -778,7 +782,18 @@ def _domain_abs_url(self): | |
"""Get an absolute URL for this domain.""" | ||
return self.request.build_absolute_uri(reverse("domain", kwargs={"pk": self.object.id})) | ||
|
||
def _send_domain_invitation_email(self, email: str, requestor: User, add_success=True): | ||
def _is_member_of_different_org(self, email, requestor, requested_user): | ||
"""Verifies if an email belongs to a different organization as a member or invited member.""" | ||
# Check if user is a already member of a different organization than the requestor's org | ||
requestor_org = UserPortfolioPermission.objects.get(user=requestor).portfolio | ||
existing_org_permission = UserPortfolioPermission.objects.filter(user=requested_user).first() | ||
existing_org_invitation = PortfolioInvitation.objects.filter(email=email).first() | ||
|
||
return (existing_org_permission and existing_org_permission.portfolio != requestor_org) or ( | ||
existing_org_invitation and existing_org_invitation.portfolio != requestor_org | ||
) | ||
|
||
def _send_domain_invitation_email(self, email: str, requestor: User, requested_user=None, add_success=True): | ||
"""Performs the sending of the domain invitation email, | ||
does not make a domain information object | ||
email: string- email to send to | ||
|
@@ -803,6 +818,17 @@ def _send_domain_invitation_email(self, email: str, requestor: User, add_success | |
) | ||
return None | ||
|
||
# Check is user is a member or invited member of a different org from this domain's org | ||
if flag_is_active_for_user(requestor, "organization_feature") and self._is_member_of_different_org( | ||
email, requestor, requested_user | ||
): | ||
add_success = False | ||
messages.error( | ||
self.request, | ||
"That email is already a member of another .gov organization.", | ||
) | ||
raise OutsideOrgMemberError | ||
|
||
# Check to see if an invite has already been sent | ||
try: | ||
invite = DomainInvitation.objects.get(email=email, domain=self.object) | ||
|
@@ -831,6 +857,8 @@ def _send_domain_invitation_email(self, email: str, requestor: User, add_success | |
"requestor_email": requestor_email, | ||
}, | ||
) | ||
if add_success: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this into the try statement since we shouldn't be sending emails if we do run into Exceptions that aren't EmailSendingError - feel free to let me know if there was a reason we did this previously though! |
||
messages.success(self.request, f"{email} has been invited to this domain.") | ||
except EmailSendingError as exc: | ||
logger.warn( | ||
"Could not sent email invitation to %s for domain %s", | ||
|
@@ -839,9 +867,6 @@ def _send_domain_invitation_email(self, email: str, requestor: User, add_success | |
exc_info=True, | ||
) | ||
raise EmailSendingError("Could not send email invitation.") from exc | ||
else: | ||
if add_success: | ||
messages.success(self.request, f"{email} has been invited to this domain.") | ||
|
||
def _make_invitation(self, email_address: str, requestor: User): | ||
"""Make a Domain invitation for this email and redirect with a message.""" | ||
|
@@ -868,32 +893,40 @@ def form_valid(self, form): | |
else: | ||
# if user already exists then just send an email | ||
try: | ||
self._send_domain_invitation_email(requested_email, requestor, add_success=False) | ||
self._send_domain_invitation_email( | ||
requested_email, requestor, requested_user=requested_user, add_success=False | ||
) | ||
except EmailSendingError: | ||
logger.warn( | ||
"Could not send email invitation (EmailSendingError)", | ||
self.object, | ||
exc_info=True, | ||
) | ||
messages.warning(self.request, "Could not send email invitation.") | ||
except OutsideOrgMemberError: | ||
logger.warn( | ||
"Could not send email. Can not invite member of a .gov organization to a different organization.", | ||
self.object, | ||
exc_info=True, | ||
) | ||
except Exception: | ||
logger.warn( | ||
"Could not send email invitation (Other Exception)", | ||
self.object, | ||
exc_info=True, | ||
) | ||
messages.warning(self.request, "Could not send email invitation.") | ||
|
||
try: | ||
UserDomainRole.objects.create( | ||
user=requested_user, | ||
domain=self.object, | ||
role=UserDomainRole.Roles.MANAGER, | ||
) | ||
except IntegrityError: | ||
messages.warning(self.request, f"{requested_email} is already a manager for this domain") | ||
else: | ||
messages.success(self.request, f"Added user {requested_email}.") | ||
else: | ||
try: | ||
UserDomainRole.objects.create( | ||
user=requested_user, | ||
domain=self.object, | ||
role=UserDomainRole.Roles.MANAGER, | ||
) | ||
except IntegrityError: | ||
messages.warning(self.request, f"{requested_email} is already a manager for this domain") | ||
else: | ||
messages.success(self.request, f"Added user {requested_email}.") | ||
return redirect(self.get_success_url()) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can delete this error class and all references to it once we allow users to be members of multiple orgs as per the ticket ACs