Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
titleMake dashboards filter accessible to admin
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchRequestEntity
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.sharing.SharePermission
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.util.Visitor

// This function will share the filter with ID "filterId" with the group "jira-administrators"
def makeFilterAccessibleToAdmins(long filterId) {
    ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
	SearchRequestManager srm = ComponentAccessor.getComponent(SearchRequestManager.class);
	
	SearchRequest sr = srm.getSearchRequestById(filterId);
    Set<SharePermission> permissionsSet = new HashSet<SharePermission>(
        sr.getPermissions().getPermissionSet()
    );
    permissionsSet.add(new SharePermissionImpl(null, Name.GROUP, "jira-administrators", null));
    sr.setPermissions(new SharePermissions(permissionsSet));
    
    srm.update(sr);
}

// Pass the ID of the filter you want to update
makeFilterAccessibleToAdmins(FILTERIDHERE);

...

Code Block
languagegroovy
titleMake all dashboards filters global
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchRequestEntity
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.sharing.SharePermission
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.util.Visitor

def makeAllFiltersGlobal() {
    def changedFilters = [];
    
    ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
	SearchRequestManager srm = ComponentAccessor.getComponent(SearchRequestManager.class);
    
    srm.visitAll(new Visitor<SearchRequestEntity>() {
        
        void visit(SearchRequestEntity e) {            
            SearchRequest sr = srm.getSearchRequestById(e.id)
            Set<SharePermission> permissionsSet = new HashSet<SharePermission>(
                sr.getPermissions().getPermissionSet()
            );
            
            // check if the filter is already global
            if (!permissionsSet.any { it.getType() == Name.GLOBAL } ) {
            	changedFilters.add(e.getId());
            
            	permissionsSet.add(new SharePermissionImpl(Name.GLOBAL, null, null));
            	sr.setPermissions(new SharePermissions(permissionsSet));
            	
                srm.update(sr);
            }
        }
    });
    
    return changedFilters;
}


makeAllFiltersGlobal();

...


Code Block
languagegroovy
titleRemove global share permission from dashboardsfilters
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchRequestEntity
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.sharing.SharePermission
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.util.Visitor

// Remove the global share permission from a filter.
def removeGlobalSharePermission(long filterId) {
    ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
	SearchRequestManager srm = ComponentAccessor.getComponent(SearchRequestManager.class);
	
	SearchRequest sr = srm.getSearchRequestById(filterId);
    Set<SharePermission> permissionsSet = new HashSet<SharePermission>(
        sr.getPermissions().getPermissionSet()
    );
    
    permissionsSet.removeAll { it.getType() == Name.GLOBAL };
    sr.setPermissions(new SharePermissions(permissionsSet));
    
    srm.update(sr);
}

// Remove the global share permission from a list of filters.
def removeGlobalSharePermission(List ids) {
    for (id in ids) {
        removeGlobalSharePermission((long)id);
    }
}

// List filter ids
def ids = [10000, 10001, 10002];

removeGlobalSharePermission(ids);


...