Example #1
0
def send_password_reset(login):
    user = get_user_by_login(login)
    reset_token = user.get_reset_token()
    sender = app.config['USER_EMAIL_SENDER_EMAIL']
    app_name = app.config['USER_APP_NAME']
    reset_link = url_for('users.reset_password',
                         token=reset_token,
                         _external=True)
    if is_valid_email(login):
        message_body = render_template('emails/password_reset_message.html',
                                       reset_link=reset_link,
                                       app_name=app_name)
        send_bulk_mail([login], sender, '{} password reset'.format(app_name),
                       message_body)
    else:
        if app.config['DEBUG']:
            reset_link = "{}{}".format(
                app.config['NGROK'],
                url_for('users.reset_password', token=reset_token))
        else:
            reset_link = url_for('users.reset_password',
                                 token=reset_token,
                                 _external=True)

        message_body = render_template('sms/password_reset_message.txt',
                                       reset_link=reset_link,
                                       app_name=app_name)
        send_sms(login, message_body)
Example #2
0
        def __verify_email(self, email):
            if not email:
                self.__errors.append(USER['CONTACT']['EMAIL']['NOT_FOUND'])
                self.status = STATUS_CODE['NOT_FOUND']

            elif not is_valid_email(email):
                self.__errors.append(USER['CONTACT']['EMAIL']['INVALID'])
                self.status = STATUS_CODE['BAD_REQUEST']
Example #3
0
    def is_ok(self):
        is_ok = True

        self.errors = []

        if self.first_name == '-':
            self.errors.append('No first name provided')
            is_ok = False

        if self.last_name == '-':
            self.errors.append('No last name provided')
            is_ok = False

        if not is_valid_phone_number(self.mobile_phone):
            if not is_valid_email(self.email):
                self.errors.append('No valid login provided, please provide valid '
                                   'e-mail address of mobile phone number')
                is_ok = False

        if self.mobile_phone:

            if self.mobile_phone != '-':
                user = User.query.filter_by(mobile_phone=self.mobile_phone).first()
                if user:
                    is_ok = False
                    self.errors.append('A user with that mobile phone number already exists in the Maranatha database.')
                else:
                    user_invite = UserInvitation.query.filter_by(mobile_phone=self.mobile_phone).first()
                    if user_invite:
                        is_ok = False
                        self.is_duplicate_invite = True
                        self.errors.append(f"An invite for user with mobile phone '{self.mobile_phone}' "
                                           f"already exists.")

        if self.email:
            if self.email != '-':
                user = User.query.filter_by(email=self.email).first()
                if user:
                    is_ok = False
                    self.errors.append('A user with that e-mail address already exists in the Maranatha database.')
                else:
                    user_invite = UserInvitation.query.filter_by(email=self.email).first()
                    if user_invite:
                        is_ok = False
                        self.is_duplicate_invite = True
                        self.errors.append(f"An invite for user with e-mail '{self.email}' already exists.")

        return is_ok
Example #4
0
    def signup(self):
        form = SignupForm()
        if request.method == 'POST':
            if not form.validate_on_submit():
                return render_template('home/register.html', form=form)

            email = form.email.data
            if not is_valid_email(email, False):
                flash_error(gettext(u'Invalid email.'))
                return render_template('home/register.html', form=form)

            existing_user = User.objects(email=email).first()
            if existing_user:
                return redirect(url_for('HomeView:signin'))

            hash_pass = generate_password_hash(form.password.data,
                                               method='sha256')
            new_user = User(email=email, password=hash_pass)
            new_user.save()

            token = self._confirm_link_generator.dumps(email,
                                                       salt=HomeView.SALT_LINK)

            confirm_url = url_for('HomeView:confirm_email',
                                  token=token,
                                  _external=True)
            config = app.config['PUBLIC_CONFIG']
            html = render_template(
                'home/email/activate.html',
                confirm_url=confirm_url,
                contact_email=config['support']['contact_email'],
                title=config['site']['title'],
                company=config['company']['title'])
            msg = Message(subject=gettext(u'Confirm Email'),
                          recipients=[email],
                          html=html)
            mail.send(msg)
            flash_success(gettext(u'Please check email: {0}.'.format(email)))
            return redirect(url_for('HomeView:signin'))

        return render_template('home/register.html', form=form)
