Coverage for app/logic/loginManager.py: 25%
36 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-10-24 20:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-10-24 20:53 +0000
1from flask import request, session, abort
2from peewee import DoesNotExist
4from app import app
5from app.models.user import User
6from app.models.term import Term
9def logout():
10 """
11 Erases the session and returns the URL for redirection
12 """
13 if 'username' in session:
14 print("Logging out", session['username'])
15 session.clear()
17 url ="/"
18 if app.config['use_shibboleth']:
19 url = "/Shibboleth.sso/Logout"
20 return url
22def getUsernameFromEnvironment():
23 if "username" in session:
24 username = session["username"]
26 shibKey = "eppn"
27 if app.config['use_shibboleth']:
28 if not shibKey in request.environ:
29 print("No Shibboleth session present! Aborting")
30 abort(403)
32 username = request.environ[shibKey].split("@")[0].split('/')[-1].lower()
33 return username
35 else:
36 return app.config['default_user']
38def getLoginUser():
39 username = getUsernameFromEnvironment()
41 try:
42 user = User.get_by_id(username)
43 except DoesNotExist as e:
44 # Find the user by email
45 user = User.get(email=f"{username}@berea.edu")
47 # or fail
49 if 'username' not in session:
50 print("Logging in as", user.username)
51 session['username'] = user.username
53 return user
55def getCurrentTerm():
56 return Term.get_or_none(isCurrentTerm = True)