diff --git a/server/backend/api/app/crud/crud_user.py b/server/backend/api/app/crud/crud_user.py index 712de1333..72b2618ed 100644 --- a/server/backend/api/app/crud/crud_user.py +++ b/server/backend/api/app/crud/crud_user.py @@ -5,8 +5,8 @@ from api.app.constants import UserType, ApiInstanceEnv, IdimSearchUserParamType from api.app.models import model as models from api.app.crud import crud_utils -from api.app.crud.validator.target_user_validator import TargetUserValidator from api.app.integration.idim_proxy import IdimProxyService +from api.config import config from .. import schemas @@ -266,7 +266,9 @@ def update_user_info_from_idim_source( # get a requester from the database requester = ( db.query(models.FamUser) - .filter(models.FamUser.user_name == "CMENG") + .filter( + models.FamUser.user_name == config.get_requester_name_for_update_user_info() + ) .one_or_none() ) # setup IDIM web service @@ -277,8 +279,9 @@ def update_user_info_from_idim_source( # grab fam users from user table fam_users = get_users(db) - total_users_count = len(fam_users) - LOGGER.debug(f"Total number of users: {total_users_count}") + total_db_users_count = len(fam_users) + LOGGER.debug(f"Total number of users in database: {total_db_users_count}") + if use_pagination: fam_users = ( db.query(models.FamUser) @@ -350,7 +353,9 @@ def update_user_info_from_idim_source( else: # ignore bc service card users ignored_user_list.append(user.user_id) - LOGGER.debug(f"Updating information for user {user.user_name} is ignored because we only focus on IDIR and Business BCeID") + LOGGER.debug( + f"Updating information for user {user.user_name} is ignored because we only focus on IDIR and Business BCeID" + ) continue # Update various target_user fields from idim search if exists @@ -363,12 +368,14 @@ def update_user_info_from_idim_source( models.FamUser.business_guid: search_result.get("businessGuid"), } - update(db, user.user_id, properties_to_update, requester.cognito_user_id) + update( + db, user.user_id, properties_to_update, requester.cognito_user_id + ) LOGGER.debug(f"Updating information for user {user.user_name} is done") success_user_list.append(user.user_id) else: LOGGER.debug( - f"Invalid request, cannot find user {user.user_name} {user.user_guid} with user type {user.user_type_code}" + f"Cannot find user {user.user_name} {user.user_guid} with user type {user.user_type_code}" ) failed_user_list.append(user.user_id) @@ -378,11 +385,11 @@ def update_user_info_from_idim_source( return schemas.FamUserUpdateResponse( **{ - "total_users_count": total_users_count, + "total_db_users_count": total_db_users_count, "current_page": page, "users_count_on_page": len(fam_users), "success_user_id_list": success_user_list, "failed_user_id_list": failed_user_list, - "ignored_user_id_list": ignored_user_list + "ignored_user_id_list": ignored_user_list, } ) diff --git a/server/backend/api/app/main.py b/server/backend/api/app/main.py index 7d157f85f..1a4d3647b 100644 --- a/server/backend/api/app/main.py +++ b/server/backend/api/app/main.py @@ -132,7 +132,7 @@ def main(): ) app.include_router( router_user.router, - prefix=apiPrefix + "/user", + prefix=apiPrefix + "/users", dependencies=[Depends(router_guards.verify_api_key_for_update_user_info)], tags=["FAM User"], ) diff --git a/server/backend/api/app/routers/router_guards.py b/server/backend/api/app/routers/router_guards.py index a3b4dea94..54e508eb5 100644 --- a/server/backend/api/app/routers/router_guards.py +++ b/server/backend/api/app/routers/router_guards.py @@ -460,6 +460,6 @@ def enforce_bceid_terms_conditions_guard( def verify_api_key_for_update_user_info(x_api_key: str = Security(x_api_key)): if x_api_key != config.get_api_key_for_update_user_info(): utils.raise_http_exception( - status_code=HTTPStatus.FORBIDDEN, + status_code=HTTPStatus.UNAUTHORIZED, error_msg="Request needs api key.", ) \ No newline at end of file diff --git a/server/backend/api/app/routers/router_user.py b/server/backend/api/app/routers/router_user.py index fa89be7b6..c58e9843a 100644 --- a/server/backend/api/app/routers/router_user.py +++ b/server/backend/api/app/routers/router_user.py @@ -11,7 +11,7 @@ router = APIRouter() -@router.post("", status_code=HTTPStatus.OK, response_model=FamUserUpdateResponse) +@router.put("/users-information", status_code=HTTPStatus.OK, response_model=FamUserUpdateResponse) def update_user_information_from_idim_source( page: int = 1, per_page: int = 100, diff --git a/server/backend/api/app/schemas.py b/server/backend/api/app/schemas.py index af0c431f9..6326a78e7 100644 --- a/server/backend/api/app/schemas.py +++ b/server/backend/api/app/schemas.py @@ -74,7 +74,7 @@ class FamUserInfo(BaseModel): class FamUserUpdateResponse(BaseModel): - total_users_count: int + total_db_users_count: int current_page: int users_count_on_page: int success_user_id_list: List[int] diff --git a/server/backend/api/config/config.py b/server/backend/api/config/config.py index 28d9c7b4d..67bab2952 100644 --- a/server/backend/api/config/config.py +++ b/server/backend/api/config/config.py @@ -193,4 +193,7 @@ def is_bcsc_key_enabled(): return os.environ.get("ENABLE_BCSC_JWKS_ENDPOINT", "True") == "True" def get_api_key_for_update_user_info(): - return os.environ.get("FAM_UPDATE_USER_INFO_API_KEY") \ No newline at end of file + return os.environ.get("FAM_UPDATE_USER_INFO_API_KEY") + +def get_requester_name_for_update_user_info(): + return os.environ.get("FAM_UPDATE_USER_INFO_REQUESTER_NAME") or 'CMENG' \ No newline at end of file