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

soerenkornetzki February 6, 2018

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

4 votes
Alexey Matveev
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.
February 6, 2018

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)

0 votes
Vijay Sv June 23, 2021

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