How do I find Groups

AbrahamA
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.
September 1, 2013

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
Answer accepted
RambanamP
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.
September 1, 2013

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
			}
		}

AbrahamA
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.
September 2, 2013

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

AbrahamA
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.
September 2, 2013

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);

RambanamP
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.
September 2, 2013

you can get project object like this

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

0 votes
Jobin Kuruvilla [Adaptavist]
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.
September 2, 2013
0 votes
AbrahamA
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.
September 2, 2013

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