def render_create_application(token, data=None, errors=None):
    data = data or {}
    try:
        token_data = decode_user_token(token.encode())
    except InvalidToken:
        return render_template('errors/500.html', error_message='Account creation invitation invalid or expired')

    if not token_data.get('email_address'):
        abort(503, 'Invalid email address')

    user_json = data_api_client.get_user(email_address=token_data['email_address'])

    if not user_json:
        rendered_component = render_component(
            'bundles/SellerRegistration/EnterPasswordWidget.js', {
                'form_options': {
                    'errors': errors
                },
                'enterPasswordForm': dict(token_data.items() + data.items()),
            }
        )

        return render_template(
            '_react.html',
            component=rendered_component
        )

    user = User.from_json(user_json)
    return render_template(
        'auth/create_user_error.html',
        data=token_data,
        user=user), 400
def render_create_application(token, data=None, errors=None):
    data = data or {}
    try:
        token_data = decode_user_token(token.encode())
    except InvalidToken:
        return render_template(
            'errors/500.html',
            error_message='Account creation invitation invalid or expired')

    if not token_data.get('email_address'):
        abort(503, 'Invalid email address')

    user_json = data_api_client.get_user(
        email_address=token_data['email_address'])

    if not user_json:
        rendered_component = render_component(
            'bundles/SellerRegistration/EnterPasswordWidget.js', {
                'form_options': {
                    'errors': errors
                },
                'enterPasswordForm': dict(token_data.items() + data.items()),
            })

        return render_template('_react.html', component=rendered_component)

    user = User.from_json(user_json)
    return render_template('auth/create_user_error.html',
                           data=token_data,
                           user=user), 400
def authorise_application(id):
    application = data_api_client.get_application(id)
    if not can_user_view_application(application):
        abort(403, 'Not authorised to access application')
    if is_application_submitted(application):
        return redirect(url_for('.submit_application', id=id))

    application = application['application']
    url = url_for('main.render_application',
                  id=id,
                  step='submit',
                  _external=True)
    user_json = data_api_client.get_user(email_address=application['email'])
    template = 'emails/create_authorise_email_has_account.html'

    if not user_json:
        token_data = {
            'id': id,
            'name': application['representative'],
            'email_address': application['email']
        }
        token = generate_application_invitation_token(token_data)
        url = url_for('main.render_create_application',
                      token=token,
                      _external=True)
        template = 'emails/create_authorise_email_no_account.html'

    email_body = render_template(
        template,
        url=url,
        name=application['representative'],
        business_name=application['name'],
    )

    try:
        send_email(application['email'], email_body,
                   current_app.config['AUTHREP_EMAIL_SUBJECT'],
                   current_app.config['INVITE_EMAIL_FROM'],
                   current_app.config['INVITE_EMAIL_NAME'])
    except EmailError as e:
        rollbar.report_exc_info()
        current_app.logger.error(
            'Authorisation email failed to send. '
            'error {error}',
            extra={'error': six.text_type(e)})
        abort(503, 'Failed to send user invite reset')

    return render_template('suppliers/authorisation_submitted.html',
                           name=application['representative'],
                           email_address=application['email'],
                           subject=current_app.config['AUTHREP_EMAIL_SUBJECT'])
def authorise_application(id):
    application = data_api_client.get_application(id)
    if not can_user_view_application(application):
        abort(403, 'Not authorised to access application')
    if is_application_submitted(application):
        return redirect(url_for('.submit_application', id=id))

    application = application['application']
    url = url_for('main.render_application', id=id, step='submit', _external=True)
    user_json = data_api_client.get_user(email_address=application['email'])
    template = 'emails/create_authorise_email_has_account.html'

    if not user_json:
        token_data = {'id': id, 'name': application['representative'], 'email_address': application['email']}
        token = generate_application_invitation_token(token_data)
        url = url_for('main.render_create_application', token=token, _external=True)
        template = 'emails/create_authorise_email_no_account.html'

    email_body = render_template(
        template,
        url=url,
        name=application['representative'],
        business_name=application['name'],
    )

    try:
        send_email(
            application['email'],
            email_body,
            current_app.config['AUTHREP_EMAIL_SUBJECT'],
            current_app.config['INVITE_EMAIL_FROM'],
            current_app.config['INVITE_EMAIL_NAME']
        )
    except EmailError as e:
        rollbar.report_exc_info()
        current_app.logger.error(
            'Authorisation email failed to send. '
            'error {error}',
            extra={'error': six.text_type(e)}
        )
        abort(503, 'Failed to send user invite reset')

    return render_template('suppliers/authorisation_submitted.html',
                           name=application['representative'],
                           email_address=application['email'],
                           subject=current_app.config['AUTHREP_EMAIL_SUBJECT'])
