How do I find Groups

Hi

I am writing a condition where web-item is shown only if the user is administrator or project lead. I have added project lead to project administrator role. I am looking for strings to check for the roles, I wrote the following code so far:

import java.util.SortedSet;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractJiraCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserUtils;
import com.atlassian.jira.user.util.UserUtil;

public class SubComponentAccessCondition extends AbstractJiraCondition {

	UserUtil userUtil;
	@Override
	public boolean shouldDisplay(User user, JiraHelper jiraHelper) {
		userUtil = ComponentAccessor.getUserUtil();
		SortedSet<Group> groups = userUtil.getGroupsForUser(user.getName());
		
		return user != null && groups.contains("jira-developers");
	}

}

Can you please let me how I check if user belongs to jira administrator or project administrator role. Also it shows me AbstractJiraCondition as depricated (I am using JIRA6) is there a better way to write this..

Please let me know.

Thanks

Abe

3 answers

1 accepted

to check user in project role you can try this by doing little bit changes

SimpleErrorCollection errorCollection = new SimpleErrorCollection();
ProjectRole projectLeaders = projectRoleService.getProjectRoleByName(loggedInUser,"project Leaders",errorCollection);
ProjectRoleActors actors = projectRoleService.getProjectRoleActors(	user, projectLeaders,project, errorCollection);
for (Iterator<RoleActor> iterator = actors.getRoleActors().iterator(); iterator.hasNext();) {
			RoleActor roleActor = (RoleActor) iterator.next();
			if (roleActor.getType().equals("atlassian-user-role-actor")) {
				Set<User> users = roleActor.getUsers();
				//remaining logic need to add here
			}
		}

Hello Prasad

Is it possible to send you some code in private for a opinion, I can't post it online. If ok please email me jiraabraham@gmail.com

Thanks

Abe

Made some progress, just did not know how to get reference to project in the following line:

ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);

you can get project object like this

Issue issue = (Issue) transientVars.get("issue");
Project project=issue.getProjectObject();

Cant write comment so long so writing as answer:

Made some progress, just did not know how to get reference to project in the following line:

ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);

Code written so far:

package com.collab.consulting.plugin.subcomponent.conditions;

import java.util.SortedSet;

import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractJiraCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserUtils;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.util.SimpleErrorCollection;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.roles.DefaultRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleImpl;
import com.atlassian.jira.security.roles.RoleActor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 

public class SubComponentAccessCondition extends AbstractJiraCondition {

	UserUtil userUtil;
	@Override
	public boolean shouldDisplay(User user, JiraHelper jiraHelper) {
		boolean validated = false;
		
		if (user == null) return false;
		
		// Check if the user in project leader role
		
		// Get all actors in the project leader role
		ProjectRoleService projectRoleService = ComponentManager.getComponentInstanceOfType(ProjectRoleService.class);
		SimpleErrorCollection errorCollection = new SimpleErrorCollection();
		ProjectRole projectLeaders = projectRoleService.getProjectRoleByName(user,"project Leaders",errorCollection);
		if (!errorCollection.hasAnyErrors()) {
		ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);
		
			for (Iterator<RoleActor> iterator = actors.getRoleActors().iterator(); iterator.hasNext();) {
				RoleActor roleActor = (RoleActor) iterator.next();
				if (roleActor.getType().equals("atlassian-user-role-actor")) {
				Set<User> roleUsers = roleActor.getUsers();
				//remaining logic need to add here
					for (Iterator<User> it = roleUsers.iterator(); it.hasNext(); ) {
				        User itUser = it.next();
				        if (itUser.getName().equalsIgnoreCase(user.getName())){
				        	return true; // User name matched so returning that user has access
				        }
				    }
				}
			}
		}
		
		// Check if the user is in global admin role , get the groups to which user belongs and see jira-administrators is part of the group
		userUtil = ComponentAccessor.getUserUtil();
		SortedSet<Group> groups = userUtil.getGroupsForUser(user.getName());
		return groups.contains("jira-administrators");
		
	}

}

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,755 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot