madhushreeag commented on code in PR #40178:
URL: https://github.com/apache/superset/pull/40178#discussion_r3252008258


##########
superset/security/manager.py:
##########
@@ -174,6 +178,38 @@ def post_update(self, item: Model) -> None:
     def post_delete(self, item: Model) -> None:
         _log_audit_event("RoleDeleted", {"role_name": item.name, "role_id": 
item.id})
 
+    @expose("/<int:role_id>/users", methods=["PUT"])
+    @protect()
+    @safe
+    def update_role_users(self, role_id: int) -> Any:
+        """Override to deduplicate user IDs and handle missing users 
gracefully."""
+        try:
+            item = self.update_role_user_schema.load(request.json)
+            role = self.datamodel.get(role_id)
+            if not role:
+                return self.response_404()
+
+            user_ids = list(set(item["user_ids"]))
+            users = (
+                current_app.appbuilder.session.query(User)
+                .filter(User.id.in_(user_ids))
+                .all()
+            )
+            role.user = users
+            self.datamodel.edit(role)
+            return self.response(
+                200,
+                **{
+                    API_RESULT_RES_KEY: self.update_role_user_schema.dump(
+                        item, many=False
+                    )
+                },
+            )

Review Comment:
   The response now serializes {"user_ids": [u.id for u in users]} — the IDs 
that were actually queried from the DB and persisted — so the response is 
always an accurate reflection of the roles user assignments after the save.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to