Skip to content

Commit

Permalink
adding user add/update/verify functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillipsOwen committed Nov 27, 2024
1 parent e35155a commit 28b7b90
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
14 changes: 4 additions & 10 deletions src/common/pg_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,13 @@ def get_instance_names(self, name: str, project_code: str = None) -> EnumType:
# Return Pandas dataframe
return ret_val

def verify_user(self, email: str, password_hash: str) -> dict:
def verify_user(self, email: str) -> dict:
"""
verifies the user has an account and the password is correct.
if the verification is successful, return a JSON object with pass/fail and user account data
:param email:
:param password_hash:
:return:
"""
# init the return value:
Expand All @@ -621,14 +620,8 @@ def verify_user(self, email: str, password_hash: str) -> dict:
else:
email = f"'{email}'"

# prep the password param for the SP
if password_hash is None:
password_hash = 'null'
else:
password_hash = f"'{password_hash}'"

# build the query. this will also return the users profile
sql = f"SELECT verify_user(_email := {email}, _password_hash := {password_hash});"
sql = f"SELECT verify_user(_email := {email});"

# get the info
ret_val = self.exec_sql('apsviz', sql)
Expand Down Expand Up @@ -671,7 +664,8 @@ def update_user(self, **kwargs) -> dict:
ret_val = None

# create the SQL query
sql = f"SELECT public.update_user({kwargs['email']}, _password_hash:={kwargs['password_hash']}, _role_id:={kwargs['role_id']}, _details:={kwargs['details']});"
sql = (f"SELECT public.update_user(_email:={kwargs['email']}, _password_hash:={kwargs['password_hash']}, _role_id:={kwargs['role_id']}, "
f"_details:={kwargs['details']});")

# get the info
ret_val = self.exec_sql('apsviz', sql)
Expand Down
28 changes: 9 additions & 19 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,12 @@ async def get_pulldown_data(grid_type: Union[str, None] = Query(default=None), e
return JSONResponse(content=ret_val, status_code=status_code, media_type="application/json")


@APP.get('/verify_user', status_code=200, response_model=None)
async def verify_user(email: Union[str, None] = Query(default=None), password_hash: Union[str, None] = Query(default=None)):
@APP.get('/verify_user', dependencies=[Depends(JWTBearer(security))], status_code=200, response_model=None)
async def verify_user(email: Union[str, None] = Query(default=None)):
"""
Verifies that the user exists and returns their profile if they do.
<br/>&nbsp;&nbsp;&nbsp;The user's email address
<br/>&nbsp;&nbsp;&nbsp;The user's password (hashed)
"""
# pylint: disable=locally-disabled, unused-argument

Expand All @@ -797,20 +796,11 @@ async def verify_user(email: Union[str, None] = Query(default=None), password_ha

try:
# try to make the call for records
ret_val: dict = db_info.verify_user(email, password_hash)
ret_val: dict = db_info.verify_user(email)

# check the return
if ret_val['success']:
# create the JWT payload
payload = {'bearer_name': os.environ.get("BEARER_NAME"), 'bearer_secret': os.environ.get("BEARER_SECRET")}

# create an access token
token = security.sign_jwt(payload)

# create a new dict element with the JWT token
ret_val['token'] = token['access_token']
# the verification was not successful
else:
if not ret_val['success']:
# set the error
ret_val = {'Error': "Could not verify the user's credentials."}

# set the status to a server error
Expand All @@ -830,7 +820,7 @@ async def verify_user(email: Union[str, None] = Query(default=None), password_ha
return JSONResponse(content=ret_val, status_code=status_code, media_type="application/json")


@APP.get('/update_user', status_code=200, response_model=None)
@APP.get('/update_user', dependencies=[Depends(JWTBearer(security))], status_code=200, response_model=None)
async def update_user(email: Union[str, None] = Query(default=None), password_hash: Union[str, None] = Query(default=None),
role_id: Union[str, None] = Query(default=None), details: Union[str, None] = Query(default=None)):
"""
Expand Down Expand Up @@ -862,7 +852,7 @@ async def update_user(email: Union[str, None] = Query(default=None), password_ha
ret_val: dict = db_info.update_user(**kwargs)

# check the return
if not ret_val['success']:
if ret_val == -1 or not ret_val['success']:
ret_val = {'Error': 'Database error updating the users information.'}

# set the status to a server error
Expand All @@ -882,7 +872,7 @@ async def update_user(email: Union[str, None] = Query(default=None), password_ha
return JSONResponse(content=ret_val, status_code=status_code, media_type="application/json")


@APP.get('/add_user', status_code=200, response_model=None)
@APP.get('/add_user', dependencies=[Depends(JWTBearer(security))], status_code=200, response_model=None)
async def add_user(email: Union[str, None] = Query(default=None), password_hash: Union[str, None] = Query(default=None),
role_id: Union[str, None] = Query(default=None), details: Union[str, None] = Query(default=None)):
"""
Expand Down Expand Up @@ -914,7 +904,7 @@ async def add_user(email: Union[str, None] = Query(default=None), password_hash:
ret_val: dict = db_info.add_user(**kwargs)

# check the return
if not ret_val['success']:
if ret_val == -1 or not ret_val['success']:
ret_val = {'Error': 'Database error adding the user.'}

# set the status to a server error
Expand Down

0 comments on commit 28b7b90

Please sign in to comment.