@Test public void testCreateEntry() throws Exception { if (USE_EXTERNAL_TEST_LDAP_SERVER) { Session session = getLDAPDirectory("userDirectory").getSession(); try { assertNotNull(session); Map<String, Object> map = new HashMap<String, Object>(); map.put("username", "user0"); map.put("password", "pass0"); map.put("firstName", "User"); map.put("lastName", ""); map.put("intField", Long.valueOf(0)); map.put("dn", "cn=this,ou=is,ou=a,ou=fake,o=dn"); map.put("email", "nobody@nowhere.com"); map.put("employeeType", Arrays.asList("item1", "item2")); if (USE_EXTERNAL_TEST_LDAP_SERVER) { map.put("groups", Arrays.asList("members", "administrators")); } DocumentModel dm = session.createEntry(map); dm = session.getEntry("user0"); assertNotNull(dm); String id = dm.getId(); assertNotNull(id); String[] schemaNames = dm.getSchemas(); assertEquals(1, schemaNames.length); assertEquals(USER_SCHEMANAME, schemaNames[0]); assertEquals("user0", dm.getProperty(USER_SCHEMANAME, "username")); assertEquals("User", dm.getProperty(USER_SCHEMANAME, "firstName")); assertEquals("", dm.getProperty(USER_SCHEMANAME, "lastName")); assertEquals(Long.valueOf(0), dm.getProperty(USER_SCHEMANAME, "intField")); assertEquals("uid=user0,ou=people,dc=example,dc=com", dm.getProperty(USER_SCHEMANAME, "dn")); assertEquals("nobody@nowhere.com", dm.getProperty(USER_SCHEMANAME, "email")); assertEquals(Arrays.asList("item1", "item2"), dm.getProperty(USER_SCHEMANAME, "employeeType")); assertEquals(Arrays.asList("administrators", "members"), dm.getProperty(USER_SCHEMANAME, "groups")); assertTrue(session.authenticate("user0", "pass0")); } finally { session.close(); } session = getLDAPDirectory("groupDirectory").getSession(); try { DocumentModel entry = session.getEntry("administrators"); assertNotNull(entry); assertEquals(Arrays.asList("Administrator", "user0"), entry.getProperty(GROUP_SCHEMANAME, "members")); entry = session.getEntry("members"); assertNotNull(entry); assertEquals(Arrays.asList("Administrator", "user0", "user1", "user2"), entry.getProperty(GROUP_SCHEMANAME, "members")); } finally { session.close(); } } }
@Override @SuppressWarnings("unchecked") public DocumentModel createEntry(Map<String, Object> fieldMap) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return null; } if (isReadOnly()) { return null; } List<String> referenceFieldList = new LinkedList<String>(); try { String dn = String.format("%s=%s,%s", rdnAttribute, fieldMap.get(rdnField), directory.getConfig().getCreationBaseDn()); Attributes attrs = new BasicAttributes(); Attribute attr; List<String> mandatoryAttributes = getMandatoryAttributes(); for (String mandatoryAttribute : mandatoryAttributes) { attr = new BasicAttribute(mandatoryAttribute); attr.add(" "); attrs.put(attr); } String[] creationClasses = directory.getConfig().getCreationClasses(); if (creationClasses.length != 0) { attr = new BasicAttribute("objectclass"); for (String creationClasse : creationClasses) { attr.add(creationClasse); } attrs.put(attr); } for (String fieldId : fieldMap.keySet()) { String backendFieldId = directory.getFieldMapper().getBackendField( fieldId); if (backendFieldId.equals(getPasswordField())) { attr = new BasicAttribute(backendFieldId); String password = (String) fieldMap.get(fieldId); password = PasswordHelper.hashPassword(password, passwordHashAlgorithm); attr.add(password); attrs.put(attr); } else if (directory.isReference(fieldId)) { Reference reference = directory.getReference(fieldId); if (reference instanceof LDAPReference) { attr = new BasicAttribute( ((LDAPReference) reference).getStaticAttributeId()); attr.add(directory.getConfig().getEmptyRefMarker()); attrs.put(attr); } referenceFieldList.add(fieldId); } else if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendFieldId)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", fieldId)); } else { Object value = fieldMap.get(fieldId); if ((value != null) && !value.equals("") && !Collections.emptyList().equals(value)) { attrs.put(getAttributeValue(fieldId, value)); } } } if (log.isDebugEnabled()) { String idField = directory.getConfig().getIdField(); log.debug(String.format( "LDAPSession.createEntry(%s=%s): LDAP bind dn='%s' attrs='%s' [%s]", idField, fieldMap.get(idField), dn, attrs, this)); } dirContext.bind(dn, null, attrs); for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) fieldMap.get(referenceFieldName); reference.addLinks((String) fieldMap.get(getIdField()), targetIds); } String dnFieldName = directory.getFieldMapper().getDirectoryField( LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY); if (directory.getSchemaFieldMap().containsKey(dnFieldName)) { fieldMap.put(dnFieldName, dn); } directory.invalidateCaches(); return fieldMapToDocumentModel(fieldMap); } catch (Exception e) { handleException(e, "createEntry failed"); return null; } }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public DocumentModel createEntry(Map<String, Object> fieldMap) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return null; } if (isReadOnly()) { return null; } List<String> referenceFieldList = new LinkedList<String>(); try { String dn = String.format("%s=%s,%s", rdnAttribute, fieldMap.get(rdnField), directory.getConfig().getCreationBaseDn()); Attributes attrs = new BasicAttributes(); Attribute attr; List<String> mandatoryAttributes = getMandatoryAttributes(); for (String mandatoryAttribute : mandatoryAttributes) { attr = new BasicAttribute(mandatoryAttribute); attr.add(" "); attrs.put(attr); } String[] creationClasses = directory.getConfig().getCreationClasses(); if (creationClasses.length != 0) { attr = new BasicAttribute("objectclass"); for (String creationClasse : creationClasses) { attr.add(creationClasse); } attrs.put(attr); } for (String fieldId : fieldMap.keySet()) { String backendFieldId = directory.getFieldMapper().getBackendField( fieldId); if (backendFieldId.equals(getPasswordField())) { attr = new BasicAttribute(backendFieldId); String password = (String) fieldMap.get(fieldId); password = PasswordHelper.hashPassword(password, passwordHashAlgorithm); attr.add(password); attrs.put(attr); } else if (directory.isReference(fieldId)) { Reference reference = directory.getReference(fieldId); if (reference instanceof LDAPReference) { attr = new BasicAttribute( ((LDAPReference) reference).getStaticAttributeId()); attr.add(directory.getConfig().getEmptyRefMarker()); attrs.put(attr); } referenceFieldList.add(fieldId); } else if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendFieldId)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", fieldId)); } else { Object value = fieldMap.get(fieldId); if ((value != null) && !value.equals("") && !Collections.emptyList().equals(value)) { attrs.put(getAttributeValue(fieldId, value)); } } } if (log.isDebugEnabled()) { String idField = directory.getConfig().getIdField(); log.debug(String.format( "LDAPSession.createEntry(%s=%s): LDAP bind dn='%s' attrs='%s' [%s]", idField, fieldMap.get(idField), dn, attrs, this)); } dirContext.bind(dn, null, attrs); for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) fieldMap.get(referenceFieldName); reference.addLinks((String) fieldMap.get(getIdField()), targetIds); } String dnFieldName = directory.getFieldMapper().getDirectoryField( LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY); if (directory.getSchemaFieldMap().containsKey(dnFieldName)) { fieldMap.put(dnFieldName, dn); } directory.invalidateCaches(); return fieldMapToDocumentModel(fieldMap); } catch (Exception e) { handleException(e, "createEntry failed"); return null; } } }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public DocumentModel createEntry(Map<String, Object> fieldMap) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return null; } if (isReadOnly()) { return null; } List<String> referenceFieldList = new LinkedList<String>(); try { String dn = String.format("%s=%s,%s", rdnAttribute, fieldMap.get(rdnField), directory.getConfig().getCreationBaseDn()); Attributes attrs = new BasicAttributes(); Attribute attr; List<String> mandatoryAttributes = getMandatoryAttributes(); for (String mandatoryAttribute : mandatoryAttributes) { attr = new BasicAttribute(mandatoryAttribute); attr.add(" "); attrs.put(attr); } String[] creationClasses = directory.getConfig().getCreationClasses(); if (creationClasses.length != 0) { attr = new BasicAttribute("objectclass"); for (String creationClasse : creationClasses) { attr.add(creationClasse); } attrs.put(attr); } for (String fieldId : fieldMap.keySet()) { String backendFieldId = directory.getFieldMapper().getBackendField( fieldId); if (backendFieldId.equals(getPasswordField())) { attr = new BasicAttribute(backendFieldId); String password = (String) fieldMap.get(fieldId); password = PasswordHelper.hashPassword(password, passwordHashAlgorithm); attr.add(password); attrs.put(attr); } else if (directory.isReference(fieldId)) { Reference reference = directory.getReference(fieldId); if (reference instanceof LDAPReference) { attr = new BasicAttribute( ((LDAPReference) reference).getStaticAttributeId()); attr.add(directory.getConfig().getEmptyRefMarker()); attrs.put(attr); } referenceFieldList.add(fieldId); } else if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendFieldId)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", fieldId)); } else { Object value = fieldMap.get(fieldId); if ((value != null) && !value.equals("") && !Collections.emptyList().equals(value)) { attrs.put(getAttributeValue(fieldId, value)); } } } if (log.isDebugEnabled()) { String idField = directory.getConfig().getIdField(); log.debug(String.format( "LDAPSession.createEntry(%s=%s): LDAP bind dn='%s' attrs='%s' [%s]", idField, fieldMap.get(idField), dn, attrs, this)); } dirContext.bind(dn, null, attrs); for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) fieldMap.get(referenceFieldName); reference.addLinks((String) fieldMap.get(getIdField()), targetIds); } String dnFieldName = directory.getFieldMapper().getDirectoryField( LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY); if (directory.getSchemaFieldMap().containsKey(dnFieldName)) { fieldMap.put(dnFieldName, dn); } directory.invalidateCaches(); return fieldMapToDocumentModel(fieldMap); } catch (Exception e) { handleException(e, "createEntry failed"); return null; } } LDAPSession(LDAPDirectory directory, DirContext dirContext); }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public DocumentModel createEntry(Map<String, Object> fieldMap) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return null; } if (isReadOnly()) { return null; } List<String> referenceFieldList = new LinkedList<String>(); try { String dn = String.format("%s=%s,%s", rdnAttribute, fieldMap.get(rdnField), directory.getConfig().getCreationBaseDn()); Attributes attrs = new BasicAttributes(); Attribute attr; List<String> mandatoryAttributes = getMandatoryAttributes(); for (String mandatoryAttribute : mandatoryAttributes) { attr = new BasicAttribute(mandatoryAttribute); attr.add(" "); attrs.put(attr); } String[] creationClasses = directory.getConfig().getCreationClasses(); if (creationClasses.length != 0) { attr = new BasicAttribute("objectclass"); for (String creationClasse : creationClasses) { attr.add(creationClasse); } attrs.put(attr); } for (String fieldId : fieldMap.keySet()) { String backendFieldId = directory.getFieldMapper().getBackendField( fieldId); if (backendFieldId.equals(getPasswordField())) { attr = new BasicAttribute(backendFieldId); String password = (String) fieldMap.get(fieldId); password = PasswordHelper.hashPassword(password, passwordHashAlgorithm); attr.add(password); attrs.put(attr); } else if (directory.isReference(fieldId)) { Reference reference = directory.getReference(fieldId); if (reference instanceof LDAPReference) { attr = new BasicAttribute( ((LDAPReference) reference).getStaticAttributeId()); attr.add(directory.getConfig().getEmptyRefMarker()); attrs.put(attr); } referenceFieldList.add(fieldId); } else if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendFieldId)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", fieldId)); } else { Object value = fieldMap.get(fieldId); if ((value != null) && !value.equals("") && !Collections.emptyList().equals(value)) { attrs.put(getAttributeValue(fieldId, value)); } } } if (log.isDebugEnabled()) { String idField = directory.getConfig().getIdField(); log.debug(String.format( "LDAPSession.createEntry(%s=%s): LDAP bind dn='%s' attrs='%s' [%s]", idField, fieldMap.get(idField), dn, attrs, this)); } dirContext.bind(dn, null, attrs); for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) fieldMap.get(referenceFieldName); reference.addLinks((String) fieldMap.get(getIdField()), targetIds); } String dnFieldName = directory.getFieldMapper().getDirectoryField( LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY); if (directory.getSchemaFieldMap().containsKey(dnFieldName)) { fieldMap.put(dnFieldName, dn); } directory.invalidateCaches(); return fieldMapToDocumentModel(fieldMap); } catch (Exception e) { handleException(e, "createEntry failed"); return null; } } LDAPSession(LDAPDirectory directory, DirContext dirContext); void setSubStringMatchType(String type); Directory getDirectory(); DirContext getContext(); @Override @SuppressWarnings("unchecked") DocumentModel createEntry(Map<String, Object> fieldMap); @Override DocumentModel getEntry(String id); @Override DocumentModel getEntry(String id, boolean fetchReferences); @Override DocumentModel getEntryFromSource(String id, boolean fetchReferences); @Override boolean hasEntry(String id); @Override DocumentModelList getEntries(); @Override @SuppressWarnings("unchecked") void updateEntry(DocumentModel docModel); @Override void deleteEntry(DocumentModel dm); @Override void deleteEntry(String id); @Override void deleteEntry(String id, Map<String, String> map); DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, boolean fetchReferences,
            Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy,
            boolean fetchReferences); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext); @Override void close(); @Override List<String> getProjection(Map<String, Serializable> filter,
            String columnName); @Override List<String> getProjection(Map<String, Serializable> filter,
            Set<String> fulltext, String columnName); @Override boolean authenticate(String username, String password); @Override String getIdField(); @Override String getPasswordField(); @Override boolean isAuthenticating(); @Override boolean isReadOnly(); boolean rdnMatchesIdField(); @Override // useful for the log function String toString(); @Override DocumentModel createEntry(DocumentModel entry); }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public DocumentModel createEntry(Map<String, Object> fieldMap) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return null; } if (isReadOnly()) { return null; } List<String> referenceFieldList = new LinkedList<String>(); try { String dn = String.format("%s=%s,%s", rdnAttribute, fieldMap.get(rdnField), directory.getConfig().getCreationBaseDn()); Attributes attrs = new BasicAttributes(); Attribute attr; List<String> mandatoryAttributes = getMandatoryAttributes(); for (String mandatoryAttribute : mandatoryAttributes) { attr = new BasicAttribute(mandatoryAttribute); attr.add(" "); attrs.put(attr); } String[] creationClasses = directory.getConfig().getCreationClasses(); if (creationClasses.length != 0) { attr = new BasicAttribute("objectclass"); for (String creationClasse : creationClasses) { attr.add(creationClasse); } attrs.put(attr); } for (String fieldId : fieldMap.keySet()) { String backendFieldId = directory.getFieldMapper().getBackendField( fieldId); if (backendFieldId.equals(getPasswordField())) { attr = new BasicAttribute(backendFieldId); String password = (String) fieldMap.get(fieldId); password = PasswordHelper.hashPassword(password, passwordHashAlgorithm); attr.add(password); attrs.put(attr); } else if (directory.isReference(fieldId)) { Reference reference = directory.getReference(fieldId); if (reference instanceof LDAPReference) { attr = new BasicAttribute( ((LDAPReference) reference).getStaticAttributeId()); attr.add(directory.getConfig().getEmptyRefMarker()); attrs.put(attr); } referenceFieldList.add(fieldId); } else if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendFieldId)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", fieldId)); } else { Object value = fieldMap.get(fieldId); if ((value != null) && !value.equals("") && !Collections.emptyList().equals(value)) { attrs.put(getAttributeValue(fieldId, value)); } } } if (log.isDebugEnabled()) { String idField = directory.getConfig().getIdField(); log.debug(String.format( "LDAPSession.createEntry(%s=%s): LDAP bind dn='%s' attrs='%s' [%s]", idField, fieldMap.get(idField), dn, attrs, this)); } dirContext.bind(dn, null, attrs); for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) fieldMap.get(referenceFieldName); reference.addLinks((String) fieldMap.get(getIdField()), targetIds); } String dnFieldName = directory.getFieldMapper().getDirectoryField( LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY); if (directory.getSchemaFieldMap().containsKey(dnFieldName)) { fieldMap.put(dnFieldName, dn); } directory.invalidateCaches(); return fieldMapToDocumentModel(fieldMap); } catch (Exception e) { handleException(e, "createEntry failed"); return null; } } LDAPSession(LDAPDirectory directory, DirContext dirContext); void setSubStringMatchType(String type); Directory getDirectory(); DirContext getContext(); @Override @SuppressWarnings("unchecked") DocumentModel createEntry(Map<String, Object> fieldMap); @Override DocumentModel getEntry(String id); @Override DocumentModel getEntry(String id, boolean fetchReferences); @Override DocumentModel getEntryFromSource(String id, boolean fetchReferences); @Override boolean hasEntry(String id); @Override DocumentModelList getEntries(); @Override @SuppressWarnings("unchecked") void updateEntry(DocumentModel docModel); @Override void deleteEntry(DocumentModel dm); @Override void deleteEntry(String id); @Override void deleteEntry(String id, Map<String, String> map); DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, boolean fetchReferences,
            Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy,
            boolean fetchReferences); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext); @Override void close(); @Override List<String> getProjection(Map<String, Serializable> filter,
            String columnName); @Override List<String> getProjection(Map<String, Serializable> filter,
            Set<String> fulltext, String columnName); @Override boolean authenticate(String username, String password); @Override String getIdField(); @Override String getPasswordField(); @Override boolean isAuthenticating(); @Override boolean isReadOnly(); boolean rdnMatchesIdField(); @Override // useful for the log function String toString(); @Override DocumentModel createEntry(DocumentModel entry);  }