How to get current users LDAP groups in groovy-script

1,012 views
Skip to first unread message

m...@pascada.de

unread,
Jun 23, 2014, 5:36:22 AM6/23/14
to jenkins...@googlegroups.com
I have setup a parametrized job for self-service deployments in Jenkins.
Users can select a version of the application and the environment to deploy to.
The available environments displayed to the user is currently just a static list of strings (choice parameter).

Now i want to restrict deployments to some environments based on the LDAP-groups of the current user.

My user-page in jenkins displays something like:


Jenkins User Id: maku

Groups:

    adm_proj_a
    nexus_admin
    ROLE_ADM_PROJ_XY
    ROLE_BH_KK

How do i get these groups within a groovy-script?

I tried to use dynamic choice parameter (scriptler) and get the LDAP-groups using a groovy-script but did not find my way through the Jenkins-API.

Any hints welcome

PS: Question also posted here:  http://stackoverflow.com/questions/24279864/jenkins-how-to-get-a-users-ldap-groups-in-groovy-script

m...@pascada.de

unread,
Jun 25, 2014, 9:00:14 AM6/25/14
to jenkins...@googlegroups.com
I found a solution. Just in case anybody is interested:

Within scriptler i created a script groovy-script similar to this:

    import hudson.model.*
    
    def allowed_environments = ["dev","test","test-integration"]  
    
    if ("adm_proj_a" in User.current().getAuthorities() )
    {
       allowed_environments.add("production")
    }
    
     return allowed_environments


This script is used by dynamic choice parameter (scriptler) within my Jenkins-Job.

Now only users within the group adm_proj_a can see production as a choice.
Reply all
Reply to author
Forward
0 new messages