Esempio n. 1
0
def accept_org_invite(token):
    invited_org_user = InvitedOrgUser.from_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 class="govuk-link govuk-link--no-visited-state" 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':
        organisation = Organisation.from_id(invited_org_user.organisation)
        return render_template('views/cancelled-invitation.html',
                               from_user=invited_org_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.from_email_address_or_none(
        invited_org_user.email_address)
    organisation_users = OrganisationUsers(invited_org_user.organisation)

    if existing_user:
        invited_org_user.accept_invite()
        if existing_user not in organisation_users:
            existing_user.add_to_organisation(
                organisation_id=invited_org_user.organisation)
        return redirect(
            url_for('main.organisation_dashboard',
                    org_id=invited_org_user.organisation))
    else:
        return redirect(url_for('main.register_from_org_invite'))
Esempio n. 2
0
def accept_org_invite(token):
    invited_org_user = InvitedOrgUser.from_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 %(email)s. This invite is for another email address. "
                + "<a href=%(href)s>Sign out</a> and click the link again to accept this invite.",
                email=current_user.email_address,
                href=url_for("main.sign_out", _external=True),
            )
        )
        flash(message=message)

        abort(403)

    if invited_org_user.status == "cancelled":
        organisation = Organisation.from_id(invited_org_user.organisation)
        return render_template(
            "views/cancelled-invitation.html",
            from_user=invited_org_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.from_email_address_or_none(invited_org_user.email_address)
    organisation_users = OrganisationUsers(invited_org_user.organisation)

    if existing_user:
        invited_org_user.accept_invite()
        if existing_user not in organisation_users:
            existing_user.add_to_organisation(organisation_id=invited_org_user.organisation)
        return redirect(url_for("main.organisation_dashboard", org_id=invited_org_user.organisation))
    else:
        return redirect(url_for("main.register_from_org_invite"))