22from flask import request , jsonify , make_response
33from flask_restful import Resource , abort
44from flask_bcrypt import check_password_hash
5- from email_validator import validate_email , EmailNotValidError
5+ from email_validator import EmailNotValidError
66from sqlalchemy .exc import SQLAlchemyError , IntegrityError
77from configparser import NoOptionError
88from pbench .server .database .models .users import User
@@ -119,40 +119,19 @@ def post(self):
119119 400 , message = "Missing lastName field" ,
120120 )
121121
122- # validate the email field
123- try :
124- valid = validate_email (email_id )
125- email = valid .email
126- except EmailNotValidError :
127- self .logger .warning ("Invalid email {}" , email_id )
128- abort (400 , message = f"Invalid email: { email_id } " )
129-
130- # check if user already exist
131- try :
132- user = User .query (username = user_data .get ("username" ))
133- except Exception :
134- self .logger .exception ("Exception while querying user" )
135- abort (500 , message = "INTERNAL ERROR" )
136-
137- if user :
138- self .logger .warning (
139- "A user tried to re-register. Username: {}" , user .username
140- )
141- abort (403 , message = "A user with that name already exists." )
142-
143122 try :
144123 user = User (
145124 username = username ,
146125 password = password ,
147126 first_name = first_name ,
148127 last_name = last_name ,
149- email = email ,
128+ email = email_id ,
150129 )
151130
152131 # insert the user
153132 user .add ()
154133 self .logger .info (
155- "New user registered, username: {}, email: {}" , username , email
134+ "New user registered, username: {}, email: {}" , username , email_id
156135 )
157136
158137 response_object = {
@@ -162,6 +141,9 @@ def post(self):
162141 response = jsonify (response_object )
163142 response .status_code = 201
164143 return make_response (response , 201 )
144+ except EmailNotValidError :
145+ self .logger .warning ("Invalid email {}" , email_id )
146+ abort (400 , message = f"Invalid email: { email_id } " )
165147 except Exception :
166148 self .logger .exception ("Exception while registering a user" )
167149 abort (500 , message = "INTERNAL ERROR" )
@@ -385,17 +367,27 @@ def get(self, username):
385367 abort (500 , message = "INTERNAL ERROR" )
386368
387369 # TODO: Check if the user has the right privileges
388- if verified or user .is_admin ():
370+ if verified :
371+ return_data = user .get_json ()
389372 response_object = {
390373 "status" : "success" ,
391- "data" : {
392- "email" : user .email ,
393- "first_name" : user .first_name ,
394- "last_name" : user .last_name ,
395- "registered_on" : user .registered_on ,
396- },
374+ "data" : return_data ,
397375 }
398376 return make_response (jsonify (response_object ), 200 )
377+ elif user .is_admin ():
378+ try :
379+ target_user = User .query (username = username )
380+ return_data = target_user .get_json ()
381+ response_object = {
382+ "status" : "success" ,
383+ "data" : return_data ,
384+ }
385+ return make_response (jsonify (response_object ), 200 )
386+ except Exception :
387+ self .logger .exception (
388+ "Exception occurred while querying the user. Username: {}" , username
389+ )
390+ abort (500 , message = "INTERNAL ERROR" )
399391 else :
400392 self .logger .warning (
401393 "Username retrieved from the auth token {} is different from the username provided" ,
@@ -448,23 +440,28 @@ def put(self, username):
448440
449441 # TODO: Check if the user has the right privileges
450442 if verified :
451- try :
452- # Log if the user payload contain fields that are either non-updatabale or
453- # are not present in the user db.
454- non_updatable = list (
455- set (post_data .keys ()) - set (User .__table__ .columns .keys ())
443+ # Check if the user payload contain fields that are either non-updatabale or
444+ # are not present in the user db. If any key in the payload does not match
445+ # with the column name we will abort the update request.
446+ non_updatable = list (
447+ set (post_data .keys ()) - set (User .__table__ .columns .keys ())
448+ )
449+ if non_updatable :
450+ self .logger .warning (
451+ "User trying to update fields that are not present in the user database. Fields: {}" ,
452+ non_updatable ,
456453 )
457- if "registered_on" in post_data .keys ():
458- non_updatable .append ("registered_on" )
459- if non_updatable :
460- self .logger .warning (
461- "User trying to update fields that are either non-updatable or does not present in the user database. Fields: {}" ,
462- non_updatable ,
463- )
454+ abort (400 , message = "Invalid fields in update request payload" )
455+ try :
464456 # We will update the user object with the keys and values provided in the request payload.
465- # THe keys need to match with the column names in the user model. However, if any key in
466- # the payload does not match with the column name we just skip that field.
457+ # THe keys need to match with the column names in the user model.
467458 user .update (** post_data )
459+ except ValueError :
460+ self .logger .warning (
461+ "Either provided values to update the user does not adhere to the user model "
462+ "datatype or user attempted to update the protected properties."
463+ )
464+ abort (400 , message = "Invalid fields in update request payload" )
468465 except Exception :
469466 self .logger .exception ("Exception occurred during updating user object" )
470467 abort (500 , message = "INTERNAL ERROR" )
@@ -477,15 +474,10 @@ def put(self, username):
477474 403 , message = "Authentication token does not belong to the current user"
478475 )
479476
477+ return_data = user .get_json ()
480478 response_object = {
481479 "status" : "success" ,
482- "data" : {
483- "username" : user .username ,
484- "email" : user .email ,
485- "first_name" : user .first_name ,
486- "last_name" : user .last_name ,
487- "registered_on" : user .registered_on ,
488- },
480+ "data" : return_data ,
489481 }
490482 return make_response (jsonify (response_object ), 200 )
491483
0 commit comments