How to replace all filter views

Jeremy Jedlicka April 28, 2022

Hey, I have the following code that updates all the filters in our Jira instance to ADD the view "All logged in users", but I'm running into difficulties when a filter already has a specified view. This script clashes with that and Jira gives the warning that "all logged in users view cannot be used with others. 

I've been searching how to modify this code to replace all current views instead of add, but haven't had any luck.  I'm thinking the offending line of code is:

permissionsSet.add(new SharePermissionImpl(Name.AUTHENTICATED, null, null));

Can anyone help modifying this to REPLACE all views, and not merely ADD an new view?

Here is my full script:

import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharePermission
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity.SharePermissions
import com.atlassian.jira.sharing.type.ShareType.Name
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.issue.search.SearchRequestManager

def userSearchService = ComponentAccessor.getComponent(UserSearchService)
def userSearchParams = new UserSearchParams.Builder()
        .allowEmptyQuery(true)
        .ignorePermissionCheck(true)
        .maxResults(10000) //Sets the number of users to search
        .build()
def allUsers = userSearchService.findUserNames("", userSearchParams)

for
(String item : allUsers) {
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def filterUser = ComponentAccessor.getUserManager ().getUserByName (item)
SearchRequestService searchRequestService = ComponentAccessor.getComponent(SearchRequestService.class)
Collection<SearchRequest> searchRequests = searchRequestService.getOwnedFilters(filterUser).each{filter->
  SearchRequestManager srm = ComponentAccessor.getComponent(SearchRequestManager.class);
  Set<SharePermission> permissionsSet = new HashSet<SharePermission>(
     filter.getPermissions().getPermissionSet()
            );

 permissionsSet.add(new SharePermissionImpl(Name.AUTHENTICATED, null, null));
 filter.setPermissions(new SharePermissions(permissionsSet));
    srm.update(user, filter);
 }
}

1 answer

1 vote
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.
April 28, 2022

The way to deal with that is to filter out any View permissions from the permissionSet.

Here is an example:

import com.atlassian.jira.sharing.rights.ShareRights

/* ... */
def
newPermissionsList = permissionsSet.collect().findAll{it.rights != ShareRights.VIEW}
newPermissionsList.add(new SharePermissionImpl(null, Name.AUTHENTICATED, null,null, ShareRights.VIEW))
filter.setPermissions(new SharePermissions(newPermissionsList.toSet()))
srm.update(user, filter)

You'll see the new import. 

The next line converts the set to a list via "collect()" and then filters with findAll
We can then add the new permission (I used a newer signature of the SharePermissionImpl constructor that isn't depracated) to the list.

And then finally, we convert the list back to a set before adding it to the filter.

Jeremy Jedlicka April 29, 2022

@Peter-Dave Sheehan

Thanks a lot! That worked wonderfully.

Suggest an answer

Log in or Sign up to answer