@Test public void testUpdateEntry() throws Exception { if (USE_EXTERNAL_TEST_LDAP_SERVER) { Session session = getLDAPDirectory("userDirectory").getSession(); Session groupSession = getLDAPDirectory("groupDirectory").getSession(); try { DocumentModel entry = session.getEntry("user1"); assertNotNull(entry); assertFalse(BaseSession.isReadOnlyEntry(entry)); entry.setProperty(USER_SCHEMANAME, "firstName", "toto"); entry.setProperty(USER_SCHEMANAME, "lastName", ""); entry.setProperty(USER_SCHEMANAME, "password", "toto"); entry.setProperty(USER_SCHEMANAME, "intField", Long.valueOf(123)); entry.setProperty(USER_SCHEMANAME, "dn", "cn=this,ou=is,ou=a,ou=fake,o=dn"); entry.setProperty(USER_SCHEMANAME, "employeeType", Arrays.asList("item3", "item4")); List<String> groups = Arrays.asList("administrators", "members"); entry.setProperty(USER_SCHEMANAME, "groups", groups); session.updateEntry(entry); entry = session.getEntry("user1"); assertNotNull(entry); assertEquals("toto", entry.getProperty(USER_SCHEMANAME, "firstName")); assertEquals("", entry.getProperty(USER_SCHEMANAME, "lastName")); assertEquals(Long.valueOf(123), entry.getProperty(USER_SCHEMANAME, "intField")); assertEquals(Arrays.asList("item3", "item4"), entry.getProperty(USER_SCHEMANAME, "employeeType")); if (HAS_DYNGROUP_SCHEMA) { assertEquals(Arrays.asList("administrators", "dyngroup1", "dyngroup2", "dyngroup3", "members"), entry.getProperty(USER_SCHEMANAME, "groups")); } else { assertEquals(Arrays.asList("administrators", "members"), entry.getProperty(USER_SCHEMANAME, "groups")); } entry = groupSession.getEntry("administrators"); assertNotNull(entry); assertEquals(Arrays.asList("Administrator", "user1"), entry.getProperty(GROUP_SCHEMANAME, "members")); assertEquals(Arrays.asList(), entry.getProperty(GROUP_SCHEMANAME, "subGroups")); entry = groupSession.getEntry("members"); assertNotNull(entry); assertEquals(Arrays.asList("Administrator", "user1", "user2"), entry.getProperty(GROUP_SCHEMANAME, "members")); assertEquals(Arrays.asList("submembers"), entry.getProperty(GROUP_SCHEMANAME, "subGroups")); } finally { session.close(); groupSession.close(); } session = getLDAPDirectory("groupDirectory").getSession(); try { DocumentModel entry = session.getEntry("administrators"); assertNotNull(entry); assertEquals(Arrays.asList("Administrator", "user1"), entry.getProperty(GROUP_SCHEMANAME, "members")); } finally { session.close(); } } }
@Override @SuppressWarnings("unchecked") public void updateEntry(DocumentModel docModel) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return; } if (isReadOnlyEntry(docModel)) { return; } List<String> updateList = new ArrayList<String>(); List<String> referenceFieldList = new LinkedList<String>(); try { DataModel dataModel = docModel.getDataModel(schemaName); for (String fieldName : schemaFieldMap.keySet()) { if (!dataModel.isDirty(fieldName)) { continue; } if (directory.isReference(fieldName)) { referenceFieldList.add(fieldName); } else { updateList.add(fieldName); } } if (!isReadOnlyEntry(docModel) && !updateList.isEmpty()) { Attributes attrs = new BasicAttributes(); SearchResult ldapEntry = getLdapEntry(docModel.getId()); if (ldapEntry == null) { throw new DirectoryException(docModel.getId() + " not found"); } Attributes oldattrs = ldapEntry.getAttributes(); String dn = ldapEntry.getNameInNamespace(); Attributes attrsToDel = new BasicAttributes(); for (String f : updateList) { Object value = docModel.getProperty(schemaName, f); String backendField = directory.getFieldMapper().getBackendField( f); if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendField)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", f)); continue; } if (value == null || value.equals("")) { Attribute objectClasses = oldattrs.get("objectClass"); Attribute attr; if (getMandatoryAttributes(objectClasses).contains( backendField)) { attr = new BasicAttribute(backendField); attr.add(" "); attrs.put(attr); } else if (oldattrs.get(backendField) != null) { attr = new BasicAttribute(backendField); attr.add(oldattrs.get(backendField).get()); attrsToDel.put(attr); } } else if (f.equals(getPasswordField())) { Attribute attr = new BasicAttribute(backendField); attr.add(PasswordHelper.hashPassword((String) value, passwordHashAlgorithm)); attrs.put(attr); } else { attrs.put(getAttributeValue(f, value)); } } if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REMOVE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrsToDel, this)); } dirContext.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, attrsToDel); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REPLACE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrs, this)); } dirContext.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, attrs); } for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) docModel.getProperty( schemaName, referenceFieldName); reference.setTargetIdsForSource(docModel.getId(), targetIds); } } catch (Exception e) { handleException(e, "updateEntry failed:"); } directory.invalidateCaches(); }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public void updateEntry(DocumentModel docModel) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return; } if (isReadOnlyEntry(docModel)) { return; } List<String> updateList = new ArrayList<String>(); List<String> referenceFieldList = new LinkedList<String>(); try { DataModel dataModel = docModel.getDataModel(schemaName); for (String fieldName : schemaFieldMap.keySet()) { if (!dataModel.isDirty(fieldName)) { continue; } if (directory.isReference(fieldName)) { referenceFieldList.add(fieldName); } else { updateList.add(fieldName); } } if (!isReadOnlyEntry(docModel) && !updateList.isEmpty()) { Attributes attrs = new BasicAttributes(); SearchResult ldapEntry = getLdapEntry(docModel.getId()); if (ldapEntry == null) { throw new DirectoryException(docModel.getId() + " not found"); } Attributes oldattrs = ldapEntry.getAttributes(); String dn = ldapEntry.getNameInNamespace(); Attributes attrsToDel = new BasicAttributes(); for (String f : updateList) { Object value = docModel.getProperty(schemaName, f); String backendField = directory.getFieldMapper().getBackendField( f); if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendField)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", f)); continue; } if (value == null || value.equals("")) { Attribute objectClasses = oldattrs.get("objectClass"); Attribute attr; if (getMandatoryAttributes(objectClasses).contains( backendField)) { attr = new BasicAttribute(backendField); attr.add(" "); attrs.put(attr); } else if (oldattrs.get(backendField) != null) { attr = new BasicAttribute(backendField); attr.add(oldattrs.get(backendField).get()); attrsToDel.put(attr); } } else if (f.equals(getPasswordField())) { Attribute attr = new BasicAttribute(backendField); attr.add(PasswordHelper.hashPassword((String) value, passwordHashAlgorithm)); attrs.put(attr); } else { attrs.put(getAttributeValue(f, value)); } } if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REMOVE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrsToDel, this)); } dirContext.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, attrsToDel); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REPLACE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrs, this)); } dirContext.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, attrs); } for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) docModel.getProperty( schemaName, referenceFieldName); reference.setTargetIdsForSource(docModel.getId(), targetIds); } } catch (Exception e) { handleException(e, "updateEntry failed:"); } directory.invalidateCaches(); } }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public void updateEntry(DocumentModel docModel) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return; } if (isReadOnlyEntry(docModel)) { return; } List<String> updateList = new ArrayList<String>(); List<String> referenceFieldList = new LinkedList<String>(); try { DataModel dataModel = docModel.getDataModel(schemaName); for (String fieldName : schemaFieldMap.keySet()) { if (!dataModel.isDirty(fieldName)) { continue; } if (directory.isReference(fieldName)) { referenceFieldList.add(fieldName); } else { updateList.add(fieldName); } } if (!isReadOnlyEntry(docModel) && !updateList.isEmpty()) { Attributes attrs = new BasicAttributes(); SearchResult ldapEntry = getLdapEntry(docModel.getId()); if (ldapEntry == null) { throw new DirectoryException(docModel.getId() + " not found"); } Attributes oldattrs = ldapEntry.getAttributes(); String dn = ldapEntry.getNameInNamespace(); Attributes attrsToDel = new BasicAttributes(); for (String f : updateList) { Object value = docModel.getProperty(schemaName, f); String backendField = directory.getFieldMapper().getBackendField( f); if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendField)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", f)); continue; } if (value == null || value.equals("")) { Attribute objectClasses = oldattrs.get("objectClass"); Attribute attr; if (getMandatoryAttributes(objectClasses).contains( backendField)) { attr = new BasicAttribute(backendField); attr.add(" "); attrs.put(attr); } else if (oldattrs.get(backendField) != null) { attr = new BasicAttribute(backendField); attr.add(oldattrs.get(backendField).get()); attrsToDel.put(attr); } } else if (f.equals(getPasswordField())) { Attribute attr = new BasicAttribute(backendField); attr.add(PasswordHelper.hashPassword((String) value, passwordHashAlgorithm)); attrs.put(attr); } else { attrs.put(getAttributeValue(f, value)); } } if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REMOVE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrsToDel, this)); } dirContext.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, attrsToDel); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REPLACE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrs, this)); } dirContext.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, attrs); } for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) docModel.getProperty( schemaName, referenceFieldName); reference.setTargetIdsForSource(docModel.getId(), targetIds); } } catch (Exception e) { handleException(e, "updateEntry failed:"); } directory.invalidateCaches(); } LDAPSession(LDAPDirectory directory, DirContext dirContext); }
LDAPSession extends BaseSession implements EntrySource { @Override @SuppressWarnings("unchecked") public void updateEntry(DocumentModel docModel) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return; } if (isReadOnlyEntry(docModel)) { return; } List<String> updateList = new ArrayList<String>(); List<String> referenceFieldList = new LinkedList<String>(); try { DataModel dataModel = docModel.getDataModel(schemaName); for (String fieldName : schemaFieldMap.keySet()) { if (!dataModel.isDirty(fieldName)) { continue; } if (directory.isReference(fieldName)) { referenceFieldList.add(fieldName); } else { updateList.add(fieldName); } } if (!isReadOnlyEntry(docModel) && !updateList.isEmpty()) { Attributes attrs = new BasicAttributes(); SearchResult ldapEntry = getLdapEntry(docModel.getId()); if (ldapEntry == null) { throw new DirectoryException(docModel.getId() + " not found"); } Attributes oldattrs = ldapEntry.getAttributes(); String dn = ldapEntry.getNameInNamespace(); Attributes attrsToDel = new BasicAttributes(); for (String f : updateList) { Object value = docModel.getProperty(schemaName, f); String backendField = directory.getFieldMapper().getBackendField( f); if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendField)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", f)); continue; } if (value == null || value.equals("")) { Attribute objectClasses = oldattrs.get("objectClass"); Attribute attr; if (getMandatoryAttributes(objectClasses).contains( backendField)) { attr = new BasicAttribute(backendField); attr.add(" "); attrs.put(attr); } else if (oldattrs.get(backendField) != null) { attr = new BasicAttribute(backendField); attr.add(oldattrs.get(backendField).get()); attrsToDel.put(attr); } } else if (f.equals(getPasswordField())) { Attribute attr = new BasicAttribute(backendField); attr.add(PasswordHelper.hashPassword((String) value, passwordHashAlgorithm)); attrs.put(attr); } else { attrs.put(getAttributeValue(f, value)); } } if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REMOVE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrsToDel, this)); } dirContext.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, attrsToDel); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REPLACE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrs, this)); } dirContext.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, attrs); } for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) docModel.getProperty( schemaName, referenceFieldName); reference.setTargetIdsForSource(docModel.getId(), targetIds); } } catch (Exception e) { handleException(e, "updateEntry failed:"); } directory.invalidateCaches(); } 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 void updateEntry(DocumentModel docModel) throws ClientException { if (!isCurrentUserAllowed(SecurityConstants.WRITE)) { return; } if (isReadOnlyEntry(docModel)) { return; } List<String> updateList = new ArrayList<String>(); List<String> referenceFieldList = new LinkedList<String>(); try { DataModel dataModel = docModel.getDataModel(schemaName); for (String fieldName : schemaFieldMap.keySet()) { if (!dataModel.isDirty(fieldName)) { continue; } if (directory.isReference(fieldName)) { referenceFieldList.add(fieldName); } else { updateList.add(fieldName); } } if (!isReadOnlyEntry(docModel) && !updateList.isEmpty()) { Attributes attrs = new BasicAttributes(); SearchResult ldapEntry = getLdapEntry(docModel.getId()); if (ldapEntry == null) { throw new DirectoryException(docModel.getId() + " not found"); } Attributes oldattrs = ldapEntry.getAttributes(); String dn = ldapEntry.getNameInNamespace(); Attributes attrsToDel = new BasicAttributes(); for (String f : updateList) { Object value = docModel.getProperty(schemaName, f); String backendField = directory.getFieldMapper().getBackendField( f); if (LDAPDirectory.DN_SPECIAL_ATTRIBUTE_KEY.equals(backendField)) { log.warn(String.format( "field %s is mapped to read only DN field: ignored", f)); continue; } if (value == null || value.equals("")) { Attribute objectClasses = oldattrs.get("objectClass"); Attribute attr; if (getMandatoryAttributes(objectClasses).contains( backendField)) { attr = new BasicAttribute(backendField); attr.add(" "); attrs.put(attr); } else if (oldattrs.get(backendField) != null) { attr = new BasicAttribute(backendField); attr.add(oldattrs.get(backendField).get()); attrsToDel.put(attr); } } else if (f.equals(getPasswordField())) { Attribute attr = new BasicAttribute(backendField); attr.add(PasswordHelper.hashPassword((String) value, passwordHashAlgorithm)); attrs.put(attr); } else { attrs.put(getAttributeValue(f, value)); } } if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REMOVE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrsToDel, this)); } dirContext.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, attrsToDel); if (log.isDebugEnabled()) { log.debug(String.format( "LDAPSession.updateEntry(%s): LDAP modifyAttributes dn='%s' " + "mod_op='REPLACE_ATTRIBUTE' attr='%s' [%s]", docModel, dn, attrs, this)); } dirContext.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, attrs); } for (String referenceFieldName : referenceFieldList) { Reference reference = directory.getReference(referenceFieldName); List<String> targetIds = (List<String>) docModel.getProperty( schemaName, referenceFieldName); reference.setTargetIdsForSource(docModel.getId(), targetIds); } } catch (Exception e) { handleException(e, "updateEntry failed:"); } directory.invalidateCaches(); } 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);  }