- self.check_conflict_on_edit(session, content, indata, _id=_id)
- self.format_on_edit(content, indata)
-
- if "password" in content:
- self.auth.change_password(content["name"], content["password"])
- else:
- users = self.auth.get_user_list()
- user = [user for user in users if user["_id"] == content["_id"]][0]
- original_mapping = []
- edit_mapping = content["project_role_mappings"]
-
- for project in user["projects"]:
- for role in project["roles"]:
- original_mapping += {
- "project": project["name"],
- "role": role["name"]
- }
-
- mappings_to_remove = [mapping for mapping in original_mapping
- if mapping not in edit_mapping]
-
- mappings_to_add = [mapping for mapping in edit_mapping
- if mapping not in original_mapping]
-
- for mapping in mappings_to_remove:
- self.auth.remove_role_from_user(
- user["name"],
- mapping["project"],
- mapping["role"]
- )
-
- for mapping in mappings_to_add:
- self.auth.assign_role_to_user(
- user["name"],
- mapping["project"],
- mapping["role"]
- )
-
- return content["_id"]
+ indata = self._validate_input_edit(indata, content, force=session["force"])
+ content = self.check_conflict_on_edit(session, content, indata, _id=_id)
+ # self.format_on_edit(content, indata)
+
+ if not ("password" in indata or "username" in indata or indata.get("remove_project_role_mappings") or
+ indata.get("add_project_role_mappings") or indata.get("project_role_mappings") or
+ indata.get("projects") or indata.get("add_projects")):
+ return _id
+ if indata.get("project_role_mappings") \
+ and (indata.get("remove_project_role_mappings") or indata.get("add_project_role_mappings")):
+ raise EngineException("Option 'project_role_mappings' is incompatible with 'add_project_role_mappings"
+ "' or 'remove_project_role_mappings'", http_code=HTTPStatus.BAD_REQUEST)
+
+ if indata.get("projects") or indata.get("add_projects"):
+ role = self.auth.get_role_list({"name": "project_admin"})
+ if not role:
+ role = self.auth.get_role_list()
+ if not role:
+ raise AuthconnNotFoundException("Can't find a default role for user '{}'"
+ .format(content["username"]))
+ rid = role[0]["_id"]
+ if "add_project_role_mappings" not in indata:
+ indata["add_project_role_mappings"] = []
+ if "remove_project_role_mappings" not in indata:
+ indata["remove_project_role_mappings"] = []
+ if isinstance(indata.get("projects"), dict):
+ # backward compatible
+ for k, v in indata["projects"].items():
+ if k.startswith("$") and v is None:
+ indata["remove_project_role_mappings"].append({"project": k[1:]})
+ elif k.startswith("$+"):
+ indata["add_project_role_mappings"].append({"project": v, "role": rid})
+ del indata["projects"]
+ for proj in indata.get("projects", []) + indata.get("add_projects", []):
+ indata["add_project_role_mappings"].append({"project": proj, "role": rid})
+
+ # user = self.show(session, _id) # Already in 'content'
+ original_mapping = content["project_role_mappings"]
+
+ mappings_to_add = []
+ mappings_to_remove = []
+
+ # remove
+ for to_remove in indata.get("remove_project_role_mappings", ()):
+ for mapping in original_mapping:
+ if to_remove["project"] in (mapping["project"], mapping["project_name"]):
+ if not to_remove.get("role") or to_remove["role"] in (mapping["role"], mapping["role_name"]):
+ mappings_to_remove.append(mapping)
+
+ # add
+ for to_add in indata.get("add_project_role_mappings", ()):
+ for mapping in original_mapping:
+ if to_add["project"] in (mapping["project"], mapping["project_name"]) and \
+ to_add["role"] in (mapping["role"], mapping["role_name"]):
+
+ if mapping in mappings_to_remove: # do not remove
+ mappings_to_remove.remove(mapping)
+ break # do not add, it is already at user
+ else:
+ pid = self.auth.get_project(to_add["project"])["_id"]
+ rid = self.auth.get_role(to_add["role"])["_id"]
+ mappings_to_add.append({"project": pid, "role": rid})
+
+ # set
+ if indata.get("project_role_mappings"):
+ for to_set in indata["project_role_mappings"]:
+ for mapping in original_mapping:
+ if to_set["project"] in (mapping["project"], mapping["project_name"]) and \
+ to_set["role"] in (mapping["role"], mapping["role_name"]):
+ if mapping in mappings_to_remove: # do not remove
+ mappings_to_remove.remove(mapping)
+ break # do not add, it is already at user
+ else:
+ pid = self.auth.get_project(to_set["project"])["_id"]
+ rid = self.auth.get_role(to_set["role"])["_id"]
+ mappings_to_add.append({"project": pid, "role": rid})
+ for mapping in original_mapping:
+ for to_set in indata["project_role_mappings"]:
+ if to_set["project"] in (mapping["project"], mapping["project_name"]) and \
+ to_set["role"] in (mapping["role"], mapping["role_name"]):
+ break
+ else:
+ # delete
+ if mapping not in mappings_to_remove: # do not remove
+ mappings_to_remove.append(mapping)
+
+ self.auth.update_user({"_id": _id, "username": indata.get("username"), "password": indata.get("password"),
+ "add_project_role_mappings": mappings_to_add,
+ "remove_project_role_mappings": mappings_to_remove
+ })
+ data_to_send = {'_id': _id, "changes": indata}
+ self._send_msg("edited", data_to_send, not_send_msg=None)
+
+ # return _id