Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,833
Community Members
 
Community Events
168
Community Groups

Issue on Condition script - Fragments

Hello Community,

I have created a Fragment (with ScriptRunner) to hide elements from users in "Developer" role in "MyProject".
I implemented condition below :

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

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)

ProjectActionSupport projectSupport = new ProjectActionSupport()
Project currentProject = ComponentAccessor.getProjectManager().getProjectObj(projectSupport.getSelectedProjectId())

def allowedRoles = ["Developer"] // if current user is in Developer role
def isUserInAllowedRoles = allowedRoles.any{projectRole ->
   def role = projectRoleManager.getProjectRole(projectRole)
   projectRoleManager.isUserInProjectRole(currentUser, role, currentProject)
   }
return (!isUserInAllowedRoles && jiraHelper.project?.key != "MyProject") // hiding elements when issue in MyProject and user is a Developer

With this condition elements are well hidden on the project, but for all roles, not just Developers.
I tested ways to get the right role but nothing to do, it applies to all...
If anyone has an idea to help on this topic, it would be appreciated :)

Thanks !

1 answer

1 accepted

1 vote
Answer accepted

Hi

Your script is almost there, but I think you have your logic the wrong way round for checking the project because you stated you want to do this:

hide elements from users in "Developer" role in "MyProject"

however, your code does this:

!isUserInAllowedRoles && jiraHelper.project?.key != "MyProject"

This means if the user is not in your allowed roles and the project key is not `MyProject`, then return true.
Also, "MyProject" is not a valid key. A project key would be all upper case.

 

If you are trying to make the elements hidden when the logged-in user is a member of a given role for only a specific project, then you should be able to use something like this:

import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.component.ComponentAccessor

def RESTRICTED_ROLES = ['Developers']

def roleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def project = jiraHelper.project

/**
* If the current project key is SSPA check the roles of the current user:
* If the user is NOT in the restricted roles display the item else hide the item
* If the project key is NOT SSPA display the element
*/
project?.key == 'SSPA' ?
!roleManager.getProjectRoles(user, project).any { it.name in RESTRICTED_ROLES } :
true

 

Please try the above and let me know how it goes. Replace the 'Developers' string with the name of your role if it is different, and replace 'SSPA' with the key for the project you are trying to hide elements for.

 

Kind regards,

Matthew

Hello Matthew,

Thanks for providing this quick answer.
I have tested your code a bit remastered and it's working perfectly.
"MyProject" was not the real key ;)

Thank you very much for taking time to reply, it's greatly appreciated.
See you on the Community !

Like # people like this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Jira Software

An update on Jira Software customer feedback – June 2022

Hello Atlassian Community! Feedback from customers like you has helped us shape and improve Jira Software. As Head of Product, Jira Software, I wanted to take this opportunity to share an update on...

5,085 views 18 32
Read article

Atlassian Community Events