Check for user with matching Microsoft UUID first
This commit is contained in:
parent
1ffcdc3ec1
commit
ba29ecbc73
1 changed files with 17 additions and 8 deletions
|
@ -39,19 +39,28 @@ def authorized() -> typing.Any:
|
|||
client.set_token(resp.data)
|
||||
|
||||
resp = client.users.get_me()
|
||||
username = resp.data['userPrincipalName']
|
||||
microsoft_uuid = resp.data['id']
|
||||
username = resp.data['userPrincipalName']
|
||||
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if username and user:
|
||||
return login_and_redirect_user(user)
|
||||
elif username:
|
||||
user = create_user(username, microsoft_uuid)
|
||||
return login_and_redirect_user(user)
|
||||
|
||||
# Fail if fields are not populated
|
||||
if not microsoft_uuid or not username:
|
||||
flash("You're not allowed to enter, please contact a system administrator")
|
||||
return redirect(url_for("general_bp.home"))
|
||||
|
||||
# Find existing user by Microsoft UUID (userPrincipalName can change)
|
||||
user = User.query.filter_by(microsoft_uuid=microsoft_uuid).first()
|
||||
if user:
|
||||
return login_and_redirect_user(user)
|
||||
|
||||
# Find existing user by username (pre-existing account)
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user:
|
||||
return login_and_redirect_user(user)
|
||||
|
||||
# No user found, create a new one
|
||||
user = create_user(username, microsoft_uuid=microsoft_uuid)
|
||||
return login_and_redirect_user(user)
|
||||
|
||||
|
||||
def login_and_redirect_user(user) -> Response:
|
||||
"""Log in the user and then redirect them"""
|
||||
|
@ -59,7 +68,7 @@ def login_and_redirect_user(user) -> Response:
|
|||
return redirect(url_for("general_bp.home"))
|
||||
|
||||
|
||||
def create_user(username, microsoft_uuid) -> User:
|
||||
def create_user(username, *, microsoft_uuid) -> User:
|
||||
"""Create a temporary user if it is needed"""
|
||||
user = User()
|
||||
user.configure(username, False, 1, microsoft_uuid=microsoft_uuid)
|
||||
|
|
Loading…
Reference in a new issue