Coverage for app/logic/loginManager.py: 25%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-11-22 21:05 +0000

1from flask import request, session, abort 

2from peewee import DoesNotExist 

3 

4from app import app 

5from app.models.user import User 

6from app.models.term import Term 

7 

8 

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() 

16 

17 url ="/" 

18 if app.config['use_shibboleth']: 

19 url = "/Shibboleth.sso/Logout" 

20 return url 

21 

22def getUsernameFromEnvironment(): 

23 if "username" in session: 

24 username = session["username"] 

25 

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) 

31 

32 username = request.environ[shibKey].split("@")[0].split('/')[-1].lower() 

33 return username 

34 

35 else: 

36 return app.config['default_user'] 

37 

38def getLoginUser(): 

39 username = getUsernameFromEnvironment() 

40 

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") 

46 

47 # or fail 

48 

49 if 'username' not in session: 

50 print("Logging in as", user.username) 

51 session['username'] = user.username 

52 

53 return user 

54 

55def getCurrentTerm(): 

56 return Term.get_or_none(isCurrentTerm = True)