Search in sources :

Example 16 with Modification

use of com.unboundid.ldap.sdk.Modification in project cas by apereo.

the class BaseLdapConsentRepositoryTests method verifyAllConsentDecisionsAreFoundForAllUsers.

@Test
public void verifyAllConsentDecisionsAreFoundForAllUsers() throws Exception {
    final ConsentDecision decision = BUILDER.build(SVC, REG_SVC, USER_CN, ATTR);
    decision.setId(1);
    final Modification mod = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER_DN, mod).getResultCode());
    final ConsentDecision decision2 = BUILDER.build(SVC, REG_SVC, USER2_CN, ATTR);
    decision2.setId(2);
    final Modification mod2 = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision2));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER2_DN, mod2).getResultCode());
    final Collection<ConsentDecision> d = this.repository.findConsentDecisions();
    assertNotNull(d);
    assertFalse(d.isEmpty());
    assertEquals(2, d.size());
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 17 with Modification

use of com.unboundid.ldap.sdk.Modification in project cas by apereo.

the class BaseLdapConsentRepositoryTests method verifyConsentDecisionIsNotMistaken.

@Test
public void verifyConsentDecisionIsNotMistaken() throws Exception {
    final ConsentDecision decision = BUILDER.build(SVC, REG_SVC, USER_CN, ATTR);
    decision.setId(1);
    final Modification mod = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER_DN, mod).getResultCode());
    final ConsentDecision d = this.repository.findConsentDecision(SVC, REG_SVC, CoreAuthenticationTestUtils.getAuthentication("unknownUser"));
    assertNull(d);
    final ConsentDecision d2 = this.repository.findConsentDecision(RegisteredServiceTestUtils.getService2(), REG_SVC, CoreAuthenticationTestUtils.getAuthentication(USER_CN));
    assertNull(d2);
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with Modification

use of com.unboundid.ldap.sdk.Modification in project cas by apereo.

the class BaseLdapConsentRepositoryTests method verifyConsentDecisionIsUpdated.

@Test
public void verifyConsentDecisionIsUpdated() throws Exception {
    final ConsentDecision decision = BUILDER.build(SVC, REG_SVC, USER_CN, ATTR);
    decision.setId(1);
    final Modification mod = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER_DN, mod).getResultCode());
    final LocalDateTime t = LocalDateTime.now();
    assertNotEquals(t, decision.getCreatedDate());
    decision.setCreatedDate(t);
    this.repository.storeConsentDecision(decision);
    final SearchResult r2 = getConnection().search(USER_DN, SearchScope.SUB, DEF_FILTER, ATTR_NAME);
    assertTrue(r2.getEntryCount() > 0);
    final ConsentDecision d = MAPPER.readValue(r2.getSearchEntry(USER_DN).getAttributeValue(ATTR_NAME), ConsentDecision.class);
    assertNotNull(d);
    assertEquals(d.getId(), decision.getId());
    assertEquals(d.getCreatedDate(), t);
}
Also used : LocalDateTime(java.time.LocalDateTime) Modification(com.unboundid.ldap.sdk.Modification) SearchResult(com.unboundid.ldap.sdk.SearchResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with Modification

use of com.unboundid.ldap.sdk.Modification in project oxCore by GluuFederation.

the class LdapEntryManager method merge.

@Override
public void merge(String dn, List<AttributeDataModification> attributeDataModifications) {
    // Update entry
    try {
        List<Modification> modifications = new ArrayList<Modification>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            String attributeName = null;
            String[] attributeValues = null;
            if (attribute != null) {
                attributeName = attribute.getName();
                attributeValues = attribute.getValues();
            }
            String oldAttributeName = null;
            String[] oldAttributeValues = null;
            if (oldAttribute != null) {
                oldAttributeName = oldAttribute.getName();
                oldAttributeValues = oldAttribute.getValues();
            }
            Modification modification = null;
            if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
                modification = createModification(ModificationType.ADD, attributeName, attributeValues);
            } else {
                if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
                    modification = createModification(ModificationType.DELETE, oldAttributeName, oldAttributeValues);
                } else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
                    if (attributeValues.length == 1) {
                        modification = createModification(ModificationType.REPLACE, attributeName, attributeValues);
                    } else {
                        String[] oldValues = ArrayHelper.arrayClone(oldAttributeValues);
                        String[] newValues = ArrayHelper.arrayClone(attributeValues);
                        Arrays.sort(oldValues);
                        Arrays.sort(newValues);
                        boolean[] retainOldValues = new boolean[oldValues.length];
                        Arrays.fill(retainOldValues, false);
                        List<String> addValues = new ArrayList<String>();
                        List<String> removeValues = new ArrayList<String>();
                        // Add new values
                        for (String value : newValues) {
                            int idx = Arrays.binarySearch(oldValues, value, new Comparator<String>() {

                                @Override
                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx >= 0) {
                                // Old values array contains new value. Retain
                                // old value
                                retainOldValues[idx] = true;
                            } else {
                                // This is new value
                                addValues.add(value);
                            }
                        }
                        // Remove values which we don't have in new values
                        for (int i = 0; i < oldValues.length; i++) {
                            if (!retainOldValues[i]) {
                                removeValues.add(oldValues[i]);
                            }
                        }
                        if (removeValues.size() > 0) {
                            Modification removeModification = createModification(ModificationType.DELETE, attributeName, removeValues.toArray(new String[removeValues.size()]));
                            modifications.add(removeModification);
                        }
                        if (addValues.size() > 0) {
                            Modification addModification = createModification(ModificationType.ADD, attributeName, addValues.toArray(new String[addValues.size()]));
                            modifications.add(addModification);
                        }
                    }
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = this.ldapOperationService.updateEntry(dn, modifications);
            if (!result) {
                throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn));
            }
        }
    } catch (ConnectionException ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex.getCause());
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex);
    }
}
Also used : AttributeDataModification(org.gluu.persist.model.AttributeDataModification) Modification(com.unboundid.ldap.sdk.Modification) ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) Comparator(java.util.Comparator) AttributeDataModification(org.gluu.persist.model.AttributeDataModification) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData) ConnectionException(org.gluu.persist.exception.operation.ConnectionException)

Aggregations

Modification (com.unboundid.ldap.sdk.Modification)19 Test (org.junit.Test)12 LdapKeyManager (com.gitblit.transport.ssh.LdapKeyManager)6 SshKey (com.gitblit.transport.ssh.SshKey)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5 SearchResult (com.unboundid.ldap.sdk.SearchResult)3 ArrayList (java.util.ArrayList)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1 Signature (java.security.Signature)1 ParseException (java.text.ParseException)1 LocalDateTime (java.time.LocalDateTime)1 Comparator (java.util.Comparator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)1 MappingException (org.gluu.persist.exception.mapping.MappingException)1 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)1 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)1