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

Scriptrunner Fragment the option More -> Labels in issues of 4 Jira projects only for certain users

Marco Brundel
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 10, 2022

Hi,

Via a Scriptrunner Fragment I want to have the option More -> Labels in issues of 4 Jira projects only available for users in the project roles 'Administrators', 'Scrum Masters', 'Productowners'.

With the following script I am able to make the option More -> Labels in issues available only to users in the project roles 'Administrators', 'Scrum Masters', 'Productowners', but this now applies to all jira projects.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.action.ProjectActionSupport
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleManager

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
ProjectActionSupport projectSupport = new ProjectActionSupport();

Project currentProject = ComponentAccessor.getProjectManager().getProjectObj(projectSupport.getSelectedProjectId());

def projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager)

def allowedRoles = ['Administrators', 'Scrum Masters', 'Productowners','Viewers','Service Desk Team']
def isUserInAllowedRoles = allowedRoles.any{ projectRole ->

   def role = projectRoleManager.getProjectRole(projectRole)

   projectRoleManager.isUserInProjectRole(currentUser, role, currentProject)

}

return ( isUserInAllowedRoles || !jiraHelper.project?.key in ["SH3","SF3","SI3","SM3"])
System does not respond (properly) to

return ( isUserInAllowedRoles || !jiraHelper.project?.key in ["SH3","SF3","SI3","SM3"])

  

How do I modify the script so that it only applies to the 4 jira projects?

 

Thank you in advance.

 

Regards, Marco Brundel

 

 

This is a follow up question to 

https://community.atlassian.com/t5/Adaptavist-questions/How-can-a-Scriptrunner-Fragment-make-it-apply-to-multiple/qaq-p/1932960

Screenshot 2022-02-10 at 14.28.27.png

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 10, 2022

WIthout actually having tested anything, why don't you just leverage the "currentProject" object you're already getting via the ProjectActionSupport (I've never used that).

I would do this first so you don't bother checking permissions if the project isn't correct.

You could do it like this (a bit of future-proofing if you have other projects/role combinations later)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.action.ProjectActionSupport
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleManager

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

ProjectActionSupport projectSupport = new ProjectActionSupport();

Project currentProject = ComponentAccessor.getProjectManager().getProjectObj(projectSupport.getSelectedProjectId());
def controlledProjectMaps = [
[
projects: ["SH3", "SF3", "SI3", "SM3"],
roles : ['Administrators', 'Scrum Masters', 'Productowners', 'Viewers', 'Service Desk Team']
]
]
if (!controlledProjectMaps.any { it.projects.contains(currentProject.key) }) {
//keep current functionality for non-controlled projects
return true
}
def projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager)
def allowedRoles = controlledProjectMaps.find{it.projects.contains(currentProject.key)}.roles
def isUserInAllowedRoles = allowedRoles.any { projectRole ->
def role = projectRoleManager.getProjectRole(projectRole)
projectRoleManager.isUserInProjectRole(currentUser, role, currentProject)
}
return isUserInAllowedRoles
TAGS
AUG Leaders

Atlassian Community Events