mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
173 lines
6.4 KiB
Python
173 lines
6.4 KiB
Python
from flask import flash, redirect, render_template, request, url_for
|
|
from flask_login import current_user, login_required, login_user, logout_user
|
|
from . import auth
|
|
from .. import db
|
|
from .forms import ChangePasswordForm, LoginForm, PasswordResetForm, PasswordResetRequestForm, RegistrationForm, EditProfileForm
|
|
from ..email import send_email
|
|
from ..models import User
|
|
|
|
|
|
@auth.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('main.index'))
|
|
form = LoginForm()
|
|
if form.validate_on_submit():
|
|
user = User.query.filter_by(email=form.login.data).first()
|
|
if user is None:
|
|
user = User.query.filter_by(username=form.login.data).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 username or password.')
|
|
return render_template('auth/login.html.j2', form=form, title='Log in')
|
|
|
|
|
|
@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'))
|
|
form = RegistrationForm()
|
|
if form.validate_on_submit():
|
|
user = User(email=form.email.data.lower(),
|
|
username=form.username.data,
|
|
password=form.password.data)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
token = user.generate_confirmation_token()
|
|
send_email(user.email, 'Confirm Your Account',
|
|
'auth/email/confirm', user=user, token=token)
|
|
flash('A confirmation email has been sent to you by email.')
|
|
return redirect(url_for('auth.login'))
|
|
return render_template('auth/register.html.j2', form=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',
|
|
user=current_user, token=token)
|
|
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',
|
|
user=user, token=token)
|
|
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', 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('auth.login'))
|
|
else:
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/reset_password.html.j2', form=form,
|
|
title='Password Reset')
|
|
|
|
|
|
@auth.route('/settings', methods=['GET', 'POST'])
|
|
@login_required
|
|
def settings():
|
|
"""
|
|
View where loged in User can change own User information like Password etc.
|
|
"""
|
|
change_password_form = ChangePasswordForm()
|
|
if change_password_form.validate_on_submit():
|
|
if current_user.verify_password(change_password_form.old_password.data):
|
|
current_user.password = change_password_form.new_password.data
|
|
db.session.add(current_user)
|
|
db.session.commit()
|
|
flash('Your password has been updated.')
|
|
return redirect(url_for('auth.settings'))
|
|
else:
|
|
flash('Invalid password.')
|
|
change_profile_form = EditProfileForm(user=current_user)
|
|
if change_profile_form.validate_on_submit():
|
|
current_user.email = change_profile_form.email.data
|
|
db.session.add(current_user._get_current_object())
|
|
db.session.commit()
|
|
flash('Your email has been updated.')
|
|
change_profile_form.email.data = current_user.email
|
|
return render_template(
|
|
'auth/settings.html.j2',
|
|
change_password_form=change_password_form,
|
|
change_profile_form=change_profile_form,
|
|
title='Settings'
|
|
)
|
|
|
|
|
|
@auth.route('/settings/delete_self/<int:user_id>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def delete_self(user_id):
|
|
selected_user = User.query.filter_by(id=user_id).first()
|
|
db.session.delete(selected_user)
|
|
db.session.commit()
|
|
flash('Your account has been deleted!')
|
|
return redirect(url_for('main.index'))
|