【发布时间】:2017-05-12 21:46:32
【问题描述】:
我正在尝试验证 Nexus Sonatype 配置。 我从这里发现了 Groovy 脚本:
https://github.com/savoirfairelinux/ansible-nexus3-oss/tree/master/templates/groovy
我可以在 Nexus Sonatype 中配置 LDAP,甚至可以创建一个新角色(不是来自 LDAP)。但现在我正在搜索如何获取 LDAP 用户,然后将它们放入特定的组/角色中。
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...)。 我在 org.sonatype.security 组的 nexus-public 存储库中进行了搜索,但老实说,我不了解他们的类...: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 类型有问题,我尝试使用 '[]' 但不是更好..
【问题讨论】:
-
我们对您要完成的工作感到有些困惑。您要查找现有的已创建 LDAP 用户,然后更改来源吗?
-
没有。配置 LDAP 后,我将在 LDAP 中搜索用户并将其添加到组中。实际上,在进行任何操作之前,我需要将 Source 更改为 LDAP,而不是“默认”(它是搜索用户的源)。
标签: java groovy nexus sonatype