示例#1
0
def post_password_reset():
    try:
        email = request.form['email']
        json_resp, status = api_post('/api/authentication/token',
                                     json={"email": email})

        form = FormValidator('An error has occurred')
        form.add_validator('email',
                           email, [email_validator, is_not_empty],
                           empty_msg='Enter your email address')

        if status != 200 or not form.is_valid():
            raise ValueError

        token = json_resp['token']
        decoded_jwt, status = api_get('/api/authentication/token/' + token +
                                      '?validation-type=reset-password')

        if status != 200:
            current_app.logger.error('Token has failed validation:' +
                                     decoded_jwt['error'])
            raise ValueError

        first_name = decoded_jwt['principle']['first_name']
        url = current_app.config.get(
            "SITE_URL") + "/password/change?t=" + token
        current_app.logger.info(url)

        template_id = current_app.config.get("RESET_PASSWORD_TEMPLATE")
        _, email_status = api_post('/api/notifications',
                                   json={
                                       "email_address": email,
                                       "template_id": template_id,
                                       "personalisation": {
                                           "first_name":
                                           first_name,
                                           "last_name":
                                           decoded_jwt['principle']['surname'],
                                           "change_password_link":
                                           url
                                       },
                                       "reference": "password-reset"
                                   },
                                   headers={'Accept': 'application/json'})
        if email_status != 201:
            raise ValueError

        return render_template('app/password/email_sent.html', email=email)

    except ValueError:
        form = FormValidator('An error has occurred')
        form.add_validator('email',
                           email, [is_not_empty, email_validator],
                           empty_msg='Enter your email address')
        return render_template(
            'app/password/reset.html',
            error_title="There was a problem",
            fields=form.validate(),
        )
def add_overseas_info_validators(dto):
    form = FormValidator('There are errors on this page')
    form.add_validator('name',
                       dto.name,
                       is_not_empty,
                       empty_msg='Enter your organisation name')
    form.add_validator('country',
                       dto.country_incorp, [is_not_empty, is_overseas_country],
                       empty_msg='Enter a country')
    return form
def add_charity_details_validators(dto):
    form = FormValidator('There are errors on this page')

    form.add_validator('name',
                       dto.name,
                       is_not_empty,
                       empty_msg='Enter your charity name')
    form.add_validator('charity',
                       dto.reg_no, [is_not_empty],
                       empty_msg='Enter your charity number')
    return form
def add_company_details_validators(dto):
    form = FormValidator('There are errors on this page')

    form.add_validator('name',
                       dto.name,
                       is_not_empty,
                       empty_msg='Enter your company name')
    form.add_validator('reg_no',
                       dto.reg_no, [is_not_empty, crn_validator],
                       empty_msg='Enter your company registration number')
    return form
示例#5
0
def _validate_and_save_password(user_id, password, confirm_password):
    try:
        form = FormValidator('Enter a valid password')
        form.add_validator('passwords',
                           password,
                           fvs=[
                               password_length, password_letters,
                               password_number, password_symbol
                           ])
        form.add_validator('confirm_password', [password, confirm_password],
                           fvs=confirm_passwords_match)

        if not form.is_valid():
            current_app.logger.error('New password has failed validation')
            raise ValueError

        _, status = api_patch(
            '/api/account/users/' + user_id,
            json={
                'password': password,
                'disabled': None
            },
            headers={'Content-Type': 'application/merge-patch+json'})

        if status != 204:
            current_app.logger.error('Account-api has failed the validation')
            raise ValueError

        return True, {}

    except ValueError as e:
        current_app.logger.error(e)
        result = form.validate()
        if (not result['passwords'].error) and (
                not result['confirm_password'].error):
            result = {
                'passwords': ValidationResult(0, None,
                                              ['Enter a valid password'])
            }
        return False, result
def fail_signin():
    email = request.form['email']
    password = request.form['password']

    current_app.logger.info(
        'Calling account-api to check lock status for {}'.format(email))
    api_url = '/api/account/users/{}/check_lock'.format(email)
    resp, code = api_get(api_url, headers={'Content-Type': 'application/json'})

    if 'locked' in resp and resp['locked'] is not None:
        current_app.logger.info('Users account is locked')
        breadcrumb_links = [{
            "label": "Home",
            "href": "/"
        }, {
            "label": "Sign in to your account",
            "href": None
        }]
        flash('Your account is locked. Check your email.')
        return render_template("app/auth/signin.html",
                               error_title="There was a problem",
                               breadcrumb_links=breadcrumb_links)

    form = FormValidator('Email or password not recognised')
    form.add_validator('email', email, [email_validator, is_not_empty])
    form.add_validator('password', password, is_not_empty)
    breadcrumb_links = [{
        "label": "Home",
        "href": "/"
    }, {
        "label": "Sign in to your account",
        "href": None
    }]

    return render_template("app/auth/signin.html",
                           error_title="There was a problem",
                           fields=form.validate(),
                           breadcrumb_links=breadcrumb_links)
