X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2Fdirectory%2Fldap%2FDefaultLdapEntry.java;h=c01d1c3c1a3e5b898f34b4b0cc48bf9a0b532034;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=8db662393e5ee1fb702664465a2692616ef17e68;hpb=b9810eb23f4e2470952a04c07d15a523c3c61a0e;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/directory/ldap/DefaultLdapEntry.java b/org.argeo.util/src/org/argeo/util/directory/ldap/DefaultLdapEntry.java index 8db662393..c01d1c3c1 100644 --- a/org.argeo.util/src/org/argeo/util/directory/ldap/DefaultLdapEntry.java +++ b/org.argeo.util/src/org/argeo/util/directory/ldap/DefaultLdapEntry.java @@ -32,20 +32,44 @@ public class DefaultLdapEntry implements LdapEntry { private final LdapName dn; - private Attributes publishedAttributes; + private AttributeDictionary properties; + private AttributeDictionary credentials; - // Temporarily expose the fields - protected AttributeDictionary properties; - protected AttributeDictionary credentials; +// private String primaryObjectClass; +// private List objectClasses = new ArrayList<>(); - protected DefaultLdapEntry(AbstractLdapDirectory directory, LdapName dn, Attributes attributes) { + protected DefaultLdapEntry(AbstractLdapDirectory directory, LdapName dn) { Objects.requireNonNull(directory); Objects.requireNonNull(dn); this.directory = directory; this.dn = dn; - this.publishedAttributes = attributes; -// properties = new AttributeDictionary(false); -// credentials = new AttributeDictionary(true); + + // Object classes +// Objects.requireNonNull(initialAttributes); +// try { +// NamingEnumeration en = initialAttributes.get(LdapAttrs.objectClass.name()).getAll(); +// String first = null; +// attrs: while (en.hasMore()) { +// String v = en.next().toString(); +// if (v.equalsIgnoreCase(LdapObjs.top.name())) +// continue attrs; +// if (first == null) +// first = v; +// if (v.equalsIgnoreCase(getDirectory().getUserObjectClass())) +// primaryObjectClass = getDirectory().getUserObjectClass(); +// else if (v.equalsIgnoreCase(getDirectory().getGroupObjectClass())) +// primaryObjectClass = getDirectory().getGroupObjectClass(); +// objectClasses.add(v); +// } +// if (primaryObjectClass == null) { +// if (first == null) +// throw new IllegalStateException("Could not find primary object class"); +// primaryObjectClass = first; +// } +// } catch (NamingException e) { +// throw new IllegalStateException("Cannot find object classes", e); +// } + } @Override @@ -54,10 +78,7 @@ public class DefaultLdapEntry implements LdapEntry { } public synchronized Attributes getAttributes() { - // lazy loading - if (publishedAttributes == null) - publishedAttributes = getDirectory().getDirectoryDao().doGetAttributes(dn); - return isEditing() ? getModifiedAttributes() : publishedAttributes; + return isEditing() ? getModifiedAttributes() : getDirectory().getDirectoryDao().doGetAttributes(dn); } @Override @@ -104,7 +125,7 @@ public class DefaultLdapEntry implements LdapEntry { } public synchronized void publishAttributes(Attributes modifiedAttributes) { - publishedAttributes = modifiedAttributes; +// publishedAttributes = modifiedAttributes; } /* @@ -119,7 +140,7 @@ public class DefaultLdapEntry implements LdapEntry { public Dictionary getCredentials() { if (credentials == null) - credentials = new AttributeDictionary(false); + credentials = new AttributeDictionary(true); return credentials; } @@ -164,7 +185,7 @@ public class DefaultLdapEntry implements LdapEntry { return false; } - Object storedValue = credentials.get(key); + Object storedValue = getCredentials().get(key); if (storedValue == null || value == null) return false; if (!(value instanceof String || value instanceof byte[])) @@ -374,29 +395,48 @@ public class DefaultLdapEntry implements LdapEntry { @Override public Object put(String key, Object value) { - if (key == null) { - // TODO persist to other sources (like PKCS12) - char[] password = DirectoryDigestUtils.bytesToChars(value); - byte[] hashedPassword = sha1hash(password); - return put(LdapAttrs.userPassword.name(), hashedPassword); - } - if (key.startsWith("X-")) { - return put(LdapAttrs.authPassword.name(), value); - } + Objects.requireNonNull(value, "Value for key " + key + " is null"); + try { + if (key == null) { + // FIXME remove this "feature", a key should be specified + // TODO persist to other sources (like PKCS12) + char[] password = DirectoryDigestUtils.bytesToChars(value); + byte[] hashedPassword = sha1hash(password); + return put(LdapAttrs.userPassword.name(), hashedPassword); + } + if (key.startsWith("X-")) { + return put(LdapAttrs.authPassword.name(), value); + } - getDirectory().checkEdit(); - if (!isEditing()) - startEditing(); + // start editing + getDirectory().checkEdit(); + if (!isEditing()) + startEditing(); + + // object classes special case. + if (key.equals(LdapAttrs.objectClasses.name())) { + Attribute attribute = new BasicAttribute(LdapAttrs.objectClass.name()); + String[] objectClasses = value.toString().split("\n"); + for (String objectClass : objectClasses) { + if (objectClass.trim().equals("")) + continue; + attribute.add(objectClass); + } + Attribute previousAttribute = getModifiedAttributes().put(attribute); + if (previousAttribute != null) + return previousAttribute.get(); + else + return null; + } - if (!(value instanceof String || value instanceof byte[])) - throw new IllegalArgumentException("Value must be String or byte[]"); + if (!(value instanceof String || value instanceof byte[])) + throw new IllegalArgumentException("Value must be String or byte[]"); - if (includeFilter && !attrFilter.contains(key)) - throw new IllegalArgumentException("Key " + key + " not included"); - else if (!includeFilter && attrFilter.contains(key)) - throw new IllegalArgumentException("Key " + key + " excluded"); + if (includeFilter && !attrFilter.contains(key)) + throw new IllegalArgumentException("Key " + key + " not included"); + else if (!includeFilter && attrFilter.contains(key)) + throw new IllegalArgumentException("Key " + key + " excluded"); - try { Attribute attribute = getModifiedAttributes().get(key.toString()); // if (attribute == null) // block unit tests attribute = new BasicAttribute(key.toString());