Unable to create LDAP user (empty LDAPProfileClass object)

When creating users with values from our LDAP server with the following groovy script, the object “XWiki.LDAPProfileClass” is empty, regardless if we use “getObject” or “newObject”:

def create_ldap_user(user) {
    udoc = xwiki.getDocument("XWiki."+user)
    uobj = udoc.getObject("XWiki.XWikiUsers", true)
    uobj.set("active", 1)
    lobj = udoc.getObject("XWiki.LDAPProfileClass", true)
    //lobj = udoc.newObject("XWiki.LDAPProfileClass")
    lobj.set("dn" , "uid=" + user + ",ou=Users,dc=example,dc=com")
    lobj.set("uid" , user)
    udoc.save()
}

Newly created user profile in object inspector

After creation the object inspector shows a class of type “XWiki.LDAPProfileClass” attached to the user, but it does not contain any attributes. When clicking on “New LDAPProfileClass object”, we only get the error “Failed:Not Found”.
error message

This only happens on a newly installed instance, that is configured for LDAP login, as long as no LDAP user ever logged in.

Why does this happen and how can we avoid it? Thanks in advance!

The XWiki.LDAPProfileClass class is automatically generated by the LDAP authenticator the first time it creates a user, so that’s probably your problem.

Thanks, we suspected as much… is there any possibility of manually triggering the class creation via a groovy script?

It’s initialized in the constructor of the class org.xwiki.contrib.ldap.LDAPProfileXClass so something like the following Groovy script should do it:

new org.xwiki.contrib.ldap.LDAPProfileXClass(context.context)

Thank you for the pointer! After some trial and error, we could get it to work with this groovy script:

def create_ldap_user(user) {
    udoc = xwiki.getDocument("XWiki."+user)
    uobj = udoc.getObject("XWiki.XWikiUsers", true)
    uobj.set("active", 1)
    lobj = udoc.getObject("XWiki.LDAPProfileClass", false)
    if (!lobj) {
        new org.xwiki.contrib.ldap.LDAPProfileXClass(xcontext.context)
        lobj = udoc.getObject("XWiki.LDAPProfileClass", true)
        print("created XWiki.LDAPProfileClass\n")
    }
    lobj.set("dn" , "uid=" + user + ",ou=Users,dc=example,dc=com")
    lobj.set("uid" , user)
    udoc.save()
}
create_ldap_user("hmuster")

Glad you found a working solution !