To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 57ba3080 authored by Mathis Dedial's avatar Mathis Dedial
Browse files

Kill Flask login

parent dc907c44
Pipeline #2727 skipped with stage
from flask import Flask, g, session
from flask_login import LoginManager, UserMixin
from .models import db
from nethz.ldap import AuthenticatedLdap
......@@ -12,41 +11,7 @@ app.secret_key = app.config['SECRET_KEY']
ldap_connector = AuthenticatedLdap(app.config['LDAP_USERNAME'],
app.config['LDAP_PASSWORD'])
# Initialize login manager
login_manager = LoginManager()
login_manager.init_app(app)
# Bind SQLAlchemy to Flask app
db.init_app(app)
class BurgerUser(UserMixin):
'''
Custom user model
'''
is_active = True
def __init__(self, nethz):
self.nethz = nethz
def get_id(self):
return self.nethz
@login_manager.user_loader
def load_burger_user(nethz):
return BurgerUser(nethz)
@app.before_request
def set_ldap_test_user():
'''
Set fake LDAP user on g.user for when LDAP is not available
Enabled only if LDAP_TEST_USER is set and flask_simpleldap is not working
'''
ldap_test_user = app.config.get('LDAP_TEST_USER')
if ldap_test_user and not hasattr(g, 'user'):
g.user = ldap_test_user
from app import views
......@@ -4,7 +4,6 @@ Contains all views
from app import app, ldap_connector, BurgerUser
from datetime import datetime
from flask import flash, g, redirect, render_template, request, session, url_for
from flask_login import login_user, login_required
from sqlalchemy.sql import text
from .mappings import BREAD, PATTY, TOPPINGS, SIDE
from .models import db, Burger, Vote
......@@ -34,11 +33,13 @@ def index():
@app.route('/vote', methods=['POST'])
@login_required
def vote():
'''
Handles POST requests for voting
'''
if not 'user' in session:
return redirect(url_for('login'))
def die(message):
'''
Display an error-level flash message and return
......@@ -86,8 +87,9 @@ def login():
'''
Login page. LDAP-Login only.
'''
if getattr(session, 'user', None):
if 'user' in session:
return redirect(url_for('index'))
if request.method == 'POST':
user = request.form['user']
passwd = request.form['passwd']
......@@ -96,11 +98,7 @@ def login():
if test is None or passwd == '':
flash('Invalid credentials', 'error')
else:
setattr(session, 'user', user)
# Create instance of BurgerUser
burger_user = BurgerUser(user)
# Log in BurgerUser
login_user(burger_user)
session['user'] = user
return redirect(url_for('index'))
return render_template('login.html')
......@@ -110,17 +108,18 @@ def logout():
'''
Logout page
'''
session.pop('nethz', None)
session.pop('user', None)
flash('Successfully logged out.')
return redirect(url_for('index'))
@app.route('/new', methods=['GET', 'POST'])
@login_required
def create():
'''
Burger creator
'''
if not 'user' in session:
return redirect(url_for('login'))
def die(message):
'''
......@@ -158,7 +157,7 @@ def create():
return die('Please describe your burger in a few short sentences.')
# Check that the user hasn't already created a burger
if Burger.query.filter_by(nethz=getattr(session, 'user', None)).first():
if Burger.query.filter_by(nethz=session['user']).first():
# In this case, send the user back to the voting page
flash('You can only create one burger.', 'error')
return redirect(url_for('index'))
......@@ -167,7 +166,7 @@ def create():
burger = Burger(
name=name,
description=description,
nethz=getattr(session, 'user', None),
nethz=session['user'],
timestamp=datetime.now(),
bread=bread,
patty=patty,
......
......@@ -11,6 +11,7 @@ lazy-object-proxy==1.3.1
ldap3==2.4.1
MarkupSafe==1.0
mccabe==0.6.1
nethz==1.0
pkg-resources==0.0.0
pyasn1==0.4.2
pyldap==2.4.45
......@@ -20,4 +21,3 @@ six==1.11.0
SQLAlchemy==1.2.3
Werkzeug==0.14.1
wrapt==1.10.11
-e git+https://github.com/NotSpecial/nethz.git@1d3004081c3618f1f41463476a847b0bddd6d91a#egg=nethz
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment