Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Create a user picker field with different lists depending on the user

이나은 July 26, 2023

I'm trying to make a 'User Picker' field.

This field should show the list of users in a specific group of currently logged-in users as select list.

'User Filtering' in Custom field is not available because the filter must be different depending on the current logged in user.

So I wrote the script below using Scriptrunner's custom picker, but the code runs only once and the list looks the same to all users.

 

import com.atlassian.jira.avatar.Avatar
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserFilter
import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption

def userSearchService = ComponentAccessor.getComponent(UserSearchService)
def userManager = ComponentAccessor.userManager
def avatarService = ComponentAccessor.avatarService
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
def groupManager = ComponentAccessor.groupManager

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def groups = groupManager.getGroupNamesForUser(currentUser)
ArrayList<String> resGroups = []

def mLen = 255
def aGroup = ""
for(group in groups){
    if(group.indexOf("something")==-1){
        if(group.length() <= mLen ){
            mLen = group.length()
            aGroup = group
        }
    }
}
if(aGroup!="") resGroups.add(aGroup)

def userFilter = new UserFilter(true, null, resGroups)
def userSearchParams = new UserSearchParams.Builder().allowEmptyQuery(true).filter(userFilter).maxResults(30).build()

search = { String inputValue ->
    userSearchService.findUsers(inputValue, userSearchParams)
}

getItemFromId = { String id ->
    userManager.getUserByKey(id)
}

renderItemViewHtml = { ApplicationUser user ->
    user.displayName
}

renderItemTextOnlyValue = renderItemViewHtml

toOption = { ApplicationUser user, Closure<String> highlight ->
    def remoteUser = authenticationContext.loggedInUser

    new PickerOption(
        value: user.key,
        label: user.displayName,
        html: highlight(user.displayName, false),
        icon: avatarService.getAvatarURL(remoteUser, user, Avatar.Size.SMALL)?.toString()
    )
}
Is there a way to create a field that filters the value of the list differently for each user (the logged-in user)?

1 answer

0 votes
Mohanraj Thangamuthu
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 27, 2023

Hello, Good day. We don't have this option at the moment. I believe this FR : https://jira.atlassian.com/browse/JRACLOUD-40283 is similar to your requirement. 

이나은 July 27, 2023

Hello, thank you for your reply.
I modified my script and it works the way I want it to.
UserSearchParams-related settings have been put into the search so that users can query and import new data whenever they try to enter a field.

import com.atlassian.jira.avatar.Avatar
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserFilter
import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption

 

def userSearchService = ComponentAccessor.getComponent(UserSearchService)
def userManager = ComponentAccessor.userManager
def avatarService = ComponentAccessor.avatarService
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
def groupManager = ComponentAccessor.groupManager
search = { String inputValue ->
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

def groups = groupManager.getGroupNamesForUser(currentUser)
ArrayList<String> resGroups = []

 

def mLen = 255
def aGroup = ""
for(group in groups){
    if(group.indexOf("something")==-1){
        if(group.length() <= mLen ){
            mLen = group.length()
            aGroup = group
        }
    }
}
if(aGroup!="") resGroups.add(aGroup)

 

def userFilter = new UserFilter(truenull, resGroups)
def userSearchParams = new UserSearchParams.Builder().allowEmptyQuery(true).filter(userFilter).maxResults(30).build()
    userSearchService.findUsers(inputValue, userSearchParams)
}

 

getItemFromId = { String id ->
    userManager.getUserByKey(id)
}

 

renderItemViewHtml = { ApplicationUser user ->
    user.displayName
}

 

renderItemTextOnlyValue = renderItemViewHtml

 

toOption = { ApplicationUser userClosure<String> highlight ->
    def remoteUser = authenticationContext.loggedInUser

 

    new PickerOption(
        value: user.key,
        label: user.displayName,
        html: highlight(user.displayName, false),
        icon: avatarService.getAvatarURL(remoteUser, user, Avatar.Size.SMALL)?.toString()
    )
}

Suggest an answer

Log in or Sign up to answer