Hide a UI element depending on Role

Bastian Stehmann
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 15, 2018

Hi community,

 

I am using the "Hide system or plugin UI element" from ScriptRunner Add-On to hide a Link in the UI and it works to hide it for all members of the project.

Is it possible to change the condition so that the link is only hidden for the members of some project roles? I have not found any way to access the roles or the current user yet.

 

Thanks

Bastian

4 answers

1 accepted

0 votes
Answer accepted
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.
April 15, 2018

It would be like this

 

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

def project = jiraHelper.project?.key


String roleName = "Administrators"

if (project != null) {

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);
def projectRole = projectRoleManager.getProjectRole(roleName)

return projectRoleManager.isUserInProjectRole(user, projectRole, issue.getProjectObject())

}

return false
Bastian Stehmann
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2018

Hi @Alexey Matveev,

thank you very much for this quick response.

I had to change the line:

return projectRoleManager.isUserInProjectRole(user, projectRole, issue.getProjectObject())

to 

return projectRoleManager.isUserInProjectRole(user, projectRole, jiraHelper.project)

 because there is not always an issue selected, but now it works great.

 

Best regards

Bastian

Like kumar jira likes this
Megan D March 14, 2024

@Alexey Matveev 
Hey Alexey, I know this an older post, but I'm unsure of where to put the actual project key that I want this code displayed for. There are many projects on our server and I don't want to mess with their setups.

1 vote
Bojana Vasic July 8, 2019

Hi @Alexey Matveev ,

Could you please tell how to modify your suggested code to have some operations visible for two or three different project roles instead of just "Administrators"? Like visible for project roles "Administartors" and "Service Desk Team".

Many thanks! :)

artiom.s August 28, 2019

Hi @Bojana Vasic 

Try this solution for more than one role:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager

def project = jiraHelper.project?.key

if (project != null) {

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);

def projectRoleAdmin = projectRoleManager.getProjectRole("Administrators")
def projectRoleSDT = projectRoleManager.getProjectRole("Service Desk Team")

Boolean isAdmin = projectRoleManager.isUserInProjectRole(user, projectRoleAdmin, jiraHelper.project);
Boolean isSDT = projectRoleManager.isUserInProjectRole(user, projectRoleSDT, jiraHelper.project);

if(isAdmin || isSDT){
return true;
}

}

return false

Best regards,

Artiom

Harsh
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.
March 30, 2023

Is it possible to Hide UI element for a particular status?

0 votes
Alik March 10, 2020

Hi all,

Could you help me please to hide voters from mentioned project in Jira ?

I have tried to hide voters with same code, but it isn't working.

Maybe I am using not correct module ?

I tried this modules

com.atlassian.jira.plugin.system.issueoprations:vote-issue,

com.atlassian.jira.plugin.system.issueoprations:view-voters,

com.atlassian.jira.plugin.system.issueoprations:unvote-issue

0 votes
kumar jira April 17, 2019

.

Suggest an answer

Log in or Sign up to answer