Skip to content
Snippets Groups Projects
Commit 726fda6f authored by Andreas Valder's avatar Andreas Valder
Browse files

Merge branch 'master' of git.fsmpi.rwth-aachen.de:julianundandyfrickelnkram/videoagwebsite

parents 25297e65 1ce40ec8
No related branches found
No related tags found
No related merge requests found
#!/bin/python #!/bin/python
from flask import Flask, render_template, g, request, url_for, redirect, session from flask import Flask, render_template, g, request, url_for, redirect, session
import mysql.connector
import sqlite3 import sqlite3
import os import os
#import ldap3
import re import re
app = Flask(__name__) app = Flask(__name__)
...@@ -43,6 +41,7 @@ def dict_factory(cursor, row): ...@@ -43,6 +41,7 @@ def dict_factory(cursor, row):
def query(operation, *params): def query(operation, *params):
if config['DB_ENGINE'] == 'mysql': if config['DB_ENGINE'] == 'mysql':
import mysql.connector
if 'db' not in g or not g.db.is_connected(): if 'db' not in g or not g.db.is_connected():
g.db = mysql.connector.connect(user=config['MYSQL_USER'], password=config['MYSQL_PASSWD'], host=config['MYSQL_HOST'], database=config['MYSQL_DB']) g.db = mysql.connector.connect(user=config['MYSQL_USER'], password=config['MYSQL_PASSWD'], host=config['MYSQL_HOST'], database=config['MYSQL_DB'])
cur = g.db.cursor(dictionary=True) cur = g.db.cursor(dictionary=True)
...@@ -78,6 +77,7 @@ def ldapauth(user, password): ...@@ -78,6 +77,7 @@ def ldapauth(user, password):
notldap = {'videoag':('videoag', ['users','videoag']), 'gustav':('passwort', ['users'])} notldap = {'videoag':('videoag', ['users','videoag']), 'gustav':('passwort', ['users'])}
user = LDAP_USERRE.sub(r'', user.lower()) user = LDAP_USERRE.sub(r'', user.lower())
if 'LDAP_HOST' in config: if 'LDAP_HOST' in config:
import ldap3
try: try:
conn = ldap3.Connection(config['LDAP_HOST'], 'uid=%s,ou=users,dc=fsmpi,dc=rwth-aachen,dc=de'%user, password, auto_bind=True) conn = ldap3.Connection(config['LDAP_HOST'], 'uid=%s,ou=users,dc=fsmpi,dc=rwth-aachen,dc=de'%user, password, auto_bind=True)
if conn.search("ou=groups,dc=fsmpi,dc=rwth-aachen,dc=de", "(&(cn=*)(memberUid=%s))"%user, attributes=['cn']): if conn.search("ou=groups,dc=fsmpi,dc=rwth-aachen,dc=de", "(&(cn=*)(memberUid=%s))"%user, attributes=['cn']):
...@@ -90,6 +90,20 @@ def ldapauth(user, password): ...@@ -90,6 +90,20 @@ def ldapauth(user, password):
return user, notldap[user][1] return user, notldap[user][1]
return None, [] return None, []
def ldapget(user):
notldap = {'videoag': {'uid': 'videoag', 'givenName': 'Video', 'sn': 'Geier'},
'gustav': {'uid': 'gustav', 'givenName': 'Gustav', 'sn': 'Geier'}}
user = LDAP_USERRE.sub(r'', user.lower())
if 'LDAP_HOST' in config:
import ldap3
conn = ldap3.Connection('ldaps://rumo.fsmpi.rwth-aachen.de', auto_bind=True)
conn.search("ou=users,dc=fsmpi,dc=rwth-aachen,dc=de", "(uid=%s)"%user,
attributes=ldap3.ALL_ATTRIBUTES)
e = conn.entries[0]
return {'uid': user, 'givenName': e.givenName.value, 'sn':e.sn.value}
else:
return notldap[user]
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html', latestvideos=query(''' return render_template('index.html', latestvideos=query('''
...@@ -153,5 +167,23 @@ def course(): ...@@ -153,5 +167,23 @@ def course():
else: else:
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/login', methods=['POST'])
def login():
user, groups = ldapauth(request.form.get('user'), request.form.get('password'))
if user and 'users' in groups:
session['user'] = ldapget(user)
if 'ref' in request.values:
return redirect(request.values['ref'])
else:
return redirect(url_for('index'))
@app.route('/logout')
def logout():
session.pop('user')
if 'ref' in request.values:
return redirect(request.values['ref'])
else:
return redirect(url_for('index'))
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
</li> </li>
{% endfor %} {% endfor %}
<li class="navbar-right"> <li class="navbar-right">
{% if not session.userid is defined %} {% if not session.user is defined %}
<a id="loginpopover" data-container="body" data-toggle="popover" data-placement="bottom"> <a id="loginpopover" data-container="body" data-toggle="popover" data-placement="bottom">
<span class="glyphicon glyphicon-log-in"></span> <span class="glyphicon glyphicon-log-in"></span>
</a> </a>
...@@ -70,7 +70,8 @@ ...@@ -70,7 +70,8 @@
) )
</script> </script>
{% else %} {% else %}
<a herf="/logout"> <a href="/logout?ref={{ request.url|urlencode }}">
{{ session.user.givenName }}
<span class="glyphicon glyphicon-log-out"></span> <span class="glyphicon glyphicon-log-out"></span>
</a> </a>
{% endif %} {% endif %}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment