Esempio n. 1
0
def register():
    if '_id' in session:
        return redirect_to_home()

    if request.method == 'POST':
        results = {
            'name': request.form['name'],
            'email': request.form['email'],
            'password': request.form['password']
        }

        # Time to validate the shit out of this thing
        error_list = []
        errors = False
        for key, value in results.items():
            if not value:
                error_list.append('Please specify a {0}!'.format(key.replace('_', ' ')))
                errors = True

            if key == 'email':
                if not re.match(r'[^@]+@[^@]+\.[^@]+', value):
                    error_list.append('Please specify a valid email address!')
                    errors = True

            if isinstance(value, str):
                if len(value) > 50:
                    error_list.append('The length of {0} cannot exceed 50.'.format(key.replace('_', ' ')))
                    errors = True

        captcha_payload = {'privatekey': CONFIG['recaptcha_private_key'],
                           'remoteip': request.remote_addr,
                           'challenge': request.form['recaptcha_challenge_field'],
                           'response': request.form['recaptcha_response_field']}
        captcha_response = requests.post('http://www.google.com/recaptcha/api/verify', captcha_payload)

        if 'true' not in captcha_response.text:
            error_list.append('The captcha answer you provided isn\'t correct.')
            errors = True

        if errors:
            return render_template('register.html', errors=error_list)

        password_hashed = sha512_crypt.encrypt(results['password'])

        verification_key = ''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(32))

        conn = BRConnection()
        users = conn.users

        for _ in users.find({'email': results['email']}):
            return render_template('register.html', errors=['There is already a user with the email {0}. Maybe you already signed up?'.format(results['email'])])

        new_user = User(email=results['email'], name=results['name'], password=password_hashed, verification_key=verification_key)
        users.insert(new_user.data)

        resend_verification(results['email'])

        return render_template('register_success.html', name=results['name'])

    return render_template('register.html')
Esempio n. 2
0
def change_email(user_id, new_email):
    conn = BRConnection()
    users = conn.users

    user = get_user(user_id)
    if user is None:
        return False

    users.update({'_id': ObjectId(user_id)}, {'$set': {'email': new_email, 'verified': False}})
    resend_verification(new_email)
    logout()

    return True