Nexus sonatype groovy获得LDAP用户

时间:2016-12-28 15:21:19

标签: java groovy nexus sonatype

我正在尝试验证Nexus Sonatype配置。 我从这里发现了Groovy脚本:

https://github.com/savoirfairelinux/ansible-nexus3-oss/tree/master/templates/groovy

我可以在Nexus Sonatype中配置LDAP,甚至可以创建新角色(不是来自LDAP)。但现在我正在搜索如何获取LDAP用户,然后将它们放在特定的组/rôle中。

Groovy脚本如下:

import groovy.json.JsonSlurper
import org.sonatype.nexus.security.user.UserNotFoundException


parsed_args = new JsonSlurper().parseText(args)

try {
  // update an existing user
  user = security.securitySystem.getUser(parsed_args.username)

  /* I tried with 'setSource' but doesn't works... */
  user.setSource(parsed_args.source)

  user.setFirstName(parsed_args.first_name)
  user.setLastName(parsed_args.last_name)
  user.setEmailAddress(parsed_args.email)
  security.setUserRoles(parsed_args.username, parsed_args.roles)
  security.securitySystem.updateUser(user)
  security.securitySystem.changePassword(parsed_args.username, parsed_args.password)
  security.setUserRoles(parsed_args.username, parsed_args.roles)

} catch(UserNotFoundException ignored) {
  // create the new user
  security.addUser(parsed_args.username, parsed_args.first_name, parsed_args.last_name, parsed_args.email, true, parsed_args.password, parsed_args.roles)
  }

在“用户”标签中,Nexus选择“默认”源(不是LDAP ...)。 我搜索了orgus-public存储库,在org.sonatype.security组中,但说实话,我不理解他们的类...:https://github.com/sonatype/nexus-public/tree/master/components/nexus-security/src/main/java/org/sonatype/nexus/security

有人已经这样做了吗?

编辑:

我试过了:

import groovy.json.JsonSlurper
import org.sonatype.nexus.security.user.UserNotFoundException
import org.sonatype.nexus.security.user.UserSearchCriteria

parsed_args = new JsonSlurper().parseText(args)


criteria = new UserSearchCriteria(userId: 'myUser', source: 'LDAP')
user = security.securitySystem.searchUsers(criteria)
//user.forEach { println it }
security.setUserRoles(user.userId, 'myRole')
security.securitySystem.updateUser(user)

现在我的错误是:

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.sonatype.nexus.security.internal.SecurityApiImpl.setUserRoles() is applicable for argument types: (java.util.ArrayList, java.util.ArrayList) values: [[myUser], [myRole]]\\nPossible solutions: setUserRoles(java.lang.String, java.util.List)\"\n}", "content_type": "application/json", "date": "Fri, 30 Dec 2016 10:05:51 GMT", "failed": true, "json": {"name": "setup_user", "result": "javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.sonatype.nexus.security.internal.SecurityApiImpl.setUserRoles() is applicable for argument types: (java.util.ArrayList, java.util.ArrayList) values: [[myUser], [myRole]]\nPossible solutions: setUserRoles(java.lang.String, java.util.List)"}, "msg": "Status code was not [200, 204]: HTTP Error 400: Bad Request

也许,我有一个ArrayList类型的问题,我试过'[]'但不是更好..

1 个答案:

答案 0 :(得分:0)

为了找到所有LDAP用户,你可以做这样的事情

import org.sonatype.nexus.security.user.UserSearchCriteria
criteria = new UserSearchCriteria(source: 'LDAP')
users = security.securitySystem.searchUsers(criteria)
users.forEach { println it }

从那里开始,我不确定你为什么要切换源代码,但是这将为你提供所有LDAP用户的列表。