mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-25 02:44:18 +00:00
155 lines
5.7 KiB
Python
155 lines
5.7 KiB
Python
from datetime import datetime
|
|
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
|
|
|
|
|
|
@bp.before_app_request
|
|
def before_request():
|
|
"""
|
|
Checks if a user is unconfirmed when visiting specific sites. Redirects to
|
|
unconfirmed view if user is unconfirmed.
|
|
"""
|
|
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'):
|
|
return redirect(url_for('auth.unconfirmed'))
|
|
|
|
|
|
@bp.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if current_user.is_authenticated:
|
|
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()
|
|
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.')
|
|
return render_template('auth/login.html.j2', form=form, title='Log in')
|
|
|
|
|
|
@bp.route('/logout')
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
flash('You have been logged out.')
|
|
return redirect(url_for('main.index'))
|
|
|
|
|
|
@bp.route('/register', methods=['GET', 'POST'])
|
|
def register():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('main.dashboard'))
|
|
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()
|
|
try:
|
|
os.makedirs(user.path)
|
|
except OSError:
|
|
current_app.logger.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')
|
|
|
|
|
|
@bp.route('/confirm/<token>')
|
|
@login_required
|
|
def confirm(token):
|
|
if current_user.confirmed:
|
|
return redirect(url_for('main.dashboard'))
|
|
if current_user.confirm(token):
|
|
db.session.commit()
|
|
flash('You have confirmed your account. Thanks!')
|
|
return redirect(url_for('main.dashboard'))
|
|
else:
|
|
flash('The confirmation link is invalid or has expired.')
|
|
return redirect(url_for('.unconfirmed'))
|
|
|
|
|
|
@bp.route('/unconfirmed')
|
|
def unconfirmed():
|
|
if current_user.is_anonymous:
|
|
return redirect(url_for('main.index'))
|
|
elif current_user.confirmed:
|
|
return redirect(url_for('main.dashboard'))
|
|
return render_template('auth/unconfirmed.html.j2', title='Unconfirmed')
|
|
|
|
|
|
@bp.route('/confirm')
|
|
@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)
|
|
send(msg)
|
|
flash('A new confirmation email has been sent to you by email.')
|
|
return redirect(url_for('auth.unconfirmed'))
|
|
|
|
|
|
@bp.route('/reset', methods=['GET', 'POST'])
|
|
def reset_password_request():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('main.dashboard'))
|
|
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.') # noqa
|
|
return redirect(url_for('.login'))
|
|
return render_template('auth/reset_password_request.html.j2', form=form,
|
|
title='Password Reset')
|
|
|
|
|
|
@bp.route('/reset/<token>', methods=['GET', 'POST'])
|
|
def reset_password(token):
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('main.dashboard'))
|
|
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('.login'))
|
|
else:
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/reset_password.html.j2', form=form,
|
|
title='Password Reset', token=token)
|