问题
I want to be able to check whether the current build user who is running the job is admin or not. I used some APIs to figure out the admins user list as shown below
import jenkins.model.Jenkins
import hudson.model.User
import hudson.security.Permission
allUsers = User.getAll()
adminList = []
for (u in allUsers) {
if (u.hasPermission(Jenkins.ADMINISTER)) {
// if (u.canDelete()) {
adminList.add(u)
}
}
println allUsers
println adminList
but always both the list of users (admins and average-joes) are same, yes i am sure, not all users are admin in my jenkins server :)
回答1:
import jenkins.model.Jenkins
import hudson.model.User
allUsers = User.getAll()
adminList = []
authStrategy = Jenkins.instance.getAuthorizationStrategy()
for (u in allUsers) {
if (authStrategy.hasPermission(u.toString(), Jenkins.ADMINISTER)) {
adminList.add(u)
}
}
println(adminList)
来源:https://stackoverflow.com/questions/28953687/how-to-identify-admins-on-jenkins-using-groovy-scripts