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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to check if current user is in a project role?

I have a shared transition from two statuses (e.g. "open" and "verified") to one (e.g. "ready").

I want to enable users to use the transition when the source status is "verified".

Only users of two project roles (e.g. "admin", "heros") should be allowed to use the transition from "open" ro "ready" without going over "verified".

I set up the following Groovy script to accomplish the first part but struggle with the roles.

def project = issue.getProjectObject();

log.debug( project.getLeadUserKey() );


def verifiedStatusId = "10008";

// God mode (check for Project Leads or Administrators)


// Regular mode (check if source status is "Verified")
if ( issue.getStatusId() == verifiedStatusId ) {
  return true;
}

return false;

Currently I see no way to determine the project roles assigned to the current user by the user object itself. Also the getProjectObject() just returns very generic methods (e.g. project name) which are irrelevant for my task.

How to I check if the current user is in one or more of two project roles of the issue of the project?

2 answers

You can receive the current user with 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggiedInUser()

then you can use 

ComponentAccessor.getGroupManager().isUserInGroup(user, "admin") || ComponentAccessor.getGroupManager().isUserInGroup(user, "heroes") 

That was for groups. For roles it will be like this

ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class);

projectRoleManager.isUserInProjectRole(ApplicationUser user, ProjectRole projectRole, Project project)

Use something like this if you're using Scriptrunner Behaviours.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRole

if (underlyingIssue != null) {
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def componentManager = ComponentManager.getInstance();
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class) as ProjectRoleManager
def role = projectRoleManager.getProjectRole("XXXXX") as ProjectRole

 

if (currentUser != null && role != null && projectRoleManager.isUserInProjectRole(currentUser, role, underlyingIssue.getProjectObject()) ) {

return true

}

else return false

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you