Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all usage of AuthUser replace .get methods #1142

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,35 +141,33 @@ async def my_data(
user_data(dict): The dict of user data.
"""
# Save user info in User table
user = await user_crud.get_user(db, user_data["id"])
user = await user_crud.get_user(db, user_data.id)
if not user:
user_by_username = await user_crud.get_user_by_username(
db, user_data["username"]
)
user_by_username = await user_crud.get_user_by_username(db, user_data.username)
if user_by_username:
raise HTTPException(
status_code=400,
detail=(
f"User with this username {user_data['username']} already exists. "
f"User with this username {user_data.username} already exists. "
"Please contact the administrator."
),
)

# Add user to database
db_user = DbUser(
id=user_data["id"],
username=user_data["username"],
profile_img=user_data["img_url"],
id=user_data.id,
username=user_data.username,
profile_img=user_data.img_url,
)
db.add(db_user)
db.commit()
# Append role
user_data["role"] = db_user.role
user_data.role = db_user.role
else:
if user_data.get("img_url"):
user.profile_img = user_data["img_url"]
if user_data.img_url:
user.profile_img = user_data.img_url
db.commit()
# Append role
user_data["role"] = user.role
user_data.role = user.role

return JSONResponse(content={"user_data": user_data}, status_code=200)
return user_data
10 changes: 5 additions & 5 deletions src/backend/app/auth/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
and always return an AuthUser object in a standard format.
"""

from typing import Optional
from typing import Optional, Union

from fastapi import Depends, HTTPException
from loguru import logger as log
Expand Down Expand Up @@ -50,7 +50,7 @@ async def get_uid(user_data: AuthUser) -> int:

async def check_super_admin(
db: Session,
user: [AuthUser, int],
user: Union[AuthUser, int],
) -> DbUser:
"""Database check to determine if super admin role."""
if isinstance(user, int):
Expand Down Expand Up @@ -81,7 +81,7 @@ async def super_admin(

async def check_org_admin(
db: Session,
user: [AuthUser, int],
user: Union[AuthUser, int],
project: Optional[DbProject],
org_id: Optional[int],
) -> DbUser:
Expand All @@ -98,8 +98,8 @@ async def check_org_admin(
await check_org_exists(db, org_id)

# If user is admin, skip checks
if await check_super_admin(db, user):
return user
if db_user := await check_super_admin(db, user):
return db_user

return (
db.query(organisation_managers)
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/src/utilfunctions/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const createLoginWindow = (redirectTo) => {
.then((resp) => resp.json())
.then((userRes) => {
const params = new URLSearchParams({
username: userRes.user_data.username,
username: userRes.username,
osm_oauth_token: res.access_token,
id: userRes.user_data.id,
picture: userRes.user_data.img_url,
id: userRes.id,
picture: userRes.img_url,
redirect_to: redirectTo,
}).toString();
const redirectUrl = `/osmauth?${params}`;
Expand Down
Loading