Skip to content

Commit 1ccaa1b

Browse files
ChowRexzhouruixi
andauthored
Add more Crowd functions (#1277)
* Update crowd.py - Add a new function `user_groups` used to get specify user's all group info - Introduce new dependencies `jmespath` which is used for search string in json, for more info, read: [JMESPath Tutorial — JMESPath](https://jmespath.org/tutorial.html) * Add more Crowd functions - Using universal request to get user's group, controled by `kind` param - Using universal request to get group's member, controled by `kind` param - Add a new function to determine whether the user is a member of a group * Update crowd.py - Add a parameter `max_results` has prevented insufficient results from being returned, the server default is `1000` * Update requirements.txt - Sync latest requirements * Update .readthedocs.yml - Add a config parameter: `build.os` try to fix RTD docs build error * Update .readthedocs.yml - Add build config to fix RTD doc build error * Update requirements-dev.txt - Add `jmespath` into it. * Update crowd.rst - Add new functions' doc * Update setup.py - Introduce a new dependency package: `jmespath` * Update crowd.py - Using black to format code, replace pylint. --------- Co-authored-by: zhouruixi <zhouruixi@noreply.com>
1 parent 2a8a77f commit 1ccaa1b

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

.readthedocs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ version: 2
88
# Build documentation in the docs/ directory with Sphinx
99
sphinx:
1010
configuration: docs/conf.py
11+
12+
# Add this to solve RTD build error.
13+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html#build-os
14+
build:
15+
os: ubuntu-22.04
16+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html#build-tools
17+
tools:
18+
python: "3"

atlassian/crowd.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# coding=utf-8
22
import logging
33

4+
from jmespath import search
5+
46
from .rest_client import AtlassianRestAPI
57

68
log = logging.getLogger(__name__)
@@ -129,6 +131,43 @@ def user_delete(self, username):
129131

130132
return self.delete(self._crowd_api_url("usermanagement", "user"), params=params)
131133

134+
def user_groups(self, username, kind="direct"):
135+
"""
136+
Get user's all group info
137+
:param username: str - username
138+
:param kind: str - group type
139+
:return: The specify user's group info
140+
"""
141+
path = self._crowd_api_url("usermanagement", "user/group/{kind}".format(kind=kind))
142+
response = self.get(path, params={"username": username})
143+
return search("groups[*].name", response)
144+
145+
def group_members(self, group, kind="direct", max_results=99999):
146+
"""
147+
Get group's all direct members
148+
:param group: str - group name
149+
:param kind: str - group type
150+
:param max_results: int - maximum number of results
151+
:return: The specify group's direct members info
152+
"""
153+
path = self._crowd_api_url("usermanagement", "group/user/{kind}".format(kind=kind))
154+
params = {"groupname": group, "max-results": max_results}
155+
response = self.get(path, params=params)
156+
return search("users[*].name", response)
157+
158+
def is_user_in_group(self, username, group, kind="direct"):
159+
"""
160+
Check if the user is a member of the group
161+
:param username: str - username
162+
:param group: str - group name
163+
:param kind: str - group type
164+
:return: bool - Return `True` or `False`
165+
"""
166+
path = self._crowd_api_url("usermanagement", "group/user/{kind}".format(kind=kind))
167+
params = {"username": username, "groupname": group}
168+
response = self.get(path, params=params, advanced_mode=True)
169+
return response.status_code == 200
170+
132171
def group_add_user(self, username, groupname):
133172
"""
134173
Add user to group
@@ -145,15 +184,6 @@ def group_add_user(self, username, groupname):
145184
json=data,
146185
)
147186

148-
def group_nested_members(self, group):
149-
"""
150-
Get nested members of group
151-
:param group:
152-
:return:
153-
"""
154-
params = {"groupname": group}
155-
return self.get(self._crowd_api_url("group", "nested"), params=params)
156-
157187
def health_check(self):
158188
"""
159189
Get health status

docs/crowd.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Manage users
2121
# Get user
2222
crowd.user(username)
2323
24+
# Get user's all group info
25+
crowd.user_groups(username, kind='direct')
26+
27+
# Check whether the user is a member of the group
28+
crowd.is_user_in_group(username, group, kind='direct')
29+
2430
Manage groups
2531
-------------
2632

@@ -29,3 +35,6 @@ Manage groups
2935
# Add user to group
3036
crowd.group_add_user(username, groupname)
3137
38+
# Get group's members
39+
crowd.group_members(group, kind='direct', max_results=99999)
40+

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ python-magic
1414
# On October 4, 2022 importlib-metadata released importlib-metadata 5.0.0 and in version 5.0.0
1515
# They have Deprecated EntryPoints and that's why you are facing this error.
1616
importlib-metadata<=4.13.0
17-
17+
# Add this package to search string in json
18+
jmespath

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ six
44
oauthlib
55
requests_oauthlib
66
requests-kerberos==0.14.0
7+
jmespath
78
bs4
8-
lxml
9+
lxml

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package_dir={"atlassian": "atlassian"},
2626
include_package_data=True,
2727
zip_safe=False,
28-
install_requires=["deprecated", "requests", "six", "oauthlib", "requests_oauthlib"],
28+
install_requires=["deprecated", "requests", "six", "oauthlib", "requests_oauthlib", "jmespath"],
2929
extras_require={"kerberos": ["requests-kerberos"]},
3030
platforms="Platform Independent",
3131
classifiers=[

0 commit comments

Comments
 (0)