How to identify admins on Jenkins using groovy scripts

与世无争的帅哥 提交于 2019-12-24 03:28:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!