@@ -396,6 +396,7 @@ def user_update():
396
396
397
397
398
398
if not update_dict :
399
+ logger .debug ("Update called with nothing to update" )
399
400
return jsonify ("No changed items specified" ) # If nothing to do, declare victory
400
401
401
402
if "password" in update_dict .keys ():
@@ -406,7 +407,6 @@ def user_update():
406
407
return jsonify ("Password too weak" )
407
408
408
409
409
-
410
410
# We have a variable number of columns to update.
411
411
# We could generate a text query on the fly, but this seems the perfect place to use the ORM
412
412
# and let it handle the update for us.
@@ -419,10 +419,29 @@ def user_update():
419
419
session = Session ()
420
420
# #TODO: Figure out why context manager doesn't work or do try/finally
421
421
422
- PU = Table ("pdp_users" , metadata , autoload = True , autoload_with = engine )
423
- # pr = Table("pdp_user_roles", metadata, autoload=True, autoload_with=engine)
422
+ pr = Table ("pdp_user_roles" , metadata , autoload = True , autoload_with = engine )
423
+
424
+ if ("role" in update_dict .keys ()): # We are changing the role
425
+
426
+ # Build dict of roles {name: id}
427
+ role_dict = {}
428
+ r = select ((pr .c .role , pr .c ._id ))
429
+ rr = session .execute (r )
430
+ fa = rr .fetchall ()
431
+ for row in fa :
432
+ role_dict [row [0 ]] = row [1 ]
424
433
425
- #TODO: Check tendered role or join roles table for update
434
+ logger .debug ("Found %d roles" , len (role_dict ))
435
+ # Replace the role name with the corresponding id for update
436
+ try :
437
+ # We could verify that the role is actually different - doesn't seem worth the effort
438
+ update_dict ["role" ] = role_dict [update_dict ["role" ]]
439
+ except KeyError :
440
+ logger .error ("Attempted to change user '%s' to invalid role '%s'" , username , update_dict ["role" ])
441
+ session .close ()
442
+ return jsonify ("Invalid role specified" ), 400
443
+
444
+ PU = Table ("pdp_users" , metadata , autoload = True , autoload_with = engine )
426
445
427
446
stmt = update (PU ).where (PU .columns .username == username ).values (update_dict ).\
428
447
execution_options (synchronize_session = "fetch" )
0 commit comments