mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-03 20:02:47 +00:00 
			
		
		
		
	Use enums where appropriate. This commit includes new migrations that are NOT compatible with older nopaque instances
This commit is contained in:
		@@ -1,9 +1,14 @@
 | 
			
		||||
from . import USERNAME_REGEX
 | 
			
		||||
from ..models import User
 | 
			
		||||
from app.models import User
 | 
			
		||||
from flask_wtf import FlaskForm
 | 
			
		||||
from wtforms import (BooleanField, PasswordField, StringField, SubmitField,
 | 
			
		||||
                     ValidationError)
 | 
			
		||||
from wtforms import (
 | 
			
		||||
    BooleanField,
 | 
			
		||||
    PasswordField,
 | 
			
		||||
    StringField,
 | 
			
		||||
    SubmitField,
 | 
			
		||||
    ValidationError
 | 
			
		||||
)
 | 
			
		||||
from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
 | 
			
		||||
from . import USERNAME_REGEX
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LoginForm(FlaskForm):
 | 
			
		||||
@@ -17,42 +22,54 @@ class RegistrationForm(FlaskForm):
 | 
			
		||||
    email = StringField('Email', validators=[DataRequired(), Email()])
 | 
			
		||||
    username = StringField(
 | 
			
		||||
        'Username',
 | 
			
		||||
        validators=[DataRequired(), Length(1, 64),
 | 
			
		||||
                    Regexp(USERNAME_REGEX,
 | 
			
		||||
                           message='Usernames must have only letters, numbers,'
 | 
			
		||||
                                   ' dots or underscores')]
 | 
			
		||||
        validators=[
 | 
			
		||||
            DataRequired(),
 | 
			
		||||
            Length(1, 64),
 | 
			
		||||
            Regexp(
 | 
			
		||||
                USERNAME_REGEX,
 | 
			
		||||
                message='Usernames must have only letters, numbers, dots or underscores'  # noqa
 | 
			
		||||
           )
 | 
			
		||||
       ]
 | 
			
		||||
    )
 | 
			
		||||
    password = PasswordField(
 | 
			
		||||
        'Password',
 | 
			
		||||
        validators=[DataRequired(), EqualTo('password_confirmation',
 | 
			
		||||
                                            message='Passwords must match.')]
 | 
			
		||||
        validators=[
 | 
			
		||||
            DataRequired(),
 | 
			
		||||
            EqualTo('password_confirmation', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    password_confirmation = PasswordField(
 | 
			
		||||
        'Password confirmation',
 | 
			
		||||
        validators=[DataRequired(), EqualTo('password',
 | 
			
		||||
                                            message='Passwords must match.')]
 | 
			
		||||
        validators=[
 | 
			
		||||
            DataRequired(),
 | 
			
		||||
            EqualTo('password', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField('Register')
 | 
			
		||||
 | 
			
		||||
    def validate_email(self, field):
 | 
			
		||||
        if User.query.filter_by(email=field.data.lower()).first():
 | 
			
		||||
            raise ValidationError('Email already registered.')
 | 
			
		||||
            raise ValidationError('Email already registered')
 | 
			
		||||
 | 
			
		||||
    def validate_username(self, field):
 | 
			
		||||
        if User.query.filter_by(username=field.data).first():
 | 
			
		||||
            raise ValidationError('Username already in use.')
 | 
			
		||||
            raise ValidationError('Username already in use')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ResetPasswordForm(FlaskForm):
 | 
			
		||||
    password = PasswordField(
 | 
			
		||||
        'New password',
 | 
			
		||||
        validators=[DataRequired(), EqualTo('password_confirmation',
 | 
			
		||||
                                            message='Passwords must match.')]
 | 
			
		||||
        validators=[
 | 
			
		||||
            DataRequired(),
 | 
			
		||||
            EqualTo('password_confirmation', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    password_confirmation = PasswordField(
 | 
			
		||||
        'Password confirmation',
 | 
			
		||||
        validators=[DataRequired(),
 | 
			
		||||
                    EqualTo('password', message='Passwords must match.')]
 | 
			
		||||
        validators=[
 | 
			
		||||
            DataRequired(),
 | 
			
		||||
            EqualTo('password', message='Passwords must match')
 | 
			
		||||
        ]
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField('Reset Password')
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,25 @@
 | 
			
		||||
from app import db
 | 
			
		||||
from app.email import create_message, send
 | 
			
		||||
from app.models import User
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from flask import (abort, current_app, flash, redirect, render_template,
 | 
			
		||||
                   request, url_for)
 | 
			
		||||
from flask import (
 | 
			
		||||
    abort,
 | 
			
		||||
    current_app,
 | 
			
		||||
    flash,
 | 
			
		||||
    redirect,
 | 
			
		||||
    render_template,
 | 
			
		||||
    request,
 | 
			
		||||
    url_for
 | 
			
		||||
)
 | 
			
		||||
from flask_login import current_user, login_user, login_required, logout_user
 | 
			
		||||
from sqlalchemy import or_
 | 
			
		||||
from . import bp
 | 
			
		||||
from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
 | 
			
		||||
                    RegistrationForm)
 | 
			
		||||
from .. import db
 | 
			
		||||
from ..email import create_message, send
 | 
			
		||||
from ..models import User
 | 
			
		||||
import os
 | 
			
		||||
from .forms import (
 | 
			
		||||
    LoginForm,
 | 
			
		||||
    ResetPasswordForm,
 | 
			
		||||
    ResetPasswordRequestForm,
 | 
			
		||||
    RegistrationForm
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.before_app_request
 | 
			
		||||
@@ -21,10 +31,12 @@ def before_request():
 | 
			
		||||
    if current_user.is_authenticated:
 | 
			
		||||
        current_user.last_seen = datetime.utcnow()
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        if (not current_user.confirmed
 | 
			
		||||
                and request.endpoint
 | 
			
		||||
                and request.blueprint != 'auth'
 | 
			
		||||
                and request.endpoint != 'static'):
 | 
			
		||||
        if (
 | 
			
		||||
            not current_user.confirmed
 | 
			
		||||
            and request.endpoint
 | 
			
		||||
            and request.blueprint != 'auth'
 | 
			
		||||
            and request.endpoint != 'static'
 | 
			
		||||
        ):
 | 
			
		||||
            return redirect(url_for('auth.unconfirmed'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -34,15 +46,19 @@ def login():
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    form = LoginForm(prefix='login-form')
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        user = User.query.filter(or_(User.username == form.user.data,
 | 
			
		||||
                                     User.email == form.user.data.lower())).first()
 | 
			
		||||
        user = User.query.filter(
 | 
			
		||||
            or_(
 | 
			
		||||
                User.username == form.user.data,
 | 
			
		||||
                User.email == form.user.data.lower()
 | 
			
		||||
            )
 | 
			
		||||
        ).first()
 | 
			
		||||
        if user and user.verify_password(form.password.data):
 | 
			
		||||
            login_user(user, form.remember_me.data)
 | 
			
		||||
            next = request.args.get('next')
 | 
			
		||||
            if next is None or not next.startswith('/'):
 | 
			
		||||
                next = url_for('main.dashboard')
 | 
			
		||||
            return redirect(next)
 | 
			
		||||
        flash('Invalid email/username or password.')
 | 
			
		||||
        flash('Invalid email/username or password', category='error')
 | 
			
		||||
    return render_template('auth/login.html.j2', form=form, title='Log in')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -50,7 +66,7 @@ def login():
 | 
			
		||||
@login_required
 | 
			
		||||
def logout():
 | 
			
		||||
    logout_user()
 | 
			
		||||
    flash('You have been logged out.')
 | 
			
		||||
    flash('You have been logged out')
 | 
			
		||||
    return redirect(url_for('main.index'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -73,19 +89,20 @@ def register():
 | 
			
		||||
        except OSError as e:
 | 
			
		||||
            current_app.logger.error(e)
 | 
			
		||||
            db.session.rollback()
 | 
			
		||||
            flash('Internal Server Error', category='error')
 | 
			
		||||
            abort(500)
 | 
			
		||||
        else:
 | 
			
		||||
            token = user.generate_confirmation_token()
 | 
			
		||||
            msg = create_message(
 | 
			
		||||
                user.email,
 | 
			
		||||
                'Confirm Your Account',
 | 
			
		||||
                'auth/email/confirm',
 | 
			
		||||
                token=token,
 | 
			
		||||
                user=user
 | 
			
		||||
            )
 | 
			
		||||
            send(msg)
 | 
			
		||||
            flash('A confirmation email has been sent to you by email.')
 | 
			
		||||
            return redirect(url_for('.login'))
 | 
			
		||||
        token = user.generate_confirmation_token()
 | 
			
		||||
        msg = create_message(
 | 
			
		||||
            user.email,
 | 
			
		||||
            'Confirm Your Account',
 | 
			
		||||
            'auth/email/confirm',
 | 
			
		||||
            token=token,
 | 
			
		||||
            user=user
 | 
			
		||||
        )
 | 
			
		||||
        send(msg)
 | 
			
		||||
        flash('A confirmation email has been sent to you by email')
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        return redirect(url_for('.login'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'auth/register.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
@@ -100,10 +117,13 @@ def confirm(token):
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    if current_user.confirm(token):
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('You have confirmed your account. Thanks!')
 | 
			
		||||
        flash('You have confirmed your account')
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    else:
 | 
			
		||||
        flash('The confirmation link is invalid or has expired.')
 | 
			
		||||
        flash(
 | 
			
		||||
            'The confirmation link is invalid or has expired',
 | 
			
		||||
            category='error'
 | 
			
		||||
        )
 | 
			
		||||
        return redirect(url_for('.unconfirmed'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -120,10 +140,15 @@ def unconfirmed():
 | 
			
		||||
@login_required
 | 
			
		||||
def resend_confirmation():
 | 
			
		||||
    token = current_user.generate_confirmation_token()
 | 
			
		||||
    msg = create_message(current_user.email, 'Confirm Your Account',
 | 
			
		||||
                         'auth/email/confirm', token=token, user=current_user)
 | 
			
		||||
    msg = create_message(
 | 
			
		||||
        current_user.email,
 | 
			
		||||
        'Confirm Your Account',
 | 
			
		||||
        'auth/email/confirm',
 | 
			
		||||
        token=token,
 | 
			
		||||
        user=current_user
 | 
			
		||||
    )
 | 
			
		||||
    send(msg)
 | 
			
		||||
    flash('A new confirmation email has been sent to you by email.')
 | 
			
		||||
    flash('A new confirmation email has been sent to you by email')
 | 
			
		||||
    return redirect(url_for('auth.unconfirmed'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -136,14 +161,23 @@ def reset_password_request():
 | 
			
		||||
        user = User.query.filter_by(email=form.email.data.lower()).first()
 | 
			
		||||
        if user is not None:
 | 
			
		||||
            token = user.generate_reset_token()
 | 
			
		||||
            msg = create_message(user.email, 'Reset Your Password',
 | 
			
		||||
                                 'auth/email/reset_password', token=token,
 | 
			
		||||
                                 user=user)
 | 
			
		||||
            msg = create_message(
 | 
			
		||||
                user.email,
 | 
			
		||||
                'Reset Your Password',
 | 
			
		||||
                'auth/email/reset_password',
 | 
			
		||||
                token=token,
 | 
			
		||||
                user=user
 | 
			
		||||
            )
 | 
			
		||||
            send(msg)
 | 
			
		||||
        flash('An email with instructions to reset your password has been sent to you.')  # noqa
 | 
			
		||||
        flash(
 | 
			
		||||
            'An email with instructions to reset your password has been sent to you'  # noqa
 | 
			
		||||
        )
 | 
			
		||||
        return redirect(url_for('.login'))
 | 
			
		||||
    return render_template('auth/reset_password_request.html.j2', form=form,
 | 
			
		||||
                           title='Password Reset')
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'auth/reset_password_request.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        title='Password Reset'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/reset/<token>', methods=['GET', 'POST'])
 | 
			
		||||
@@ -154,9 +188,13 @@ def reset_password(token):
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        if User.reset_password(token, form.password.data):
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
            flash('Your password has been updated.')
 | 
			
		||||
            flash('Your password has been updated')
 | 
			
		||||
            return redirect(url_for('.login'))
 | 
			
		||||
        else:
 | 
			
		||||
            return redirect(url_for('main.index'))
 | 
			
		||||
    return render_template('auth/reset_password.html.j2', form=form,
 | 
			
		||||
                           title='Password Reset', token=token)
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'auth/reset_password.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        title='Password Reset',
 | 
			
		||||
        token=token
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user