Exemplo n.º 1
0
def register():
    """This function allows to register a new user to the system.
        Upon a GET request a RegistrationForm will be shown to the user.
        Upon a POST request the form will be validated and if valid the user
            will get assigned a AuthLevel and his password will be hashed.
            He will then be added to the database and redirect to the default
            route of the authentication-module.
            Should the form be invalid, the user will be shown the form again.
    """
    form = RegistrationForm(request.form)

    if request.method == 'POST' and form.validate():
        user = User()
        form.populate_obj(user)
        user.password = generateHash(user.password)
        user.authLevel = AuthLevel.USER

        user.save()

        logger.info('A user has been added.')
        flash('Your user account has been created.')
        return redirect(url_for('auth.login'))
    return render_template('auth/registration.html', form = form)