X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2Fdirectory%2Fldap%2FDefaultLdapEntry.java;h=c4d20feba922576dc181129ebcd75dae523fbde5;hb=15f93a026914b55e52f147e6aa53392434652522;hp=8eff6690003c014a70b947fb1d19b8e42b71ca53;hpb=0ce8ecfe974cec9f524c16884209cd08544d890d;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 8eff66900..c4d20feba 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 @@ -25,7 +25,6 @@ import javax.naming.ldap.LdapName; import org.argeo.util.directory.DirectoryDigestUtils; import org.argeo.util.naming.LdapAttrs; import org.argeo.util.naming.LdapObjs; -import org.argeo.util.naming.SharedSecret; /** An entry in an LDAP (or LDIF) directory. */ public class DefaultLdapEntry implements LdapEntry { @@ -33,20 +32,20 @@ public class DefaultLdapEntry implements LdapEntry { private final LdapName dn; - private Attributes publishedAttributes; +// private Attributes publishedAttributes; // Temporarily expose the fields - protected final AttributeDictionary properties; - protected final AttributeDictionary credentials; + protected AttributeDictionary properties; + protected AttributeDictionary credentials; - 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); +// this.publishedAttributes = attributes; +// properties = new AttributeDictionary(false); +// credentials = new AttributeDictionary(true); } @Override @@ -55,7 +54,10 @@ public class DefaultLdapEntry implements LdapEntry { } public synchronized Attributes getAttributes() { - return isEditing() ? getModifiedAttributes() : publishedAttributes; +// // lazy loading +// if (publishedAttributes == null) +// publishedAttributes = getDirectory().getDirectoryDao().doGetAttributes(dn); + return isEditing() ? getModifiedAttributes() : getDirectory().getDirectoryDao().doGetAttributes(dn); } @Override @@ -102,17 +104,25 @@ public class DefaultLdapEntry implements LdapEntry { } public synchronized void publishAttributes(Attributes modifiedAttributes) { - publishedAttributes = modifiedAttributes; +// publishedAttributes = modifiedAttributes; } - + /* * PROPERTIES */ @Override public Dictionary getProperties() { + if (properties == null) + properties = new AttributeDictionary(false); return properties; } + public Dictionary getCredentials() { + if (credentials == null) + credentials = new AttributeDictionary(true); + return credentials; + } + /* * CREDENTIALS */ @@ -154,7 +164,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[])) @@ -364,29 +374,47 @@ 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); - } + 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());