Beispiel #5
0
def create_user(token):
    data = get_create_user_data(token)

    user_json = data_api_client.get_user(email_address=data['emailAddress'])

    if not user_json:
        form = CreateUserForm(name=data['name'])
        return render_template_with_csrf(
            'auth/create_user.html',
            form=form,
            email_address=data['emailAddress'],
            supplier_name=data['supplierName'],
            token=token)

    user = User.from_json(user_json)
    return render_template(
        'auth/create_user_error.html',
        data=data,
        user=user), 400
def create_user(token):
    data = get_create_user_data(token)

    user_json = data_api_client.get_user(email_address=data['emailAddress'])

    if not user_json:
        form = CreateUserForm(name=data['name'])
        return render_template_with_csrf(
            'auth/create_user.html',
            form=form,
            email_address=data['emailAddress'],
            supplier_name=data['supplierName'],
            token=token)

    user = User.from_json(user_json)
    return render_template(
        'auth/create_user_error.html',
        data=data,
        user=user), 400
def create_buyer_account(token):
    try:
        data = decode_buyer_creation_token(token.encode())
    except InvalidToken:
        abort(404)

    form = auth_forms.CreateUserForm(name=data['name'])
    email_address = data.get('emailAddress', None)
    if email_address is None:
        email_address = data.get('email_address', None)

    user_json = data_api_client.get_user(email_address=email_address)

    if not user_json:
        return render_template_with_csrf('auth/create-user.html',
                                         form=form,
                                         email_address=email_address,
                                         token=token)

    user = User.from_json(user_json)
    return render_template_with_csrf('auth/create-buyer-user-error.html',
                                     status_code=400,
                                     token=token,
                                     user=user)
def send_reset_password_email():
    form = auth_forms.EmailAddressForm(request.form)
    if form.validate():
        email_address = form.email_address.data
        user_json = data_api_client.get_user(email_address=email_address)

        if user_json is not None:

            user = User.from_json(user_json)

            token = generate_token(
                {
                    "user": user.id,
                    "email": user.email_address
                }, current_app.config['SECRET_KEY'],
                current_app.config['RESET_PASSWORD_SALT'])

            url = url_for('main.reset_password', token=token, _external=True)

            email_body = render_template("emails/reset_password_email.html",
                                         url=url,
                                         locked=user.locked)

            try:
                send_email(
                    user.email_address,
                    email_body,
                    current_app.config['RESET_PASSWORD_EMAIL_SUBJECT'],
                    current_app.config['RESET_PASSWORD_EMAIL_FROM'],
                    current_app.config['RESET_PASSWORD_EMAIL_NAME'],
                )
            except EmailError as e:
                rollbar.report_exc_info()
                current_app.logger.error(
                    "Password reset email failed to send. "
                    "error {error} email_hash {email_hash}",
                    extra={
                        'error': six.text_type(e),
                        'email_hash': hash_email(user.email_address)
                    })
                abort(503, response="Failed to send password reset.")

            current_app.logger.info(
                "login.reset-email.sent: Sending password reset email for "
                "supplier_code {supplier_code} email_hash {email_hash}",
                extra={
                    'supplier_code': user.supplier_code,
                    'email_hash': hash_email(user.email_address)
                })
        else:
            current_app.logger.info(
                "login.reset-email.invalid-email: "
                "Password reset request for invalid supplier email {email_hash}",
                extra={'email_hash': hash_email(email_address)})

        flash('email_sent')
        return redirect(url_for('.request_password_reset'))
    else:
        return render_template_with_csrf("auth/request-password-reset.html",
                                         status_code=400,
                                         form=form)