From 7b716df9faa2fbe223b7525118842988d97fbd5e Mon Sep 17 00:00:00 2001 From: tierno Date: Thu, 13 Jun 2019 09:06:00 +0000 Subject: [PATCH] fix bugs 747 757: fix user-update interface Change-Id: Ic526071cd16675a5a89d7fb6285a1654e88daae8 Signed-off-by: tierno --- osmclient/scripts/osm.py | 8 +++++-- osmclient/sol005/user.py | 47 ++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 97c1942..b11ced4 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -2391,6 +2391,9 @@ def user_create(ctx, username, password, projects, project_role_mappings): # hide_input=True, # confirmation_prompt=True, help='user password') +@click.option('--set-username', 'set_username', + default=None, + help='change username') @click.option('--set-project', 'set_project', default=None, multiple=True, help='create/replace the project,role(s) mapping for this project: \'project,role1,role2,...\'') @@ -2404,21 +2407,22 @@ def user_create(ctx, username, password, projects, project_role_mappings): default=None, multiple=True, help='removes project,role(s) mapping: \'project,role1,role2,...\'') @click.pass_context -def user_update(ctx, username, password, set_project, remove_project, +def user_update(ctx, username, password, set_username, set_project, remove_project, add_project_role, remove_project_role): """Update a user information \b USERNAME: name of the user PASSWORD: new password + SET_USERNAME: new username SET_PROJECT: creating mappings for project/role(s) REMOVE_PROJECT: deleting mappings for project/role(s) ADD_PROJECT_ROLE: adding mappings for project/role(s) REMOVE_PROJECT_ROLE: removing mappings for project/role(s) """ user = {} - user['username'] = username user['password'] = password + user['username'] = set_username user['set-project'] = set_project user['remove-project'] = remove_project user['add-project-role'] = add_project_role diff --git a/osmclient/sol005/user.py b/osmclient/sol005/user.py index 6604c93..2f635f0 100644 --- a/osmclient/sol005/user.py +++ b/osmclient/sol005/user.py @@ -83,35 +83,36 @@ class User(object): def update(self, name, user): """Updates an existing OSM user identified by name """ - myuser = self.get(name) + # print(user) + myuser = self.get(name) update_user = { - "username": myuser["username"], - "project_role_mappings": myuser["project_role_mappings"] + "add_project_role_mappings": [], + "remove_project_role_mappings": [], } # if password is defined, update the password if user["password"]: update_user["password"] = user["password"] - + if user["username"]: + update_user["username"] = user["username"] + if user["set-project"]: + # Remove project and insert project role mapping for set_project in user["set-project"]: + set_project_clean = [m.strip() for m in set_project.split(",")] project, roles = set_project_clean[0], set_project_clean[1:] - update_user["project_role_mappings"] = [mapping for mapping - in update_user["project_role_mappings"] - if mapping["project"] != project] + update_user["remove_project_role_mappings"].append({"project": project}) for role in roles: mapping = {"project": project, "role": role} - update_user["project_role_mappings"].append(mapping) + update_user["add_project_role_mappings"].append(mapping) if user["remove-project"]: for remove_project in user["remove-project"]: - update_user["project_role_mappings"] = [mapping for mapping - in update_user["project_role_mappings"] - if mapping["project"] != remove_project] - + update_user["remove_project_role_mappings"].append({"project": remove_project}) + if user["add-project-role"]: for add_project_role in user["add-project-role"]: add_project_role_clean = [m.strip() for m in add_project_role.split(",")] @@ -119,23 +120,23 @@ class User(object): for role in roles: mapping = {"project": project, "role": role} - if mapping not in update_user["project_role_mappings"]: - update_user["project_role_mappings"].append(mapping) - + update_user["add_project_role_mappings"].append(mapping) + if user["remove-project-role"]: for remove_project_role in user["remove-project-role"]: remove_project_role_clean = [m.strip() for m in remove_project_role.split(",")] project, roles = remove_project_role_clean[0], remove_project_role_clean[1:] for role in roles: - mapping_to_remove = {"project": project, "role": role} - update_user["project_role_mappings"] = [mapping for mapping - in update_user["project_role_mappings"] - if mapping != mapping_to_remove] - - if not user["password"] and not user["set-project"] and not user["remove-project"] \ - and not user["add-project-role"] and not user["remove-project-role"]: - raise ClientException("At least one parameter should be defined.") + mapping = {"project": project, "role": role} + update_user["remove_project_role_mappings"].append(mapping) + + if not update_user["remove_project_role_mappings"]: + del update_user["remove_project_role_mappings"] + if not update_user["add_project_role_mappings"]: + del update_user["add_project_role_mappings"] + if not update_user: + raise ClientException("At least something should be changed.") http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase, myuser['_id']), postfields_dict=update_user) -- 2.25.1