user9473385
user9473385

Reputation: 131

How to get list of admin user list in Jenkins via any script?

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.

Upvotes: 5

Views: 5876

Answers (2)

SlashGordon
SlashGordon

Reputation: 880

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])
}

Upvotes: 1

Sagar
Sagar

Reputation: 24947

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)

Upvotes: 1

Related Questions