How to provide an user overall admin access using groovy script?

I am trying to add an user as an admin in Jenkins using groovy. When i do so, it removes permissions for all the existing users. Any idea on how I can do this.

I tried this code
import jenkins.model.*
import hudson.security.*
def instance = Jenkins.getInstance()
def strategy = new GlobalMatrixAuthorizationStrategy()
strategy.add(Jenkins.ADMINISTER, “foo”)
instance.setAuthorizationStrategy(strategy)
instance.save()

I was expecting to add user foo as an admin in jenkins, instead what happened was all the existing users lost their permissions from jenkins and were denied access. They were displayed an error like “missing overall/read permissions” when trying to access jenkins

Hi there,

“it doesn’t work” is always hard to remote debug. Try to always include these 3 things.

  1. what did you try? (Code samples, command lines, screenshots, videos, etc)
  2. what did happen? (Error messages, description, outputs, stuff)
  3. what did you expect to happen?

Otherwise, it’s hard for us to get into your context and you’ll have to have someone who is exactly knowledgeable come along.

1 Like

Hi Gavin,

Thank you so much for your response. I tried this code
import jenkins.model.*
import hudson.security.*
def instance = Jenkins.getInstance()
def strategy = new GlobalMatrixAuthorizationStrategy()
strategy.add(Jenkins.ADMINISTER, “foo”)
instance.setAuthorizationStrategy(strategy)
instance.save()

I was expecting to add user foo as an admin in jenkins, instead what happened was all the existing users lost their permissions from jenkins and were denied access. They were displayed an error like “missing overall/read permissions” when trying to access jenkins

Your creating a new strategy, adding just the one user to it, then replacing the existing strategy.
essentially you want to get the existing one, then if its not the strategy you want (if you care), replace it, then add the users.

1 Like

Problem got solved. Thanks a lot Gavin