Jenkins:如何在 groovy-script 中获取用户 LDAP 组 [英] Jenkins: How to get a users LDAP groups in groovy-script

查看:24
本文介绍了Jenkins:如何在 groovy-script 中获取用户 LDAP 组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 Jenkins 中的自助服务部署设置了一个参数化作业.用户可以选择要部署到的应用程序版本和环境.目前显示给用户的可用环境只是一个静态的字符串列表(选择参数).

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).

现在我想根据当前用户的 LDAP 组将部署限制在某些环境中.

jenkins 中的用户页面显示如下:

The user-page in jenkins displays something like:

詹金斯·贝努策 ID:maku

Jenkins Benutzer Id: maku

组:

adm_proj_a
nexus_admin
ROLE_ADM_PROJ_XY
ROLE_BH_KK

如何在 groovy 脚本中获取这些组?

我尝试使用 动态选择参数 (scriptler) 并使用 groovy 脚本获取 LDAP 组,但没有通过 Jenkins-API 找到方法.

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.

欢迎任何提示

推荐答案

User.getAuthorities() 要求调用者有 ADMINISTER 权限.(http://javadoc.jenkins-ci.org/hudson/模型/User.html#getAuthorities())

User.getAuthorities() requires the caller to have the ADMINISTER permission. (http://javadoc.jenkins-ci.org/hudson/model/User.html#getAuthorities())

另一种方法是直接查询 SecurityRealm.

An alternative is to query the SecurityRealm directly.

import hudson.model.*
import jenkins.model.*

def userid = User.current().id
def auths = Jenkins.instance.securityRealm.loadUserByUsername(userid)
            .authorities.collect{a -> a.authority}
if("adm_proj_a" in auths){
...

这篇关于Jenkins:如何在 groovy-script 中获取用户 LDAP 组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