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.ProjectActionSupportimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.security.roles.ProjectRoleManagerimport com.atlassian.jira.security.roles.ProjectRoleimport com.atlassian.jira.project.Projectdef 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 roledef 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 !
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 !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Join the largest European gathering of the Atlassian Community and reimagine what’s possible when great teams and transformative technology come together. Plus, grab your Super Fan ticket now and save over €1,000 on your pass before prices rise on 3 June.
Register nowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.