Jenkins Extensible Choice with user specific items based on users Roles

OK, after a few more searches I came across the source (https://github.com/jenkinsci/role-strategy-plugin/tree/master/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy)

After further reading and a bit of playing around I came up with this...

import com.michelin.cio.hudson.plugins.rolestrategy.*

def result = ["-- Please Select --"]
def authStrategy = jenkins.model.Jenkins.instance.getAuthorizationStrategy()

if(authStrategy instanceof RoleBasedAuthorizationStrategy){
    def currentUser = jenkins.model.Jenkins.instance.getAuthentication().getName();
    def roleMap= authStrategy.roleMaps.get("globalRoles")

    def sids= roleMap.getSidsForRole("Manage_Dev")
    if(sids != null && sids.contains(currentUser)) {
        result.add("dev1")
        ...
    }

    sids= roleMap.getSidsForRole("Manage_Test")
    if(sids != null && sids.contains(currentUser)) {
        result.add("tst1")
        ...
    }
    ...
}

return result

Which works for me. Easy when you know how!

Tags:

Jenkins