How to get Jira user information with roles and groups in an automated way ?

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

  • Need to have a list of all Jira active user and their respective groups and roles in an automated way. 

Environment

8.x and later

Solution

Solution1

    • Jira Admin users can use below mentioned python script to fetch users, roles and group details. 

      import getpass
      from jira import JIRA
      class UserMembership:
      def __init__(self, serverUrl, username, password):
      self.serverUrl = serverUrl
      self.username = username
      self.password = password
      
      def getUserGroups(self):
      jira = JIRA(server=self.serverUrl,basic_auth=(self.username, self.password))
      users = jira.search_users(".")
      for user in users:
      jirauser = jira.user(user.name,expand="groups,applicationRoles")
      # print(jirauser.name,jirauser.groups,jirauser.applicationRoles)
      groups = jirauser.groups
      groupSize = jirauser.groups.size
      group_list = list()
      for x in range(groupSize):
      group_list.append(groups.items[x].name)
      user_all_group = ",".join(group_list)
      
      appRole = jirauser.applicationRoles
      appRoleSize = jirauser.applicationRoles.size
      appRole_list = list()
      for y in range(appRoleSize):
      appRole_list.append(appRole.items[y].name)
      user_all_appRole = ",".join(appRole_list)
      
      print("user: "+jirauser.name +"; groups:" + user_all_group +"; applicationRoles: " + user_all_appRole)
      
      url = input("url address: ")
      name = input("username: ")
      password = getpass.getpass("password: ")
      
      UserMembership(url, name, password).getUserGroups();
      


      Prerequisite 

      • Make sure to install jira-python module. Please refer API doc from jira-python

Command to install Jira python module: 

pip install jira

     Steps to execute the script

         1. Copy the content of the script and save it in to UserMembership.py file.

         2. Execute below mentioned command:  

               ❯ python3 UserMembership.py
               url address: http://localhost:8080 # Provide Jira URL
               username: user1 # Provide Jira Admin Username
               password: # Provide Jira Admin Password

3. Here is how the output will be displayed:

               user: user1; groups:jira-administrators,jira-software-users; applicationRoles: jira-administrators,jira-software-users
               user: csp; groups:jira-software-users; applicationRoles: jira-software-users
               user: psc; groups:jira-software-users,jira-test-users; applicationRoles: jira-software-users,jira-test-users

                        Note: This is just a sample script which just demonstrate how we can get users and groups. You can modify as per your requirement.

Solution2 

  • Jira Admin can execute below mentioned sql query to get information about Users and associated groups.
#In PostgreSQL db
select cwd_user.user_name, cwd_user.display_name, cwd_user.email_address,array_to_string(array_agg(cwd_membership.parent_name), ',') as "MemberOf" from cwd_user inner join cwd_membership on cwd_membership.child_id = cwd_user.ID where cwd_membership.membership_type='GROUP_USER' group by cwd_user.user_name, cwd_user.display_name, cwd_user.email_address; 

#In hsqldb
select cwd_user.user_name, cwd_user.display_name, cwd_user.email_address, GROUP_CONCAT(cwd_membership.parent_name SEPARATOR ', ') as "MemberOf" from cwd_user inner join cwd_membership on cwd_membership.child_id = cwd_user.ID where cwd_membership.membership_type='GROUP_USER' group by cwd_user.user_name, cwd_user.display_name, cwd_user.email_address;




Last modified on Feb 8, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.