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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,554,884
Community Members
 
Community Events
184
Community Groups

How to restrict a ScriptRunner fragment to a specific user?

Hello,

I'm currently working on implementing a solution for a Jira environment, and need to restrict visibility to certain UI elements to only the global administrator.  We have additional admins that we do not want accessing that specific configuration screen.  How can I get the currentUser and basically say if the currentUser equals 'specific user', show the option, otherwise hide it?

Thanks!

1 answer

1 accepted

0 votes
Answer accepted
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.
Oct 26, 2020

Hi Kai

 

To get the current user, you would use the JiraAuthenticationContext

In a fragment condition, I would write it like this:

import com.atlassian.jira.component.ComponentAccessor
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def approvedUsers = ['admin', 'another-admin']
currentUser in approvedUsers

Hey @Peter-Dave Sheehan

The issue is I'm trying to restrict it to just one user, so currentUser would need to equal a specific email or username.  We're trying to avoid using groups because the other admins could just add themselves to the group, thus gaining access to the configuration tool.  IS this possible?

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.
Oct 26, 2020

Yes, that's exactly what the snippet I shared does. Except I made a small mistake, the last line should be:

currentUser.name in approvedUsers

Just specify the username for the users you want to be able to see the option in the array approvedUsers.

You could just do 

currentuser.name  == 'user_a' //where user_a is a username that will be able to see the option

But generally, I like to future proof a little, what if you want a second user later? You could then write:

currentUser.name == 'user_a' || currentUser.name == 'user_b'

But that can get tedious. So instead, you define the list of users in a variable and check that the current user matches

def approvedUsers = ['user_a', 'user_b'] 
currentUser.name in approvedUsers

Now, if you only have 1 user, that's fine too. Just use an array of size 1

def approvedUser = ['user_a']

Ah, yes.  That makes sense, and works like a charm.

Appreciate the help.  I don't do these very often but this will definitely come in handy in the future.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events