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());
}
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);
}
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);
}
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);
}
}
Aggregations