mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 09:15:41 +00:00
132 lines
4.6 KiB
Python
132 lines
4.6 KiB
Python
from app import db
|
|
from app.email import send_email
|
|
from app.models import User
|
|
from flask import (current_app, flash, redirect, render_template, request,
|
|
url_for)
|
|
from flask_login import current_user, login_required, logout_user
|
|
from . import auth
|
|
from .forms import (PasswordResetForm, PasswordResetRequestForm,
|
|
RegistrationForm)
|
|
import os
|
|
|
|
|
|
@auth.route('/logout')
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
flash('You have been logged out.')
|
|
return redirect(url_for('main.index'))
|
|
|
|
|
|
@auth.route('/register', methods=['GET', 'POST'])
|
|
def register():
|
|
if not current_user.is_anonymous:
|
|
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)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
dir = os.path.join(current_app.config['NOPAQUE_STORAGE'], str(user.id))
|
|
try:
|
|
os.makedirs(dir)
|
|
except OSError:
|
|
flash('[ERROR]!')
|
|
user.delete()
|
|
else:
|
|
token = user.generate_confirmation_token()
|
|
send_email(user.email, 'Confirm Your Account',
|
|
'auth/email/confirm', token=token, user=user)
|
|
flash('A confirmation email has been sent to you by email.')
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/register.html.j2',
|
|
registration_form=registration_form,
|
|
title='Register')
|
|
|
|
|
|
@auth.route('/confirm/<token>')
|
|
@login_required
|
|
def confirm(token):
|
|
if current_user.confirmed:
|
|
return redirect(url_for('main.index'))
|
|
if current_user.confirm(token):
|
|
db.session.commit()
|
|
flash('You have confirmed your account. Thanks!')
|
|
else:
|
|
flash('The confirmation link is invalid or has expired.')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
|
|
@auth.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
|
|
and not current_user.confirmed
|
|
and request.blueprint != 'auth'
|
|
and request.endpoint != 'static'):
|
|
return redirect(url_for('auth.unconfirmed'))
|
|
|
|
|
|
@auth.route('/unconfirmed')
|
|
def unconfirmed():
|
|
if current_user.is_anonymous or current_user.confirmed:
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/unconfirmed.html.j2', title='Unconfirmed')
|
|
|
|
|
|
@auth.route('/confirm')
|
|
@login_required
|
|
def resend_confirmation():
|
|
token = current_user.generate_confirmation_token()
|
|
send_email(current_user.email,
|
|
'Confirm Your Account',
|
|
'auth/email/confirm',
|
|
token=token,
|
|
user=current_user)
|
|
flash('A new confirmation email has benn sent to you by email.')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
|
|
@auth.route('/reset', methods=['GET', 'POST'])
|
|
def password_reset_request():
|
|
if not current_user.is_anonymous:
|
|
return redirect(url_for('main.dashboard'))
|
|
form = PasswordResetRequestForm()
|
|
if form.validate_on_submit():
|
|
user = User.query.filter_by(email=form.email.data.lower()).first()
|
|
if user:
|
|
token = user.generate_reset_token()
|
|
send_email(user.email,
|
|
'Reset Your Password',
|
|
'auth/email/reset_password',
|
|
token=token,
|
|
user=user)
|
|
flash('An email with instructions to reset your password has been '
|
|
'sent to you.')
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/reset_password_request.html.j2',
|
|
form=form,
|
|
title='Password Reset')
|
|
|
|
|
|
@auth.route('/reset/<token>', methods=['GET', 'POST'])
|
|
def password_reset(token):
|
|
if not current_user.is_anonymous:
|
|
return redirect(url_for('main.dashboard'))
|
|
form = PasswordResetForm()
|
|
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('main.index'))
|
|
else:
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/reset_password.html.j2',
|
|
form=form,
|
|
title='Password Reset')
|