From e25fc060399dbf6855491f91424850c1c4e1c771 Mon Sep 17 00:00:00 2001 From: ChowRex Date: Fri, 24 May 2024 16:40:51 +0800 Subject: [PATCH 1/3] Add `Crowd` a new attr Add a new property of class `Crowd`, can retrieves full details of all group memberships, see: https://docs.atlassian.com/atlassian-crowd/5.3.1/REST/#usermanagement/1/group-getAllMemberships --- atlassian/crowd.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/atlassian/crowd.py b/atlassian/crowd.py index f9548c841..ebea6b7a0 100644 --- a/atlassian/crowd.py +++ b/atlassian/crowd.py @@ -264,3 +264,21 @@ def update_plugin_license(self, plugin_key, raw_license): url = "/plugins/1.0/{plugin_key}/license".format(plugin_key=plugin_key) data = {"rawLicense": raw_license} return self.put(url, data=data, headers=app_headers) + + @property + def memberships(self): + """ + Retrieves full details of all group memberships, with users and nested groups. + See: https://docs.atlassian.com/atlassian-crowd/5.3.1/REST/#usermanagement/1/group-getAllMemberships + :return: All membership mapping dict + """ + path = self._crowd_api_url("usermanagement", "group/membership") + headers = {'Accept': 'application/xml'} + response = self.get(path, headers=headers, advanced_mode=True) + root = etree.fromstring(response.content) + memberships = {} + for member in root.xpath('//membership'): + group = member.get('group') + users = [user.get('name') for user in member.xpath('./users/user')] + memberships[group] = users + return memberships From dbb4debdc1c618ece11985032b31334c8372a8f0 Mon Sep 17 00:00:00 2001 From: ChowRex Date: Fri, 24 May 2024 16:48:26 +0800 Subject: [PATCH 2/3] Fix import error Lost `lxml` package --- atlassian/crowd.py | 1 + 1 file changed, 1 insertion(+) diff --git a/atlassian/crowd.py b/atlassian/crowd.py index ebea6b7a0..34238f7a0 100644 --- a/atlassian/crowd.py +++ b/atlassian/crowd.py @@ -2,6 +2,7 @@ import logging from jmespath import search +from lxml import etree from .rest_client import AtlassianRestAPI From ba684390277e6a22074650e7e654e6517da98aaf Mon Sep 17 00:00:00 2001 From: ChowRex Date: Fri, 24 May 2024 16:52:47 +0800 Subject: [PATCH 3/3] Update doc Update new feature doc --- docs/crowd.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/crowd.rst b/docs/crowd.rst index 563d0e1d5..30bc12ddc 100644 --- a/docs/crowd.rst +++ b/docs/crowd.rst @@ -38,3 +38,16 @@ Manage groups # Get group's members crowd.group_members(group, kind='direct', max_results=99999) +Get memberships +---------------- + +.. code-block:: python + + # Retrieves full details of all group memberships. + # Return data structure: + # { + # GroupName1: [ Member1, Member2, ... ], + # GroupName2: [ MemberA, MemberB, ... ], + # ... + # } + crowd.memberships