Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Use Jira API to get user's direct groups

Steve Kamens August 21, 2018

How can we efficiently get a Jira user's direct groups in a system with nested groups?  

I'm able to get them inefficiently by getting all of the user's groups (getGroupsForUser), then checking the direct members of each group (getDirectUsersInGroup), and looping through to see if the specific user (by matching names) is a direct member.

 

Is there a better way to do this with fewer calls and less looping.

2 answers

0 votes
Steve Kamens August 22, 2018

CrowdService.isUserDirectGroupMember can be used to make the function more efficient

0 votes
J van Leeuwen
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 21, 2018

Hi @Steve Kamens - 

There is no direct way to do this in the Jira's API. If you have ScriptRunner, I'd recommend reviewing this community post that describes how you can do this with the apps functionality:

How to get nested groups in Jira REST API

Hope this helps! 

Jennifer

Rao B August 16, 2021

Hi @J van Leeuwen ,

 

Is there any method to get list of users who have direct access to group, currently I am using the following script, it listing all users including sub-group members also.

def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)
def groupManager = ComponentAccessor.getGroupManager()
def users = groupManager.getUsersInGroup("groupname")

Can you please help , how we can list direct access users from respective group

 

FYI @J van Leeuwen 

Thanks in advance!

Deleted user August 17, 2021

Hello @Rao B @J van Leeuwen @Steve Kamens 

use 

def users = groupManager.getDirectUsersInGroup(groupManager.getGroup("grp-name"))

To get users who have direct access to the specified Group

 

Here you can find the complete code to get users names of users who have directaccess

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import java.util.Date;


def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)

def groupManager = ComponentAccessor.getGroupManager()
def users = groupManager.getDirectUsersInGroup(groupManager.getGroup("group-name"))

StringBuilder builder=new StringBuilder()
builder.append("<table border = 1><tr><td><b>UserName</b></td></tr>")
users.each{
Long lastLoginTime = loginManager.getLoginInfo(it.username).getLastLoginTime()

String activeStatus=it.active
if(activeStatus=="false")
builder.append("")
else if(lastLoginTime==null)
builder.append("<tr><td>"+it.username+"</td></tr>")
else{
Date date=new Date(lastLoginTime);
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
String dateText = df2.format(date);
builder.append("<tr><td>"+it.username+"</td></tr>");
}

}
builder.append("</table>")
return builder

Regards,

Muhammad Atib Junaid

Rao B August 17, 2021

Thanks @[deleted] ,

It works now as expected.

Suggest an answer

Log in or Sign up to answer