Skip to content

crowd

Crowd

Bases: AtlassianRestAPI

Crowd API wrapper. Important to note that you will have to use an application credentials, not user credentials, in order to access Crowd APIs

Source code in server/vendor/atlassian/crowd.py
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
class Crowd(AtlassianRestAPI):
    """Crowd API wrapper.
    Important to note that you will have to use an application credentials,
    not user credentials, in order to access Crowd APIs"""

    def __init__(
        self,
        url,
        username,
        password,
        timeout=60,
        api_root="rest",
        api_version="latest",
    ):
        super(Crowd, self).__init__(url, username, password, timeout, api_root, api_version)

    def _crowd_api_url(self, api, resource):
        return "/{api_root}/{api}/{version}/{resource}".format(
            api_root=self.api_root,
            api=api,
            version=self.api_version,
            resource=resource,
        )

    def _user_change_status(self, username, active):
        """
        Change user status.
        :param username: str - username
        :param active: bool - True/False
        :return:
        """

        user = self.user(username)

        user_object = {
            "name": username,
            "active": active,
            "display-name": user.get("display-name"),
            "first-name": user.get("first-name"),
            "last-name": user.get("last-name"),
            "email": user.get("email"),
        }

        params = {"username": username}

        return self.put(
            self._crowd_api_url("usermanagement", "user"),
            params=params,
            data=user_object,
        )

    def user(self, username):
        """
        Get user information
        :param username:
        :return:
        """
        params = {"username": username}
        return self.get(self._crowd_api_url("usermanagement", "user"), params=params)

    def user_activate(self, username):
        """
        Activate user
        :param username: str - username
        """

        return self._user_change_status(username, True)

    def user_create(
        self,
        username,
        active,
        first_name,
        last_name,
        display_name,
        email,
        password,
    ):
        """
        Create new user method
        :param  active: bool:
        :param  username: string: username
        :param  active: bool:
        :param  first_name: string:
        :param  last_name: string:
        :param  display_name:  string:
        :param  email: string:
        :param  password: string:
        :return:
        """

        user_object = {
            "name": username,
            "password": {"value": password},
            "active": active,
            "first-name": first_name,
            "last-name": last_name,
            "display-name": display_name,
            "email": email,
        }

        return self.post(self._crowd_api_url("usermanagement", "user"), data=user_object)

    def user_deactivate(self, username):
        """
        Deactivate user

        :return:
        """

        return self._user_change_status(username, False)

    def user_delete(self, username):
        """
        Delete user
        :param username: str - username
        :return:
        """

        params = {"username": 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
        :return:
        """

        data = {"name": groupname}

        params = {"username": username}

        return self.post(
            self._crowd_api_url("usermanagement", "user/group/direct"),
            params=params,
            json=data,
        )

    def health_check(self):
        """
        Get health status
        https://confluence.atlassian.com/jirakb/how-to-retrieve-health-check-results-using-rest-api-867195158.html
        :return:
        """
        # check as Troubleshooting & Support Tools Plugin
        response = self.get("rest/troubleshooting/1.0/check/")
        if not response:
            # check as support tools
            response = self.get("rest/supportHealthCheck/1.0/check/")
        return response

    def get_plugins_info(self):
        """
        Provide plugins info
        :return a json of installed plugins
        """
        url = "rest/plugins/1.0/"
        return self.get(url, headers=self.no_check_headers, trailing=True)

    def get_plugin_info(self, plugin_key):
        """
        Provide plugin info
        :return a json of installed plugins
        """
        url = "rest/plugins/1.0/{plugin_key}-key".format(plugin_key=plugin_key)
        return self.get(url, headers=self.no_check_headers, trailing=True)

    def get_plugin_license_info(self, plugin_key):
        """
        Provide plugin license info
        :return a json specific License query
        """
        url = "rest/plugins/1.0/{plugin_key}-key/license".format(plugin_key=plugin_key)
        return self.get(url, headers=self.no_check_headers, trailing=True)

    def upload_plugin(self, plugin_path):
        """
        Provide plugin path for upload into Jira e.g. useful for auto deploy
        :param plugin_path:
        :return:
        """
        files = {"plugin": open(plugin_path, "rb")}
        upm_token = self.request(
            method="GET",
            path="rest/plugins/1.0/",
            headers=self.no_check_headers,
            trailing=True,
        ).headers["upm-token"]
        url = "rest/plugins/1.0/?token={upm_token}".format(upm_token=upm_token)
        return self.post(url, files=files, headers=self.no_check_headers)

    def delete_plugin(self, plugin_key):
        """
        Delete plugin
        :param plugin_key:
        :return:
        """
        url = "rest/plugins/1.0/{}-key".format(plugin_key)
        return self.delete(url)

    def check_plugin_manager_status(self):
        url = "rest/plugins/latest/safe-mode"
        return self.request(method="GET", path=url, headers=self.safe_mode_headers)

    def update_plugin_license(self, plugin_key, raw_license):
        """
        Update license for plugin
        :param plugin_key:
        :param raw_license:
        :return:
        """
        app_headers = {
            "X-Atlassian-Token": "nocheck",
            "Content-Type": "application/vnd.atl.plugins+json",
        }
        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)
        soup = BeautifulSoup(response, "xml")
        memberships = {}
        for membership in soup.find_all("membership"):
            group = membership["group"]
            users = [user["name"] for user in membership.find_all("user")]
            memberships[group] = users
        return memberships

    def group_create(self, groupname, description=None, active=True):
        """
        Create new group method
        :param groupname: string: The name of new group
        :param description: string: The description of new group, default is None
        :param active: bool: Weather the group is active, default is True
        :return: Create result
        """
        group = {
            "name": groupname,
            "active": active,
            "description": description,
            "type": "GROUP",
        }

        return self.post(self._crowd_api_url("usermanagement", "group"), data=group)

memberships property

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

delete_plugin(plugin_key)

Delete plugin :param plugin_key: :return:

Source code in server/vendor/atlassian/crowd.py
241
242
243
244
245
246
247
248
def delete_plugin(self, plugin_key):
    """
    Delete plugin
    :param plugin_key:
    :return:
    """
    url = "rest/plugins/1.0/{}-key".format(plugin_key)
    return self.delete(url)

get_plugin_info(plugin_key)

Provide plugin info :return a json of installed plugins

Source code in server/vendor/atlassian/crowd.py
209
210
211
212
213
214
215
def get_plugin_info(self, plugin_key):
    """
    Provide plugin info
    :return a json of installed plugins
    """
    url = "rest/plugins/1.0/{plugin_key}-key".format(plugin_key=plugin_key)
    return self.get(url, headers=self.no_check_headers, trailing=True)

get_plugin_license_info(plugin_key)

Provide plugin license info :return a json specific License query

Source code in server/vendor/atlassian/crowd.py
217
218
219
220
221
222
223
def get_plugin_license_info(self, plugin_key):
    """
    Provide plugin license info
    :return a json specific License query
    """
    url = "rest/plugins/1.0/{plugin_key}-key/license".format(plugin_key=plugin_key)
    return self.get(url, headers=self.no_check_headers, trailing=True)

get_plugins_info()

Provide plugins info :return a json of installed plugins

Source code in server/vendor/atlassian/crowd.py
201
202
203
204
205
206
207
def get_plugins_info(self):
    """
    Provide plugins info
    :return a json of installed plugins
    """
    url = "rest/plugins/1.0/"
    return self.get(url, headers=self.no_check_headers, trailing=True)

group_add_user(username, groupname)

Add user to group :return:

Source code in server/vendor/atlassian/crowd.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def group_add_user(self, username, groupname):
    """
    Add user to group
    :return:
    """

    data = {"name": groupname}

    params = {"username": username}

    return self.post(
        self._crowd_api_url("usermanagement", "user/group/direct"),
        params=params,
        json=data,
    )

group_create(groupname, description=None, active=True)

Create new group method :param groupname: string: The name of new group :param description: string: The description of new group, default is None :param active: bool: Weather the group is active, default is True :return: Create result

Source code in server/vendor/atlassian/crowd.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def group_create(self, groupname, description=None, active=True):
    """
    Create new group method
    :param groupname: string: The name of new group
    :param description: string: The description of new group, default is None
    :param active: bool: Weather the group is active, default is True
    :return: Create result
    """
    group = {
        "name": groupname,
        "active": active,
        "description": description,
        "type": "GROUP",
    }

    return self.post(self._crowd_api_url("usermanagement", "group"), data=group)

group_members(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

Source code in server/vendor/atlassian/crowd.py
146
147
148
149
150
151
152
153
154
155
156
157
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)

health_check()

Get health status https://confluence.atlassian.com/jirakb/how-to-retrieve-health-check-results-using-rest-api-867195158.html :return:

Source code in server/vendor/atlassian/crowd.py
188
189
190
191
192
193
194
195
196
197
198
199
def health_check(self):
    """
    Get health status
    https://confluence.atlassian.com/jirakb/how-to-retrieve-health-check-results-using-rest-api-867195158.html
    :return:
    """
    # check as Troubleshooting & Support Tools Plugin
    response = self.get("rest/troubleshooting/1.0/check/")
    if not response:
        # check as support tools
        response = self.get("rest/supportHealthCheck/1.0/check/")
    return response

is_user_in_group(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

Source code in server/vendor/atlassian/crowd.py
159
160
161
162
163
164
165
166
167
168
169
170
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

update_plugin_license(plugin_key, raw_license)

Update license for plugin :param plugin_key: :param raw_license: :return:

Source code in server/vendor/atlassian/crowd.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
def update_plugin_license(self, plugin_key, raw_license):
    """
    Update license for plugin
    :param plugin_key:
    :param raw_license:
    :return:
    """
    app_headers = {
        "X-Atlassian-Token": "nocheck",
        "Content-Type": "application/vnd.atl.plugins+json",
    }
    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)

upload_plugin(plugin_path)

Provide plugin path for upload into Jira e.g. useful for auto deploy :param plugin_path: :return:

Source code in server/vendor/atlassian/crowd.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def upload_plugin(self, plugin_path):
    """
    Provide plugin path for upload into Jira e.g. useful for auto deploy
    :param plugin_path:
    :return:
    """
    files = {"plugin": open(plugin_path, "rb")}
    upm_token = self.request(
        method="GET",
        path="rest/plugins/1.0/",
        headers=self.no_check_headers,
        trailing=True,
    ).headers["upm-token"]
    url = "rest/plugins/1.0/?token={upm_token}".format(upm_token=upm_token)
    return self.post(url, files=files, headers=self.no_check_headers)

user(username)

Get user information :param username: :return:

Source code in server/vendor/atlassian/crowd.py
63
64
65
66
67
68
69
70
def user(self, username):
    """
    Get user information
    :param username:
    :return:
    """
    params = {"username": username}
    return self.get(self._crowd_api_url("usermanagement", "user"), params=params)

user_activate(username)

Activate user :param username: str - username

Source code in server/vendor/atlassian/crowd.py
72
73
74
75
76
77
78
def user_activate(self, username):
    """
    Activate user
    :param username: str - username
    """

    return self._user_change_status(username, True)

user_create(username, active, first_name, last_name, display_name, email, password)

Create new user method :param active: bool: :param username: string: username :param active: bool: :param first_name: string: :param last_name: string: :param display_name: string: :param email: string: :param password: string: :return:

Source code in server/vendor/atlassian/crowd.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def user_create(
    self,
    username,
    active,
    first_name,
    last_name,
    display_name,
    email,
    password,
):
    """
    Create new user method
    :param  active: bool:
    :param  username: string: username
    :param  active: bool:
    :param  first_name: string:
    :param  last_name: string:
    :param  display_name:  string:
    :param  email: string:
    :param  password: string:
    :return:
    """

    user_object = {
        "name": username,
        "password": {"value": password},
        "active": active,
        "first-name": first_name,
        "last-name": last_name,
        "display-name": display_name,
        "email": email,
    }

    return self.post(self._crowd_api_url("usermanagement", "user"), data=user_object)

user_deactivate(username)

Deactivate user

:return:

Source code in server/vendor/atlassian/crowd.py
115
116
117
118
119
120
121
122
def user_deactivate(self, username):
    """
    Deactivate user

    :return:
    """

    return self._user_change_status(username, False)

user_delete(username)

Delete user :param username: str - username :return:

Source code in server/vendor/atlassian/crowd.py
124
125
126
127
128
129
130
131
132
133
def user_delete(self, username):
    """
    Delete user
    :param username: str - username
    :return:
    """

    params = {"username": username}

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

user_groups(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

Source code in server/vendor/atlassian/crowd.py
135
136
137
138
139
140
141
142
143
144
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)