How to identify admins on Jenkins using groovy scripts

39 views
Skip to first unread message

Stas Roshkolets

unread,
Nov 10, 2016, 6:29:56 AM11/10/16
to Jenkins Developers
Hello, I am trying to write Groovy script that will  identify users that they are assigned as admins in Jenkins :

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


Both lists allUsers, adminList print all users in the system...

Oleg Nenashev

unread,
Nov 11, 2016, 12:24:05 PM11/11/16
to Jenkins Developers
Hi Stas,

You do not impersonate within the script, hence your code actually checks if the current user has ADMINISTER permission fo user u.

Example of a working code: https://github.com/jenkinsci/ownership-plugin/blob/0c8e5e65a78567bb152ee1c906620c9c66ea8820/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/util/userFilters/AccessRightsFilter.java#L55-L74 . You will need to replace the checked item by Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)

четверг, 10 ноября 2016 г., 12:29:56 UTC+1 пользователь Stas Roshkolets написал:

Stas Roshkolets

unread,
Nov 13, 2016, 4:33:29 AM11/13/16
to Jenkins Developers
Hi Oleg,
Thank you for fast response :)
Reply all
Reply to author
Forward
0 new messages