mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-20 13:00:36 +00:00
More exception handling. Remove unused database models. New common view structure!
This commit is contained in:
@ -2,4 +2,4 @@ from flask import Blueprint
|
||||
|
||||
|
||||
auth = Blueprint('auth', __name__)
|
||||
from . import views # noqa
|
||||
from . import views
|
||||
|
@ -18,7 +18,7 @@ class RegistrationForm(FlaskForm):
|
||||
username = StringField(
|
||||
'Username',
|
||||
validators=[DataRequired(), Length(1, 64),
|
||||
Regexp(current_app.config['ALLOWED_USERNAME_REGEX'],
|
||||
Regexp(current_app.config['NOPAQUE_USERNAME_REGEX'],
|
||||
message='Usernames must have only letters, numbers,'
|
||||
' dots or underscores')]
|
||||
)
|
||||
|
@ -1,5 +1,5 @@
|
||||
from flask import (current_app, flash, redirect, render_template, request,
|
||||
url_for)
|
||||
from datetime import datetime
|
||||
from flask import abort, flash, redirect, render_template, request, url_for
|
||||
from flask_login import current_user, login_user, login_required, logout_user
|
||||
from . import auth
|
||||
from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
|
||||
@ -7,8 +7,8 @@ from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
|
||||
from .. import db
|
||||
from ..email import create_message, send
|
||||
from ..models import User
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
@auth.before_app_request
|
||||
@ -18,11 +18,12 @@ def before_request():
|
||||
unconfirmed view if user is unconfirmed.
|
||||
"""
|
||||
if current_user.is_authenticated:
|
||||
current_user.ping()
|
||||
if not current_user.confirmed \
|
||||
and request.endpoint \
|
||||
and request.blueprint != 'auth' \
|
||||
and request.endpoint != 'static':
|
||||
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'):
|
||||
return redirect(url_for('auth.unconfirmed'))
|
||||
|
||||
|
||||
@ -30,20 +31,19 @@ def before_request():
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.dashboard'))
|
||||
login_form = LoginForm(prefix='login-form')
|
||||
if login_form.validate_on_submit():
|
||||
user = User.query.filter_by(username=login_form.user.data).first()
|
||||
form = LoginForm(prefix='login-form')
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=form.user.data).first()
|
||||
if user is None:
|
||||
user = User.query.filter_by(email=login_form.user.data).first()
|
||||
if user is not None and user.verify_password(login_form.password.data):
|
||||
login_user(user, login_form.remember_me.data)
|
||||
user = User.query.filter_by(email=form.user.data.lower()).first()
|
||||
if user is not None 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.')
|
||||
return render_template('auth/login.html.j2', login_form=login_form,
|
||||
title='Log in')
|
||||
return render_template('auth/login.html.j2', form=form, title='Log in')
|
||||
|
||||
|
||||
@auth.route('/logout')
|
||||
@ -58,26 +58,28 @@ def logout():
|
||||
def register():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.dashboard'))
|
||||
registration_form = RegistrationForm(prefix='registration-form')
|
||||
if registration_form.validate_on_submit():
|
||||
user = User(email=registration_form.email.data.lower(),
|
||||
password=registration_form.password.data,
|
||||
username=registration_form.username.data)
|
||||
form = RegistrationForm(prefix='registration-form')
|
||||
if form.validate_on_submit():
|
||||
user = User(email=form.email.data.lower(),
|
||||
password=form.password.data,
|
||||
username=form.username.data)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
user_dir = os.path.join(current_app.config['DATA_DIR'],
|
||||
str(user.id))
|
||||
if os.path.exists(user_dir):
|
||||
shutil.rmtree(user_dir)
|
||||
os.mkdir(user_dir)
|
||||
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('auth.login'))
|
||||
return render_template('auth/register.html.j2',
|
||||
registration_form=registration_form,
|
||||
try:
|
||||
os.makedirs(user.path)
|
||||
except OSError:
|
||||
logging.error('Make dir {} led to an OSError!'.format(user.path))
|
||||
db.session.delete(user)
|
||||
db.session.commit()
|
||||
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'))
|
||||
return render_template('auth/register.html.j2', form=form,
|
||||
title='Register')
|
||||
|
||||
|
||||
@ -92,7 +94,7 @@ def confirm(token):
|
||||
return redirect(url_for('main.dashboard'))
|
||||
else:
|
||||
flash('The confirmation link is invalid or has expired.')
|
||||
return redirect(url_for('auth.unconfirmed'))
|
||||
return redirect(url_for('.unconfirmed'))
|
||||
|
||||
|
||||
@auth.route('/unconfirmed')
|
||||
@ -119,39 +121,32 @@ def resend_confirmation():
|
||||
def reset_password_request():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.dashboard'))
|
||||
reset_password_request_form = ResetPasswordRequestForm(
|
||||
prefix='reset-password-request-form')
|
||||
if reset_password_request_form.validate_on_submit():
|
||||
submitted_email = reset_password_request_form.email.data
|
||||
user = User.query.filter_by(email=submitted_email.lower()).first()
|
||||
if user:
|
||||
form = ResetPasswordRequestForm(prefix='reset-password-request-form')
|
||||
if form.validate_on_submit():
|
||||
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)
|
||||
send(msg)
|
||||
flash('An email with instructions to reset your password has been '
|
||||
'sent to you.')
|
||||
return redirect(url_for('auth.login'))
|
||||
return render_template(
|
||||
'auth/reset_password_request.html.j2',
|
||||
reset_password_request_form=reset_password_request_form,
|
||||
title='Password Reset')
|
||||
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')
|
||||
|
||||
|
||||
@auth.route('/reset/<token>', methods=['GET', 'POST'])
|
||||
def reset_password(token):
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.dashboard'))
|
||||
reset_password_form = ResetPasswordForm(prefix='reset-password-form')
|
||||
if reset_password_form.validate_on_submit():
|
||||
if User.reset_password(token, reset_password_form.password.data):
|
||||
form = ResetPasswordForm(prefix='reset-password-form')
|
||||
if form.validate_on_submit():
|
||||
if User.reset_password(token, form.password.data):
|
||||
db.session.commit()
|
||||
flash('Your password has been updated.')
|
||||
return redirect(url_for('auth.login'))
|
||||
return redirect(url_for('.login'))
|
||||
else:
|
||||
return redirect(url_for('main.index'))
|
||||
return render_template('auth/reset_password.html.j2',
|
||||
reset_password_form=reset_password_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