Skip to content

Commit

Permalink
Fix accept terms endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Nov 21, 2024
1 parent 570f9a3 commit 9e73c06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
39 changes: 23 additions & 16 deletions backend/src/xfd_django/xfd_api/api_methods/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,10 @@ def get_me(current_user):
return user_dict


async def accept_terms(request: Request):
"""
Accept the latest terms of service.
Args:
request : The HTTP request containing the user and the terms version.
Returns:
User: The updated user.
"""
def accept_terms(version_data, current_user):
"""Accept the latest terms of service."""
try:
current_user = request.state.user
if not current_user:
raise HTTPException(status_code=401, detail="User not authenticated.")

body = await request.json()
version = body.get("version")
version = version_data.version
if not version:
raise HTTPException(
status_code=400, detail="Missing version in request body."
Expand All @@ -96,7 +84,26 @@ async def accept_terms(request: Request):
current_user.acceptedTermsVersion = version
current_user.save()

return UserSchema.model_validate(current_user)
return {
"id": str(current_user.id),
"cognitoId": current_user.cognitoId,
"oktaId": current_user.oktaId,
"loginGovId": current_user.loginGovId,
"createdAt": current_user.createdAt.isoformat() if current_user.createdAt else None,
"updatedAt": current_user.updatedAt.isoformat() if current_user.updatedAt else None,
"firstName": current_user.firstName,
"lastName": current_user.lastName,
"fullName": current_user.fullName,
"email": current_user.email,
"invitePending": current_user.invitePending,
"loginBlockedByMaintenance": current_user.loginBlockedByMaintenance,
"dateAcceptedTerms": current_user.dateAcceptedTerms.isoformat() if current_user.dateAcceptedTerms else None,
"acceptedTermsVersion": current_user.acceptedTermsVersion,
"lastLoggedIn": current_user.lastLoggedIn.isoformat() if current_user.lastLoggedIn else None,
"userType": current_user.userType,
"regionId": current_user.regionId,
"state": current_user.state,
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

Expand Down
5 changes: 5 additions & 0 deletions backend/src/xfd_django/xfd_api/schema_models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ class RegisterUserResponse(BaseModel):

statusCode: int
body: str

class VersionModel(BaseModel):
"""Version model."""

version: str
22 changes: 7 additions & 15 deletions backend/src/xfd_django/xfd_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
from .schema_models.search import SearchBody, SearchRequest, SearchResponse
from .schema_models.service import ServicesStat
from .schema_models.severity_count import SeverityCountSchema
from .schema_models.user import NewUser, NewUserResponseModel, RegisterUserResponse
from .schema_models.user import NewUser, NewUserResponseModel, RegisterUserResponse, VersionModel
from .schema_models.user import User as UserSchema
from .schema_models.user import UserResponse
from .schema_models.vulnerability import Vulnerability as VulnerabilitySchema
Expand Down Expand Up @@ -291,7 +291,7 @@ async def export_vulnerabilities():

@api_router.get(
"/vulnerabilities/{vulnerabilityId}",
# dependencies=[Depends(get_current_active_user)],
dependencies=[Depends(get_current_active_user)],
response_model=VulnerabilitySchema,
tags=["Vulnerabilities"],
)
Expand All @@ -306,7 +306,7 @@ async def call_get_vulnerability_by_id(vuln_id):

@api_router.put(
"/vulnerabilities/{vulnerabilityId}",
# dependencies=[Depends(get_current_active_user)],
dependencies=[Depends(get_current_active_user)],
response_model=VulnerabilitySchema,
tags="Update vulnerability",
)
Expand Down Expand Up @@ -360,19 +360,11 @@ async def callback_route(request: Request):
# ========================================


@api_router.post("/users/me/acceptTerms", tags=["Users"])
async def call_accept_terms(request: Request):
"""
Accept the latest terms of service.
Args:
request : The HTTP request containing the user and the terms version.
Returns:
User: The updated user.
"""
@api_router.post("/users/me/acceptTerms", response_model=UserSchema, dependencies=[Depends(get_current_active_user)], tags=["Users"])
async def call_accept_terms(version_data: VersionModel, current_user: User = Depends(get_current_active_user)):
"""Accept the latest terms of service."""

return accept_terms(request)
return accept_terms(version_data, current_user)


# GET Current User
Expand Down

0 comments on commit 9e73c06

Please sign in to comment.