def get_or_create_user(invitation_uuid, sender): invitation = models.Invitation.objects.get(uuid=invitation_uuid) user, created = utils.get_or_create_user(invitation) username = generate_safe_username(user.username) password = pwgen() try: profile, created = utils.get_or_create_profile(user, username, password) except (freeipa_exceptions.FreeIPAError, requests.RequestException) as e: logger.exception( 'Unable to create FreeIPA profile for user with ID: %s', user.id) invitation.error_message = str(e) invitation.save(update_fields=['error_message']) raise if created: sender = invitation.created_by.full_name or invitation.created_by.username context = utils.get_invitation_context(invitation, sender) context['username'] = username context['password'] = password context['link'] = invitation.link_template.format(uuid=invitation_uuid) broadcast_mail('users', 'invitation_approved', context, [invitation.email]) else: send_invitation_created(invitation_uuid, sender)
def send_invitation_rejected(invitation_uuid, sender): """ Invitation notification is sent to the user which has created invitation. """ invitation = models.Invitation.objects.get(uuid=invitation_uuid) context = utils.get_invitation_context(invitation, sender) broadcast_mail('users', 'invitation_rejected', context, [invitation.created_by.email])
def send_invitation_requested(invitation_uuid, sender): """ Invitation request is sent to staff users so that they can approve or reject invitation. """ invitation = models.Invitation.objects.get(uuid=invitation_uuid) base_context = utils.get_invitation_context(invitation, sender) staff_users = core_models.User.objects.filter( is_staff=True, is_active=True ).exclude(email='') for user in staff_users: token = utils.get_invitation_token(invitation, user) approve_link = format_homeport_link('invitation_approve/{token}/', token=token) reject_link = format_homeport_link('invitation_reject/{token}/', token=token) context = dict( approve_link=approve_link, reject_link=reject_link, **base_context ) broadcast_mail('users', 'invitation_requested', context, [user.email])
def send_invitation_created(invitation_uuid, sender): """ Invitation notification is sent to the user so that he can accept it and receive permissions. """ invitation = models.Invitation.objects.get(uuid=invitation_uuid) context = utils.get_invitation_context(invitation, sender) context['link'] = invitation.link_template.format(uuid=invitation_uuid) logger.debug( 'About to send invitation to {email} to join {name} {type} as {role}'. format(email=invitation.email, **context)) try: broadcast_mail('users', 'invitation_created', context, [invitation.email]) except SMTPException as e: invitation.error_message = str(e) invitation.save(update_fields=['error_message']) raise
def send_invitation_requested(invitation_uuid, sender): """ Invitation request is sent to staff users so that they can approve or reject invitation. """ invitation = models.Invitation.objects.get(uuid=invitation_uuid) base_context = utils.get_invitation_context(invitation, sender) staff_users = core_models.User.objects.filter( is_staff=True, is_active=True).exclude(email='') for user in staff_users: token = utils.get_invitation_token(invitation, user) approve_link = settings.WALDUR_CORE['INVITATION_APPROVE_URL'].format( token=token) reject_link = settings.WALDUR_CORE['INVITATION_REJECT_URL'].format( token=token) context = dict(approve_link=approve_link, reject_link=reject_link, **base_context) broadcast_mail('users', 'invitation_requested', context, [user.email])