Skip to content

Add more Crowd functions #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ version: 2
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py

# Add this to solve RTD build error.
# See https://docs.readthedocs.io/en/stable/config-file/v2.html#build-os
build:
os: ubuntu-22.04
# See https://docs.readthedocs.io/en/stable/config-file/v2.html#build-tools
tools:
python: "3"
48 changes: 39 additions & 9 deletions atlassian/crowd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# coding=utf-8
import logging

from jmespath import search

from .rest_client import AtlassianRestAPI

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

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

def user_groups(self, username, kind="direct"):
"""
Get user's all group info
:param username: str - username
:param kind: str - group type
:return: The specify user's group info
"""
path = self._crowd_api_url("usermanagement", "user/group/{kind}".format(kind=kind))
response = self.get(path, params={"username": username})
return search("groups[*].name", response)

def group_members(self, group, kind="direct", max_results=99999):
"""
Get group's all direct members
:param group: str - group name
:param kind: str - group type
:param max_results: int - maximum number of results
:return: The specify group's direct members info
"""
path = self._crowd_api_url("usermanagement", "group/user/{kind}".format(kind=kind))
params = {"groupname": group, "max-results": max_results}
response = self.get(path, params=params)
return search("users[*].name", response)

def is_user_in_group(self, username, group, kind="direct"):
"""
Check if the user is a member of the group
:param username: str - username
:param group: str - group name
:param kind: str - group type
:return: bool - Return `True` or `False`
"""
path = self._crowd_api_url("usermanagement", "group/user/{kind}".format(kind=kind))
params = {"username": username, "groupname": group}
response = self.get(path, params=params, advanced_mode=True)
return response.status_code == 200

def group_add_user(self, username, groupname):
"""
Add user to group
Expand All @@ -145,15 +184,6 @@ def group_add_user(self, username, groupname):
json=data,
)

def group_nested_members(self, group):
"""
Get nested members of group
:param group:
:return:
"""
params = {"groupname": group}
return self.get(self._crowd_api_url("group", "nested"), params=params)

def health_check(self):
"""
Get health status
Expand Down
9 changes: 9 additions & 0 deletions docs/crowd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Manage users
# Get user
crowd.user(username)

# Get user's all group info
crowd.user_groups(username, kind='direct')

# Check whether the user is a member of the group
crowd.is_user_in_group(username, group, kind='direct')

Manage groups
-------------

Expand All @@ -29,3 +35,6 @@ Manage groups
# Add user to group
crowd.group_add_user(username, groupname)

# Get group's members
crowd.group_members(group, kind='direct', max_results=99999)

3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ python-magic
# On October 4, 2022 importlib-metadata released importlib-metadata 5.0.0 and in version 5.0.0
# They have Deprecated EntryPoints and that's why you are facing this error.
importlib-metadata<=4.13.0

# Add this package to search string in json
jmespath
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ six
oauthlib
requests_oauthlib
requests-kerberos==0.14.0
jmespath
bs4
lxml
lxml
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package_dir={"atlassian": "atlassian"},
include_package_data=True,
zip_safe=False,
install_requires=["deprecated", "requests", "six", "oauthlib", "requests_oauthlib"],
install_requires=["deprecated", "requests", "six", "oauthlib", "requests_oauthlib", "jmespath"],
extras_require={"kerberos": ["requests-kerberos"]},
platforms="Platform Independent",
classifiers=[
Expand Down