From 635c3210b37ebd519d4d25c01f8d20ade4681bb2 Mon Sep 17 00:00:00 2001
From: Nils Beyer <nilsb@fsmpi.rwth-aachen.de>
Date: Fri, 24 Sep 2021 13:04:58 +0200
Subject: [PATCH] Fix: Auto-reload after SSO + display error message

---
 server.py             |  7 ++++++-
 templates/macros.html | 23 +++++++++++++++++++----
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/server.py b/server.py
index 3485ffd..30c57cc 100644
--- a/server.py
+++ b/server.py
@@ -324,6 +324,7 @@ def lecture(id, course=None, courseid=None): #pylint: disable=unused-argument,to
 	
 	password_set = False 	# Has the user set any username/password?
 	is_authorized = False	# Is the user authenticated?
+	courses_loaded = False  # Is the user logged into moodle/l2p but may not be in the course?
 
 	if request.authorization:
 		password_set = True
@@ -337,6 +338,9 @@ def lecture(id, course=None, courseid=None): #pylint: disable=unused-argument,to
 		if checkperm(perms):
 			is_authorized = True
 
+	if 'l2p_courses' in session or 'moodle_courses' in session:
+		courses_loaded = True
+
 	if not is_authorized:		
 		mode, text = permdescr(perms)
 		if mode == 'rwth':
@@ -363,7 +367,8 @@ def lecture(id, course=None, courseid=None): #pylint: disable=unused-argument,to
 			flash(text+'.', category='player')
 	return render_template('embed.html' if request.endpoint == 'embed' else 'lecture.html',
 			course=courses[0], lecture=lecture, videos=videos, chapters=chapters, seek=request.args.get('t'), 
-			isAuthorized=is_authorized, permtypes=permtypes(perms), passwordSet=password_set)
+			isAuthorized=is_authorized, permtypes=permtypes(perms), passwordSet=password_set,
+			courses_loaded=courses_loaded)
 
 @app.route('/<course>/<int:id>/login', methods = ['POST'])
 def sessionLogin(id, course): #pylint: disable=unused-argument,too-many-branches
diff --git a/templates/macros.html b/templates/macros.html
index adbb49f..dfc9d56 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -183,7 +183,7 @@ $(function() {
 					<div class="form-group">
 					  <label for="exampleInputPassword1">Passwort</label>
 					  <input type="password" class="form-control" id="password" name="password" placeholder="">
-					</div>
+					</div>	
 					<button type="submit" class="btn btn-default">Anmelden</button>
 				</form>
 			</div>
@@ -200,13 +200,28 @@ $(function() {
 				<h4 class="text-center"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> L2P/Moodle</h4>
 				<p>Für Teilnehmer der Veranstaltung verfügbar</p>
 				{% if 'l2p' in permtypes and not 'moodle' in permtypes %}
-				<a target="_blank" href="{{ url_for('start_l2pauth') }}" class="btn btn-default">Anmelden</a> 
+					{% if not courses_loaded %}
+						<a target="_blank" href="{{ url_for('start_l2pauth') }}" class="btn btn-default reloadonclose">Anmelden</a> 
+					{% else %}
+						<p class="alert alert-info">Du bist kein Teilnehmer des L2P-Kurses!</p>
+						<a target="_blank" href="{{ url_for('start_l2pauth') }}" class="btn btn-default reloadonclose">Kurse aktualisieren</a> 
+					{% endif %}
 				{% endif %}
 				{% if 'moodle' in permtypes and not 'l2p' in permtypes %}
-				<a target="_blank" href="{{ url_for('start_moodleauth') }}" class="btn btn-default">Anmelden</a> 
+					{% if not courses_loaded %}
+						<a target="_blank" href="{{ url_for('start_moodleauth') }}" class="btn btn-default reloadonclose">Anmelden</a> 
+					{% else %}
+						<p class="alert alert-info">Du bist kein Teilnehmer des Moodle-Kurses!</p>
+						<a target="_blank" href="{{ url_for('start_moodleauth') }}" class="btn btn-default reloadonclose">Kurse aktualisieren</a> 
+					{% endif %}
 				{% endif %}
 				{% if 'moodle' in permtypes and 'l2p' in permtypes %}
-				<a target="_blank" href="{{ url_for('start_moodlel2pauth') }}" class="btn btn-default">Anmelden</a> 
+					{% if not courses_loaded %}
+						<a target="_blank" href="{{ url_for('start_moodlel2pauth') }}" class="btn btn-default reloadonclose">Anmelden</a> 
+					{% else %}
+						<p class="alert alert-info">Du bist kein Teilnehmer des L2P oder Moodle-Kurses!</p>
+						<a target="_blank" href="{{ url_for('start_moodlel2pauth') }}" class="btn btn-default reloadonclose">Kurse aktualisieren</a> 
+					{% endif %}
 				{% endif %}
 			</div>
 			{% endif %}
-- 
GitLab