feat: add user editing and deletion functionality for admins
This commit is contained in:
parent
fb301ab9a7
commit
1a90920cd3
@ -70,3 +70,27 @@ def get_users():
|
||||
'role': user.role,
|
||||
'last_login': user.last_login
|
||||
} for user in users])
|
||||
|
||||
@user_bp.route('/users/<int:user_id>', methods=['PUT'])
|
||||
def edit_user(user_id):
|
||||
if not authenticate() or g.user.role != 'admin':
|
||||
return jsonify({'message': 'Unauthorized access! Only admins can edit users.'}), 401
|
||||
data = request.json
|
||||
user = User.query.get(user_id)
|
||||
if not user:
|
||||
return jsonify({'message': 'User not found!'}), 404
|
||||
user.username = data.get('username', user.username)
|
||||
user.role = data.get('role', user.role)
|
||||
db.session.commit()
|
||||
return jsonify({'message': 'User updated!'}), 200
|
||||
|
||||
@user_bp.route('/users/<int:user_id>', methods=['DELETE'])
|
||||
def delete_user(user_id):
|
||||
if not authenticate() or g.user.role != 'admin':
|
||||
return jsonify({'message': 'Unauthorized access! Only admins can delete users.'}), 401
|
||||
user = User.query.get(user_id)
|
||||
if not user:
|
||||
return jsonify({'message': 'User not found!'}), 404
|
||||
db.session.delete(user)
|
||||
db.session.commit()
|
||||
return jsonify({'message': 'User deleted!'}), 200
|
||||
|
Loading…
Reference in New Issue
Block a user