Pregunta

Please bear with me as I am new to spring and ldap.

If I want to map users from an ldap server to a Person java object, does it matter if the query from the ldap server contains the objectclass or not?

My understanding is that an object class can be something that ldap has predefined, such as "person", "organizationalPerson", "inetOrgPerson", etc. Ldap can also hold custom defined objectclasses. If an objectclass=person is defined, then it is guaranteed that the "cn", "sn" will be defined, and so forth.

The below modified sample code is supposed to map the ldap attributes to a java Person object:

 private static class PersonContextMapper
         implements ContextMapper {
      public Object mapFromContext(Object ctx) {
         DirContextAdapter context = (DirContextAdapter)ctx;
         Person p = new Person();

         if (context.getStringAttribute("cn") != null)
               p.setFullName(context.getStringAttribute("cn"));

         if (context.getStringAtribute("sn") != null)
               p.setLastName(context.getStringAttribute("sn"));

         return p;
      }
   }

Here, it makes no reference to objectclass=person, but it looks for the attributes as though objectclass=person has definitely been set. What if users in this ldap directory is also part of objectclass=CustomPerson, where the attributes "goals", "salary" are defined?

Then, without knowing whether this user is also objectclass=CustomPerson, would it be as simple as adding a check like so:

if (context.getStringAttribute("goals") != null) 
     p.setGoals(context.getStringAttribute("goals"));

Or, what if this user is ONLY part of objectclass=CustomPerson and NOT objectclass=person. Then, looking for "cn" and "sn" wouldn't make sense because only "goals" and "salary" are defined?

Am I missing something, or is it just assumed that the user attributes are already known ahead of time, and so we can hard code in the mappings between the ldap user and a java Person object?

¿Fue útil?

Solución

The Java code doesn't need to know the LDAP object class. It just gets attributes. Either the LDAP object has those attributes, in which case you get them, or it doesn't, in which case you get an exception.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top