Example #5
0
def forgot_password(with_reset):
    if with_reset:
        form = ForgotPasswordFormWithReset()
    else:
        form = ForgotPasswordForm()
    if form.validate_on_submit():
        submitted_login = form.login.data
        user = get_user_by_login(submitted_login)
        if not user:
            flash('There is no account with that login.', 'danger')
        else:
            try:
                if form.reactivate_account.data:
                    user.is_active = True
                    db.session.commit()
            except AttributeError:
                pass
            send_password_reset(submitted_login)
            flash_message = "{} with password reset instructions have been sent to you."\
                .format('An email' if is_valid_email(submitted_login) else 'A sms')
            flash(flash_message, 'info')
            return redirect(url_for('main.home'))
    return render_template('forgot_password.html', form=form)
Example #6
0
def verify_invitation(data_dict, submitted_number_set, submitted_email_set):

    data_dict['has_errors'] = False
    data_dict['errors'] = []
    data_dict['is_duplication_in_submitted_data'] = False
    data_dict['is_duplicate_of_previous_invitation'] = False
    data_dict['is_for_user_already_in_database'] = False
    data_dict['is_invitation_that_already_in_database'] = False
    data_dict['is_empty_line'] = False

    is_empty_line = True

    # Validate first name
    if not data_dict['first_name']:
        data_dict['errors'].append('The record does not have a first name')

    # Validate last name
    if not data_dict['last_name']:
        data_dict['errors'].append('The record does not have a last name')

    # Validate mobile phone
    if data_dict['mobile_phone']:
        is_empty_line = False
        submitted_number = str(data_dict['mobile_phone'])
        if submitted_number.startswith('264'):
            submitted_number = '+' + submitted_number
            data_dict['mobile_phone'] = submitted_number
        if not is_valid_phone_number(submitted_number):
            data_dict['errors'].append(
                'The mobile phone number does not appear to be valid.')
            data_dict['has_errors'] = True

        user = User.query.filter_by(
            mobile_phone=data_dict['mobile_phone']).first()
        if user:
            data_dict['errors'].append(
                f"A user with mobile phone number {data_dict['mobile_phone']} "
                f"already exists in the database.")
            data_dict['is_for_user_already_in_database'] = True

        user_invitation = UserInvitation.query.filter_by(
            mobile_phone=data_dict['mobile_phone']).first()
        if user_invitation:
            data_dict['errors'].append(
                f"There is already a previous invitation for user with mobile "
                f"phone number {data_dict['mobile_phone']} ")
            data_dict['is_duplicate_of_previous_invitation'] = True

        if data_dict['mobile_phone'] in submitted_number_set:
            data_dict['errors'].append(
                f"Mobile phone {data_dict['mobile_phone']} is already associated with another "
                f"record in this dataset.")
            data_dict['is_duplication_in_submitted_data'] = True
        else:
            submitted_number_set.add(data_dict['mobile_phone'])

    # Validate e-mail
    if data_dict['email']:
        is_empty_line = False
        if not is_valid_email(data_dict['email']):
            data_dict['errors'].append(
                'The e-mail address does not appear to be valid.')
            data_dict['has_errors'] = True

        user = User.query.filter_by(email=data_dict['email']).first()
        if user:
            data_dict['errors'].append(
                f"A user with email {data_dict['email']} "
                f"already exists in the database.")
            data_dict['is_for_user_already_in_database'] = True

        user_invitation = UserInvitation.query.filter_by(
            mobile_phone=data_dict['mobile_phone']).first()
        if user_invitation:
            data_dict['errors'].append(
                f"There is already a previous invitation for user with email "
                f"{data_dict['email']} ")
            data_dict['is_duplicate_of_previous_invitation'] = True

        if data_dict['mobile_phone'] in submitted_email_set:
            data_dict['errors'].append(
                f"E-mail {data_dict['email']} is already associated with another "
                f"record in this dataset.")
            data_dict['is_duplication_in_submitted_data'] = True
        else:
            submitted_email_set.add(data_dict['mobile_phone'])

    data_dict['is_empty_line'] = is_empty_line
    if len(data_dict['errors']) > 0:
        data_dict['has_errors'] = True
Example #7
0
def test_email_validation():
    assert utils.is_valid_email('*****@*****.**')
    assert utils.is_valid_email('ec.europa.eu') is False