5

I am trying to get the list of admin user list and also the users and their level of permissions in Jenkins.

Can anyone help me with any script available please.

daspilker
  • 8,154
  • 1
  • 35
  • 49
user9473385
  • 131
  • 2
  • 8

2 Answers2

1

You can retrieve all the users using groovy script as follows:

import hudson.model.User

allUsers = User.getAll()
adminList = []

for (u in allUsers) {

    if (u.hasPermission(Jenkins.ADMINISTER)) {
        adminList.add(u)
    }
}

println(adminList)
Sagar
  • 23,903
  • 4
  • 62
  • 62
1

I use the following script to create a admin user list. The script also works with Matrix-based security.

import hudson.model.User

def inst = Jenkins.getInstanceOrNull()
def strategy = inst.getAuthorizationStrategy()
def adminUserList = User.getAll().findAll { user ->
    strategy.hasPermission(user.id, Jenkins.ADMINISTER)
}

All users with their permissions are accessible as follows:

import hudson.model.User
import hudson.security.Permission
def inst = Jenkins.getInstanceOrNull()
def strategy = inst.getAuthorizationStrategy()
strategy.getMetaPropertyValues().find { it.getName() == 'grantedPermissions'}.each {
    // it.value is a map with permission as key and the corresponding users as value
    print(it.value[Permission.READ])
    print(it.value[Permission.WRITE])
    print(it.value[Permission.HUDSON_ADMINISTER])
}
SlashGordon
  • 720
  • 8
  • 11