Skip to content

Commit 845898f

Browse files
authored
Merge pull request #527 from CodeForPhilly/525-text-roles
Support role names in /api/admin/user/update
2 parents 8cdb867 + 0ab366c commit 845898f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ start_env.sh
2323
.mypy_cache/
2424
*secrets*
2525
*kustomization*
26+
src/.venv/

src/server/api/user_api.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def user_update():
396396

397397

398398
if not update_dict:
399+
logger.debug("Update called with nothing to update")
399400
return jsonify("No changed items specified") # If nothing to do, declare victory
400401

401402
if "password" in update_dict.keys():
@@ -406,7 +407,6 @@ def user_update():
406407
return jsonify("Password too weak")
407408

408409

409-
410410
# We have a variable number of columns to update.
411411
# We could generate a text query on the fly, but this seems the perfect place to use the ORM
412412
# and let it handle the update for us.
@@ -419,10 +419,29 @@ def user_update():
419419
session = Session()
420420
# #TODO: Figure out why context manager doesn't work or do try/finally
421421

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]
424433

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)
426445

427446
stmt = update(PU).where(PU.columns.username == username).values(update_dict).\
428447
execution_options(synchronize_session="fetch")

0 commit comments

Comments
 (0)