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

1 vote
Accepted answer

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 Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,082 views 0 8
Read article

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