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
|
2016-02-29 17:35:21 +00:00
|
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from app.main import main
|
2019-05-23 15:27:35 +01:00
|
|
|
|
from app.models.organisation import Organisation
|
|
|
|
|
|
from app.models.service import Service
|
|
|
|
|
|
from app.models.user import (
|
|
|
|
|
|
InvitedOrgUser,
|
|
|
|
|
|
InvitedUser,
|
|
|
|
|
|
OrganisationUsers,
|
|
|
|
|
|
User,
|
|
|
|
|
|
Users,
|
|
|
|
|
|
)
|
2016-02-29 17:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/invitation/<token>")
|
|
|
|
|
|
def accept_invite(token):
|
2020-03-06 11:53:55 +00:00
|
|
|
|
invited_user = InvitedUser.from_token(token)
|
2016-03-02 15:25:04 +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.
|
2020-05-29 17:25:11 +01:00
|
|
|
|
<a href={} class="govuk-link govuk-link--no-visited-state">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':
|
2019-05-23 15:27:35 +01:00
|
|
|
|
service = Service.from_id(invited_user.service)
|
2016-03-10 11:53:29 +00:00
|
|
|
|
return render_template('views/cancelled-invitation.html',
|
2019-05-23 15:27:35 +01:00
|
|
|
|
from_user=invited_user.from_user.name,
|
|
|
|
|
|
service_name=service.name)
|
2016-03-10 15:03:21 +00:00
|
|
|
|
if invited_user.status == 'accepted':
|
2021-03-08 12:51:35 +00:00
|
|
|
|
session.pop('invited_user_id', None)
|
2020-08-03 13:37:34 +01:00
|
|
|
|
service = Service.from_id(invited_user.service)
|
|
|
|
|
|
if service.has_permission('broadcast'):
|
2021-05-13 14:17:28 +01:00
|
|
|
|
if service.live:
|
|
|
|
|
|
return redirect(url_for('main.broadcast_tour_live', service_id=service.id, step_index=1))
|
|
|
|
|
|
else:
|
|
|
|
|
|
return redirect(url_for('main.broadcast_tour', service_id=service.id, step_index=1))
|
2016-03-10 15:03:21 +00:00
|
|
|
|
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
2016-03-04 14:42:52 +00:00
|
|
|
|
|
2021-03-08 12:51:35 +00:00
|
|
|
|
session['invited_user_id'] = invited_user.id
|
2016-02-29 17:35:21 +00:00
|
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
|
existing_user = User.from_email_address_or_none(invited_user.email_address)
|
2016-03-15 15:32:30 +00:00
|
|
|
|
|
|
|
|
|
|
if existing_user:
|
2021-08-17 16:38:37 +01:00
|
|
|
|
existing_user.update_email_access_validated_at()
|
2019-05-23 15:27:35 +01:00
|
|
|
|
invited_user.accept_invite()
|
|
|
|
|
|
if existing_user in Users(invited_user.service):
|
2016-03-15 15:32:30 +00:00
|
|
|
|
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
|
|
|
|
|
else:
|
2019-05-23 15:27:35 +01:00
|
|
|
|
service = Service.from_id(invited_user.service)
|
2021-05-25 15:58:33 +01:00
|
|
|
|
# if the service you're being added to can modify auth type, then check if we can do this;
|
|
|
|
|
|
# if the user is a Platform Admin, we silently leave this unchanged to prevent a security
|
|
|
|
|
|
# issue where someone could switch their auth type to something less secure
|
|
|
|
|
|
if service.has_permission('email_auth') and not existing_user.platform_admin:
|
|
|
|
|
|
if invited_user.auth_type == 'email_auth' or (
|
|
|
|
|
|
# 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'
|
|
|
|
|
|
):
|
|
|
|
|
|
existing_user.update(auth_type=invited_user.auth_type)
|
2019-06-27 15:48:29 +01:00
|
|
|
|
existing_user.add_to_service(
|
|
|
|
|
|
service_id=invited_user.service,
|
|
|
|
|
|
permissions=invited_user.permissions,
|
|
|
|
|
|
folder_permissions=invited_user.folder_permissions,
|
2021-03-05 12:19:36 +00:00
|
|
|
|
invited_by_id=invited_user.from_user.id,
|
2019-06-27 15:48:29 +01:00
|
|
|
|
)
|
2020-08-03 13:37:34 +01:00
|
|
|
|
if service.has_permission('broadcast'):
|
2021-05-13 14:17:28 +01:00
|
|
|
|
if service.live:
|
|
|
|
|
|
return redirect(url_for('main.broadcast_tour_live', service_id=service.id, step_index=1))
|
|
|
|
|
|
else:
|
|
|
|
|
|
return redirect(url_for('main.broadcast_tour', service_id=service.id, step_index=1))
|
2019-05-23 15:27:35 +01:00
|
|
|
|
return redirect(url_for('main.service_dashboard', service_id=service.id))
|
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):
|
2019-05-23 15:27:35 +01:00
|
|
|
|
invited_org_user = InvitedOrgUser.from_token(token)
|
2020-03-06 11:53:55 +00:00
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
|
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.
|
2020-05-29 17:25:11 +01:00
|
|
|
|
<a class="govuk-link govuk-link--no-visited-state" href={}>Sign out</a>
|
|
|
|
|
|
and click the link again to accept this invite.
|
2018-02-19 16:53:29 +00:00
|
|
|
|
""".format(
|
|
|
|
|
|
current_user.email_address,
|
|
|
|
|
|
url_for("main.sign_out", _external=True)))
|
|
|
|
|
|
|
|
|
|
|
|
flash(message=message)
|
|
|
|
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
if invited_org_user.status == 'cancelled':
|
2019-05-23 15:27:35 +01:00
|
|
|
|
organisation = Organisation.from_id(invited_org_user.organisation)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
return render_template('views/cancelled-invitation.html',
|
2019-05-23 15:27:35 +01:00
|
|
|
|
from_user=invited_org_user.invited_by.name,
|
|
|
|
|
|
organisation_name=organisation.name)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
if invited_org_user.status == 'accepted':
|
2021-03-08 10:01:12 +00:00
|
|
|
|
session.pop('invited_org_user_id', None)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
return redirect(url_for('main.organisation_dashboard', org_id=invited_org_user.organisation))
|
|
|
|
|
|
|
2021-03-08 10:01:12 +00:00
|
|
|
|
session['invited_org_user_id'] = invited_org_user.id
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
|
existing_user = User.from_email_address_or_none(invited_org_user.email_address)
|
|
|
|
|
|
organisation_users = OrganisationUsers(invited_org_user.organisation)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
if existing_user:
|
2021-08-17 16:38:37 +01:00
|
|
|
|
existing_user.update_email_access_validated_at()
|
2019-05-23 15:27:35 +01:00
|
|
|
|
invited_org_user.accept_invite()
|
2018-02-19 16:53:29 +00:00
|
|
|
|
if existing_user not in organisation_users:
|
2019-06-27 15:48:29 +01:00
|
|
|
|
existing_user.add_to_organisation(organisation_id=invited_org_user.organisation)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
return redirect(url_for('main.organisation_dashboard', org_id=invited_org_user.organisation))
|
|
|
|
|
|
else:
|
|
|
|
|
|
return redirect(url_for('main.register_from_org_invite'))
|