How to get a list of users who does not have Admin privilege ( Groovy jenkins)
def inst = Jenkins.getInstanceOrNull()
def strategy = inst.getAuthorizationStrategy()
def adminUserList = User.getAll().findAll { user ->
strategy.hasPermission(user.id, Jenkins.ADMINISTER)
}
This is giving me a list of users who has ADMINISTER privilege. How to retrieve the list of all users who does not have ADMINISTER privilege.
CodePudding user response:
You can just subtract the adminUserList
from the allUserList
and you will have the non admin users.
def inst = Jenkins.getInstanceOrNull()
def strategy = inst.getAuthorizationStrategy()
def adminUserList = User.getAll().findAll { user ->
strategy.hasPermission(user.id, Jenkins.ADMINISTER)
}
def allUserList = User.getAll()
print allUserList - adminUserList
CodePudding user response:
A simple negation helped to get the expected output :
def inst = Jenkins.getInstanceOrNull()
def strategy = inst.getAuthorizationStrategy()
def adminUserList = User.getAll().findAll { user ->
!strategy.hasPermission(user.id, Jenkins.ADMINISTER)
}
Just changed "strategy.hasPermission" to "!strategy.hasPermission", this has given the expected output.