示例#7
0
def _add_preference_validators(pref_list):
    form = FormValidator("There are errors on this page")
    form.add_validator('preferences', pref_list, preference_validator)
    return form
示例#8
0
def _add_research_validators(dto):
    form = FormValidator("There are errors on this page")
    form.add_validator('research', dto.research, research_validator)
    return form
示例#9
0
def _add_address_validators(dto):
    form = FormValidator("There are errors on this page")
    if dto.uk_resident == 'yes':
        form.add_validator('postcode',
                           dto.postcode, [is_not_empty, postcode_validator],
                           empty_msg="Enter your postcode")
        form.add_validator('county',
                           dto.county, [is_not_empty],
                           empty_msg="Enter your county")
    elif dto.uk_resident == 'no':
        form.add_validator('country',
                           dto.country, [is_not_empty, is_overseas_country],
                           empty_msg='Enter your country')
        form.add_validator('postcode', dto.postcode, fvs=[], empty_msg='')

    form.add_validator('building_and_street',
                       dto.street_line_1,
                       is_not_empty,
                       empty_msg='Enter a building and street')
    form.add_validator('street_line_1',
                       dto.street_line_1, [],
                       empty_msg='Enter a building and street')
    form.add_validator('street_line_2', dto.street_line_2)
    form.add_validator('city',
                       dto.city,
                       is_not_empty,
                       empty_msg='Enter a town or city')

    return form
示例#10
0
def _add_address_type_validators(dto):
    form = FormValidator("There are errors on this page")
    form.add_validator('uk_resident', dto.uk_resident, residency_validator)
    return form
def _add_org_type_validators(dto):
    form = FormValidator("There are errors on this page")
    form.add_validator('type', dto.type, org_type_validator)
    return form
def add_org_info_validators(dto, bypass):
    form = FormValidator('There are errors on this page')

    if bypass is False:
        form.add_validator('name',
                           dto.name,
                           is_not_empty,
                           empty_msg='Enter your organisation name')
    form.add_validator('building_and_street',
                       dto.street_line_1,
                       is_not_empty,
                       empty_msg='Enter a building and street')
    form.add_validator('street_line_1',
                       dto.street_line_1, [],
                       empty_msg='Enter a building and street')
    form.add_validator('street_line_2', dto.street_line_2)
    form.add_validator('city',
                       dto.city,
                       is_not_empty,
                       empty_msg='Enter a town or city')
    form.add_validator('county',
                       dto.county,
                       is_not_empty,
                       empty_msg='Enter a county')
    form.add_validator('postcode',
                       dto.postcode,
                       fvs=[is_not_empty, postcode_validator],
                       empty_msg='Enter your postcode')
    form.add_validator('phone',
                       dto.phone,
                       fvs=[is_not_empty, phone_number_validator],
                       empty_msg='Enter your phone number')
    return form
def add_personal_info_validators(dto, user_type=None):
    form = FormValidator('There are errors on this page')
    form.add_validator('title',
                       dto.title,
                       fvs=is_not_empty,
                       empty_msg='Enter your title')
    form.add_validator('first_name',
                       dto.first_name,
                       fvs=is_not_empty,
                       empty_msg='Enter your first name')
    form.add_validator('last_name',
                       dto.last_name,
                       fvs=is_not_empty,
                       empty_msg='Enter your last name')
    form.add_validator('email',
                       dto.email,
                       fvs=[is_not_empty, email_validator, account_exists],
                       empty_msg='Enter your email')
    if user_type != 'org':
        form.add_validator('phone',
                           dto.phone,
                           fvs=[is_not_empty, phone_number_validator],
                           empty_msg='Enter your phone number')
    return form
def add_org_address_validators(dto, org):
    form = FormValidator('There are errors on this page')
    form.add_validator('building_and_street',
                       dto.street_line_1,
                       is_not_empty,
                       empty_msg='Enter a building and street')
    form.add_validator('street_line_1',
                       dto.street_line_1, [],
                       empty_msg='Enter a building and street')
    form.add_validator('street_line_2', dto.street_line_2)
    form.add_validator('city',
                       dto.city,
                       is_not_empty,
                       empty_msg='Enter a town or city')
    form.add_validator('phone',
                       dto.phone,
                       fvs=[is_not_empty, phone_number_validator],
                       empty_msg='Enter your phone number')
    if org == 'overseas':
        msg = 'Enter a country'
        form.add_validator('country',
                           dto.country, [is_not_empty],
                           empty_msg=msg)
        form.add_validator('postcode', dto.postcode, fvs=[], empty_msg='')
    else:
        form.add_validator('county',
                           dto.county,
                           is_not_empty,
                           empty_msg='Enter a county')
    return form