Added deletion of users. Prep for db update

This commit is contained in:
Thastertyn 2024-03-07 15:04:34 +01:00
parent 0202a40228
commit 80caa02d74
2 changed files with 24 additions and 13 deletions

View File

@ -101,4 +101,14 @@ def update_password():
@bp_user.route('/delete', methods=['DELETE']) @bp_user.route('/delete', methods=['DELETE'])
@jwt_required() @jwt_required()
def delete_user(): def delete_user():
return abort(501) user_id = get_jwt_identity()
result, status_code = UserService.delete_user(user_id)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp)
return result, status_code

View File

@ -50,15 +50,7 @@ class UserService:
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
try: try:
db_cursor.execute("select max(user_id) as max_id from user") db_cursor.execute("insert into user (username, email, password, role_id) values (%s, %s, %s, 1)", (username, email, hashed_password))
last_id = db_cursor.fetchone()['max_id']
if last_id < 23000:
return {"Failed": "Error occurred when fetching last user id"}
new_id = last_id + 1
db_cursor.execute("insert into user (username, email, password, user_id, role_id) values (%s, %s, %s, %s, 1)", (username, email, hashed_password, new_id))
db_connection.commit() db_connection.commit()
except Error as e: except Error as e:
print(f"Error: {e}") print(f"Error: {e}")
@ -79,12 +71,11 @@ class UserService:
:rtype: Tuple[Union[dict, str], int] :rtype: Tuple[Union[dict, str], int]
""" """
db_cursor.execute("select user_id, password, last_change from user where username = %s", (username,)) db_cursor.execute("select user_id, password from user where username = %s", (username,))
result = db_cursor.fetchone() result = db_cursor.fetchone()
user_id = result['user_id'] user_id = result['user_id']
password_hash = result['password'] password_hash = result['password']
last_change = result['last_change']
if user_id is None: if user_id is None:
return {"Failed": "Username not found"}, 400 return {"Failed": "Username not found"}, 400
@ -94,7 +85,7 @@ class UserService:
expire = datetime.timedelta(hours=1) expire = datetime.timedelta(hours=1)
token = create_access_token(identity=user_id, expires_delta=expire,additional_claims={"lm": last_change}) token = create_access_token(identity=user_id, expires_delta=expire)
return {"token": token}, 200 return {"token": token}, 200
@ -114,6 +105,16 @@ class UserService:
return {"Success": "Successfully logged out"}, 200 return {"Success": "Successfully logged out"}, 200
@staticmethod
def delete_user(user_id: str) -> Tuple[Union[dict, str], int]:
try:
db_cursor.execute("delete from user where user_id = %s", (user_id,))
db_connection.commit()
except Error as e:
return {"Failed": f"Failed to delete user. {e}"}, 500
return {"Success": "User successfully deleted"}, 200
@staticmethod @staticmethod
def update_email(user_id: str, new_email: str) -> Tuple[Union[dict, str], int]: def update_email(user_id: str, new_email: str) -> Tuple[Union[dict, str], int]:
""" """