Commits

Ken Watford  committed 19de443

Added API call: remove_user_from_users_group

Added corresponding method to the model for user groups.

(Since it's possible to remove users using the web interface,
there must already be a function to do this somewhere.
But I was too lazy to look for it, and it seemed like there should
be a method for it on the model anyway.)

  • Participants
  • Parent commits edd3f20

Comments (0)

Files changed (2)

File rhodecode/controllers/api/api.py

             log.error(traceback.format_exc())
             raise JSONRPCError('failed to create users group member')
 
+    @HasPermissionAllDecorator('hg.admin')
+    def remove_user_from_users_group(self, apiuser, group_name, user_name):
+        """
+        Remove user from a group
+
+        :param apiuser
+        :param group_name
+        :param user_name
+        """
+
+        try:
+            users_group = UsersGroup.get_by_group_name(group_name)
+            if not users_group:
+                raise JSONRPCError('unknown users group %s' % group_name)
+
+            try:
+                user = User.get_by_username(user_name)
+            except NoResultFound:
+                raise JSONRPCError('unknown user %s' % user_name)
+
+            success = UsersGroupModel().remove_user_from_group(users_group, user)
+
+            return dict(success=success,
+                        msg="User removed from group" if success else "User wasn't in group")
+        except Exception:
+            log.error(traceback.format_exc())
+            raise JSONRPCError('failed to remove user from group')
+
     @HasPermissionAnyDecorator('hg.admin')
     def get_repo(self, apiuser, repo_name):
         """"

File rhodecode/model/users_group.py

             .filter(UsersGroupToPerm.permission == perm).scalar()
         if obj:
             self.sa.delete(obj)
+
+    def remove_user_from_group(self, users_group, user):
+        users_group_member = None
+        for m in users_group.members:
+            if m.user.user_id == user.user_id:
+                # Found this user's membership row
+                users_group_member = m
+                break
+
+        if users_group_member:
+            try:
+                self.sa.delete(users_group_member)
+                return True
+            except:
+                log.error(thraceback.format_exc())
+                raise
+        else:
+            # User isn't in that group
+            return False