2018-02-20 11:22:17 +00:00
|
|
|
|
from flask import abort, flash, redirect, render_template, session, url_for
|
2017-11-01 16:02:05 +00:00
|
|
|
|
from flask_login import current_user
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from markupsafe import Markup
|
2018-03-08 12:47:24 +00:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-03-17 13:07:52 +00:00
|
|
|
|
|
2016-02-29 17:35:21 +00:00
|
|
|
|
from app import (
|
|
|
|
|
|
invite_api_client,
|
2018-02-19 16:53:29 +00:00
|
|
|
|
org_invite_api_client,
|
|
|
|
|
|
organisations_client,
|
2018-02-20 11:22:17 +00:00
|
|
|
|
service_api_client,
|
|
|
|
|
|
user_api_client,
|
2016-02-29 17:35:21 +00:00
|
|
|
|
)
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from app.main import main
|
2016-02-29 17:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/invitation/<token>")
|
|
|
|
|
|
def accept_invite(token):
|
2018-03-08 12:47:24 +00:00
|
|
|
|
try:
|
|
|
|
|
|
invited_user = invite_api_client.check_token(token)
|
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
|
if e.status_code == 400 and 'invitation' in e.message:
|
|
|
|
|
|
flash(e.message['invitation'])
|
|
|
|
|
|
return redirect(url_for('main.sign_in'))
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise e
|
2016-03-10 15:03:21 +00:00
|
|
|
|
|
2017-12-21 16:42:16 +00:00
|
|
|
|
if not current_user.is_anonymous and current_user.email_address.lower() != invited_user.email_address.lower():
|
2016-04-26 12:12:47 +01:00
|
|
|
|
message = Markup("""
|
2016-04-07 15:03:36 +01:00
|
|
|
|
You’re signed in as {}.
|
|
|
|
|
|
This invite is for another email address.
|
2016-04-26 12:15:25 +01:00
|
|
|
|
<a href={}>Sign out</a> and click the link again to accept this invite.
|
2016-04-26 12:12:47 +01:00
|
|
|
|
""".format(
|
2016-04-07 15:03:36 +01:00
|
|
|
|
current_user.email_address,
|
2016-04-26 12:12:47 +01:00
|
|
|
|
url_for("main.sign_out", _external=True)))
|
|
|
|
|
|
|
|
|
|
|
|
flash(message=message)
|
|
|
|
|
|
|
2016-03-30 16:16:34 +01:00
|
|
|
|
abort(403)
|
|
|
|
|
|
|
2016-03-10 11:53:29 +00:00
|
|
|
|
if invited_user.status == 'cancelled':
|
|
|
|
|
|
from_user = user_api_client.get_user(invited_user.from_user)
|
2016-03-29 22:50:40 +01:00
|
|
|
|
service = service_api_client.get_service(invited_user.service)['data']
|
2016-03-10 11:53:29 +00:00
|
|
|
|
return render_template('views/cancelled-invitation.html',
|
|
|
|
|
|
from_user=from_user.name,
|
|
|
|
|
|
service_name=service['name'])
|
|
|
|
|
|
|
2016-03-10 15:03:21 +00:00
|
|
|
|
if invited_user.status == 'accepted':
|
|
|
|
|
|
session.pop('invited_user', None)
|
|
|
|
|
|
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
|
|
|
|
|
|
2016-03-10 11:53:29 +00:00
|
|
|
|
session['invited_user'] = invited_user.serialize()
|
|
|
|
|
|
|
2016-03-21 11:48:16 +00:00
|
|
|
|
existing_user = user_api_client.get_user_by_email_or_none(invited_user.email_address)
|
2016-03-15 15:32:30 +00:00
|
|
|
|
service_users = user_api_client.get_users_for_service(invited_user.service)
|
|
|
|
|
|
|
|
|
|
|
|
if existing_user:
|
2016-06-08 11:52:26 +01:00
|
|
|
|
invite_api_client.accept_invite(invited_user.service, invited_user.id)
|
2016-03-15 15:32:30 +00:00
|
|
|
|
if existing_user in service_users:
|
|
|
|
|
|
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
|
|
|
|
|
else:
|
2017-11-15 17:19:32 +00:00
|
|
|
|
service = service_api_client.get_service(invited_user.service)['data']
|
|
|
|
|
|
# if the service you're being added to can modify auth type, then check if this is relevant
|
|
|
|
|
|
if 'email_auth' in service['permissions'] and (
|
|
|
|
|
|
# they have a phone number, we want them to start using it. if they dont have a mobile we just
|
|
|
|
|
|
# ignore that option of the invite
|
|
|
|
|
|
(existing_user.mobile_number and invited_user.auth_type == 'sms_auth') or
|
|
|
|
|
|
# we want them to start sending emails. it's always valid, so lets always update
|
|
|
|
|
|
invited_user.auth_type == 'email_auth'
|
|
|
|
|
|
):
|
2017-11-15 14:59:03 +00:00
|
|
|
|
user_api_client.update_user_attribute(existing_user.id, auth_type=invited_user.auth_type)
|
2016-03-15 15:32:30 +00:00
|
|
|
|
user_api_client.add_user_to_service(invited_user.service,
|
|
|
|
|
|
existing_user.id,
|
|
|
|
|
|
invited_user.permissions)
|
|
|
|
|
|
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
2016-03-10 11:53:29 +00:00
|
|
|
|
else:
|
|
|
|
|
|
return redirect(url_for('main.register_from_invite'))
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/organisation-invitation/<token>")
|
|
|
|
|
|
def accept_org_invite(token):
|
|
|
|
|
|
invited_org_user = org_invite_api_client.check_token(token)
|
|
|
|
|
|
if not current_user.is_anonymous and current_user.email_address.lower() != invited_org_user.email_address.lower():
|
|
|
|
|
|
message = Markup("""
|
|
|
|
|
|
You’re signed in as {}.
|
|
|
|
|
|
This invite is for another email address.
|
|
|
|
|
|
<a href={}>Sign out</a> and click the link again to accept this invite.
|
|
|
|
|
|
""".format(
|
|
|
|
|
|
current_user.email_address,
|
|
|
|
|
|
url_for("main.sign_out", _external=True)))
|
|
|
|
|
|
|
|
|
|
|
|
flash(message=message)
|
|
|
|
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
if invited_org_user.status == 'cancelled':
|
|
|
|
|
|
invited_by = user_api_client.get_user(invited_org_user.invited_by)
|
|
|
|
|
|
organisation = organisations_client.get_organisation(invited_org_user.organisation)
|
|
|
|
|
|
return render_template('views/cancelled-invitation.html',
|
|
|
|
|
|
from_user=invited_by.name,
|
|
|
|
|
|
organisation_name=organisation['name'])
|
|
|
|
|
|
|
|
|
|
|
|
if invited_org_user.status == 'accepted':
|
|
|
|
|
|
session.pop('invited_org_user', None)
|
|
|
|
|
|
return redirect(url_for('main.organisation_dashboard', org_id=invited_org_user.organisation))
|
|
|
|
|
|
|
|
|
|
|
|
session['invited_org_user'] = invited_org_user.serialize()
|
|
|
|
|
|
|
|
|
|
|
|
existing_user = user_api_client.get_user_by_email_or_none(invited_org_user.email_address)
|
|
|
|
|
|
organisation_users = user_api_client.get_users_for_organisation(invited_org_user.organisation)
|
|
|
|
|
|
|
|
|
|
|
|
if existing_user:
|
|
|
|
|
|
org_invite_api_client.accept_invite(invited_org_user.organisation, invited_org_user.id)
|
|
|
|
|
|
if existing_user not in organisation_users:
|
|
|
|
|
|
user_api_client.add_user_to_organisation(invited_org_user.organisation, existing_user.id)
|
|
|
|
|
|
return redirect(url_for('main.organisation_dashboard', org_id=invited_org_user.organisation))
|
|
|
|
|
|
else:
|
|
|
|
|
|
return redirect(url_for('main.register_from_org_invite'))
|