The script below adds mapping from LDAP role to nexus role.
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.sonatype.nexus.security.SecuritySystem
def request = new JsonSlurper().parseText(args)
String ldap = request.ldap
String name = request.name ?: request.ldap
String nexus = request.nexus
assert ldap != null && nexus != null
def role = security.addRole(ldap, name, "Mapping for LDAP "+ldap, [], [nexus]);
JsonOutput.prettyPrint(JsonOutput.toJson(role))
The parameter 'ldap' is LDAP role name and 'nexus' is nexus role name (like 'nx-admin'). Note after adding mapping two roles with the same id will appear (one for LDAP source, other for default). Nexus apparently correlate them by id. Script below lists all roles (LDAP and default one). You might need to pass user LDAP user name to this script in order to LDAP roles to appear, because LDAP is deactivated if it is not used for some time, in that case script will show only nexus roles.
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.sonatype.nexus.security.SecuritySystem
SecuritySystem securitySystem = container.lookup(SecuritySystem.class.name)
if(args != null && args.length() > 0) {
def request = new JsonSlurper().parseText(args)
if(request.user != null && request.user.length() > 0) {
securitySystem.getUser(request.user)
}
}
JsonOutput.prettyPrint(JsonOutput.toJson(securitySystem.listRoles()))