Skip to content

Support role names in /api/admin/user/update #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ start_env.sh
.mypy_cache/
*secrets*
*kustomization*
src/.venv/
27 changes: 23 additions & 4 deletions src/server/api/user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def user_update():


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

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



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

PU = Table("pdp_users", metadata, autoload=True, autoload_with=engine)
# pr = Table("pdp_user_roles", metadata, autoload=True, autoload_with=engine)
pr = Table("pdp_user_roles", metadata, autoload=True, autoload_with=engine)

if ("role" in update_dict.keys()): # We are changing the role

# Build dict of roles {name: id}
role_dict = {}
r = select((pr.c.role, pr.c._id))
rr = session.execute(r)
fa = rr.fetchall()
for row in fa:
role_dict[row[0]] = row[1]

#TODO: Check tendered role or join roles table for update
logger.debug("Found %d roles", len(role_dict))
# Replace the role name with the corresponding id for update
try:
# We could verify that the role is actually different - doesn't seem worth the effort
update_dict["role"] = role_dict[update_dict["role"]]
except KeyError:
logger.error("Attempted to change user '%s' to invalid role '%s'", username, update_dict["role"])
session.close()
return jsonify("Invalid role specified"), 400

PU = Table("pdp_users", metadata, autoload=True, autoload_with=engine)

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