Change team invitation acceptance to join all invited teams under the org

Fixes #1989
This commit is contained in:
Joseph Schorr 2016-11-28 18:39:28 -05:00
parent 1c3012a538
commit 402ad25690
3 changed files with 119 additions and 12 deletions

View file

@ -312,10 +312,24 @@ def find_matching_team_invite(code, user_obj):
return found
def find_organization_invites(organization, user_obj):
""" Finds all organization team invites for the given user under the given organization. """
invite_check = (TeamMemberInvite.user == user_obj)
if user_obj.verified:
invite_check = invite_check | (TeamMemberInvite.email == user_obj.email)
query = (TeamMemberInvite
.select()
.join(Team)
.where(invite_check, Team.organization == organization))
return query
def confirm_team_invite(code, user_obj):
""" Confirms the given team invite code for the given user by adding the user to the team
and deleting the code. Raises a DataModelException if the code was not found or does
not apply to the given user. """
not apply to the given user. If the user is invited to two or more teams under the
same organization, they are automatically confirmed for all of them. """
found = find_matching_team_invite(code, user_obj)
# If the invite is for a specific user, we have to confirm that here.
@ -324,15 +338,18 @@ def confirm_team_invite(code, user_obj):
Please login to that account and try again.""" % found.user.username
raise DataModelException(message)
# Add the user to the team.
try:
add_user_to_team(user_obj, found.team)
except UserAlreadyInTeam:
# Ignore.
pass
# Find all matching invitations for the user under the organization.
for invite in find_organization_invites(found.team.organization, user_obj):
# Add the user to the team.
try:
add_user_to_team(user_obj, invite.team)
except UserAlreadyInTeam:
# Ignore.
pass
# Delete the invite and return the team.
invite.delete_instance()
# Delete the invite and return the team.
team = found.team
inviter = found.inviter
found.delete_instance()
return (team, inviter)