Issue on Condition script - Fragments

support for vo August 23, 2022

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
Matthew Clark
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.
August 23, 2022

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

support for vo August 23, 2022

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
AisM September 13, 2023

Hi @Matthew Clark , I have a similar issue, where I want my Script fragment to work for all users. But for some reason, it works only ONCE per user. Can you please help me on this.

Matthew Clark
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.
October 13, 2023

Hi AisM

Sorry for the delay. It has been a busy month.

I am not sure I understand what you mean by once per user. You might be better off raising a support request here so we can have a more detailed discussion regarding your exact setup.

Web items are essentially web page components loaded when users navigate certain Jira user interface sections. If it is only being triggered once, then that would suggest Jira only loads that given section once during your user session.

I cannot say more without knowing your exact web fragment configuration and having a more detailed description of exactly what running once per user means/looks like.

 

Kind Regards

Matthew

Suggest an answer

Log in or Sign up to answer