Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to check, if user is member of group ? (Jython)

Viesturs Proškins May 17, 2012

I am trying to detect, is user member of group. I have tried already several classes with no success.

Basically,what i need, is to check which group belongs logged time entry: there is my idea

# fragment from code
wManager = ComponentManager.getInstance().getWorklogManager()
worklogList = wManager.getByIssue(issue);
for worklogEntry in worklogList:
	workLogAuthor = worklogEntry.getAuthor()
	workLogTimeEntry = worklogEntry.getTimeSpent()
	groupTest = "Administrators"
		### there I need to check, and do actions with if else, no more ideas how.

Any tips & tracks will help to figure out this.

Running JIRA 4.4.4

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Viesturs Proškins June 3, 2012

Thank you for your time and answer. I have already figured out by browsing some plugins source and migrating logics to Jython:

for worklogEntry in worklogList:
		workLogAuthor = worklogEntry.getAuthor()
		workLogTimeEntry = worklogEntry.getTimeSpent()
		spentSeconds = spentSeconds + workLogTimeEntry
		authorGroups = userManager.getGroupsForUser(workLogAuthor)
		for authorGroup in authorGroups:
			partOfGroup = authorGroup.getName()
			if (partOfGroup == "A"):
				...
			elif (partOfGroup == "B"):
				...
			elif (partOfGroup == "C"):
				...
			elif (partOfGroup == "D"):
				..
			elif (partOfGroup == "E"):
				...
			elif (partOfGroup == "F"):
				...
			elif (partOfGroup == "G"):
				...

1 vote
Maciej Wojszkun August 21, 2012

for example:

import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.security.JiraAuthenticationContext
projectManagerGroup = ComponentManager.getComponentInstanceOfType(com.atlassian.jira.security.groups.GroupManager)

def inGroup(user,group):
    try:
        return projectManagerGroup.isUserInGroup(user,group)
    except:
        return None

currentUser = authenticationContext.getLoggedInUser().getName()
if inGroup(currentUser,'groupName'):
  //do sth

1 vote
Stefan Kohler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 3, 2012

Hi Wieshka,

I know how to do it in JAVA, shouldn't be hard to rewrite it for jython ;-)

private final WorklogManager worklogManager;
private final UserManager userManager;

public void doSometing(Issue issue) {
    List<Worklog> worklogs = worklogManager.getByIssue(issue);
    for (Worklog worklog : worklogs) {
        String author = worklog.getAuthor();

        if (userManager.isUserInGroup(author, "Administrators")) {
            userManager.isAdmin(author); //if your going for admins
            
            // do something
        }
    }
}

if you need the groupnames for a particular user you will need to user the GroupManager

private final GroupManager groupManager;
....

Collection<String> groupNames = groupManager.getGroupNamesForUser(username);

TAGS
AUG Leaders

Atlassian Community Events