Jenkins, update the project security via a groovy script not working

1,111 views
Skip to first unread message

RainerW

unread,
Jul 12, 2011, 8:02:32 AM7/12/11
to Jenkins Users
I using a Jenkin s1.409.1 and try to change the Security Settings for
a bunch of projects via an groovy script [SCRIPT].

The script runs on the Master ( via "Manage Jenkins"\"Script
Console" ). All matching projects seemed to have beed modified : E.g.
the Job configuration dialog shows the changed Authorization Matrix,
and the %JOB%/config.xml file is updated.

Only Jenkins doesn't seem to get it. The "build" user (The one now
should have rights) still isn't seeing the project. I can relogin,
reload / restart Jenkins (Winstone), nothing changes.

When i just press the save button in the job configuration ... magic
happens. Now the the security has been updated, the build user can see
the project immediately.

So what did i miss? Which magic does the save Button, which i'm not
doing?

Cheers Rainer

SCRIPT:

import hudson.security.*
import hudson.model.*
items = hudson.model.Hudson.instance.items;
for (job in items) {
if (job.name.matches("INTER.*")) {
println("JOB: " + job)
props = job.properties;
a = new AuthorizationMatrixProperty();
// QA group, 're-add' coz there is only one property for all
users
user = "qa"
a.add(Item.WORKSPACE,user);
a.add(Item.BUILD,user);
a.add(Run.DELETE,user);
a.add(Run.UPDATE,user);
a.add(Item.CONFIGURE,user);
a.add(Item.DELETE,user);
a.add(Item.READ,user);
// Build User
user = "build"
a.add(Item.WORKSPACE,user);
a.add(Item.BUILD,user);
// a.add(Run.DELETE,user);
a.add(Run.UPDATE,user);
a.add(Item.CONFIGURE,user);
a.add(Item.DELETE,user);
a.add(Item.READ,user);
// save it
job.addProperty( a )
job.save()
}
}


achala.rat...@gmail.com

unread,
Jun 26, 2015, 4:03:06 AM6/26/15
to jenkins...@googlegroups.com, rets...@googlemail.com
Hi,

This is the way to update user access using groovy scrip:

import hudson.model.*
import jenkins.security.*
  import hudson.security.*
 
 
// For each project
for(item in Hudson.instance.items) {
  if(item.name.equalsIgnoreCase("AuthenticationChecker_SE"))
  {
    println(item.name)
 
 println("Current USer "+User.current());    
 
      def authorizationMatrixProperty = item.getProperty(AuthorizationMatrixProperty.class)
 
   User  user=  User.current();
    String sID=  user.getId() ;
      authorizationMatrixProperty?.add(hudson.model.Item.CANCEL,sID)
       authorizationMatrixProperty?.add(Item.WORKSPACE,sID);
       authorizationMatrixProperty?.add(Item.BUILD,sID);
       authorizationMatrixProperty?.add(Run.DELETE,sID);
       authorizationMatrixProperty?.add(Run.UPDATE,sID);
       authorizationMatrixProperty?.add(Item.CONFIGURE,sID);
       authorizationMatrixProperty?.add(Item.DELETE,sID);
      authorizationMatrixProperty?.add(Item.READ,sID);
   
      item.addProperty( authorizationMatrixProperty )
      item.save()
   
   
  }
}

Thanks and best regards,
Achala M. Rathnathilaka
Java software Enginner,
Sri Lanka.
Reply all
Reply to author
Forward
0 new messages