use of org.apache.syncope.common.lib.patch.MembershipPatch in project syncope by apache.
the class AnyOperations method diff.
/**
* Calculate modifications needed by first in order to be equal to second.
*
* @param updated updated UserTO
* @param original original UserTO
* @param incremental perform incremental diff (without removing existing info)
* @return UserPatch containing differences
*/
public static UserPatch diff(final UserTO updated, final UserTO original, final boolean incremental) {
UserPatch result = new UserPatch();
diff(updated, original, result, incremental);
// 1. password
if (updated.getPassword() != null && (original.getPassword() == null || !original.getPassword().equals(updated.getPassword()))) {
result.setPassword(new PasswordPatch.Builder().value(updated.getPassword()).resources(updated.getResources()).build());
}
// 2. username
result.setUsername(replacePatchItem(updated.getUsername(), original.getUsername(), new StringReplacePatchItem()));
// 3. security question / answer
if (updated.getSecurityQuestion() == null) {
result.setSecurityQuestion(null);
result.setSecurityAnswer(null);
} else if (!updated.getSecurityQuestion().equals(original.getSecurityQuestion()) || StringUtils.isNotBlank(updated.getSecurityAnswer())) {
result.setSecurityQuestion(new StringReplacePatchItem.Builder().value(updated.getSecurityQuestion()).build());
result.setSecurityAnswer(new StringReplacePatchItem.Builder().value(updated.getSecurityAnswer()).build());
}
result.setMustChangePassword(replacePatchItem(updated.isMustChangePassword(), original.isMustChangePassword(), new BooleanReplacePatchItem()));
// 4. roles
if (!incremental) {
original.getRoles().stream().filter(role -> !updated.getRoles().contains(role)).forEach(toRemove -> {
result.getRoles().add(new StringPatchItem.Builder().operation(PatchOperation.DELETE).value(toRemove).build());
});
}
updated.getRoles().stream().filter(role -> !original.getRoles().contains(role)).forEach(toAdd -> {
result.getRoles().add(new StringPatchItem.Builder().operation(PatchOperation.ADD_REPLACE).value(toAdd).build());
});
// 5. relationships
Map<Pair<String, String>, RelationshipTO> updatedRels = EntityTOUtils.buildRelationshipMap(updated.getRelationships());
Map<Pair<String, String>, RelationshipTO> originalRels = EntityTOUtils.buildRelationshipMap(original.getRelationships());
updatedRels.entrySet().stream().filter(entry -> (!originalRels.containsKey(entry.getKey()))).forEachOrdered(entry -> {
result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.ADD_REPLACE).relationshipTO(entry.getValue()).build());
});
if (!incremental) {
originalRels.keySet().stream().filter(relationship -> !updatedRels.containsKey(relationship)).forEach(key -> {
result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.DELETE).relationshipTO(originalRels.get(key)).build());
});
}
// 6. memberships
Map<String, MembershipTO> updatedMembs = EntityTOUtils.buildMembershipMap(updated.getMemberships());
Map<String, MembershipTO> originalMembs = EntityTOUtils.buildMembershipMap(original.getMemberships());
updatedMembs.entrySet().stream().map(entry -> {
MembershipPatch membershipPatch = new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(entry.getValue().getGroupKey()).build();
MembershipTO omemb;
if (originalMembs.containsKey(entry.getKey())) {
// get the original membership
omemb = originalMembs.get(entry.getKey());
} else {
// create an empty one to generate the patch
omemb = new MembershipTO.Builder().group(entry.getKey()).build();
}
diff(entry.getValue(), omemb, membershipPatch, incremental);
return membershipPatch;
}).forEachOrdered(membershipPatch -> {
result.getMemberships().add(membershipPatch);
});
if (!incremental) {
originalMembs.keySet().stream().filter(membership -> !updatedMembs.containsKey(membership)).forEach(key -> {
result.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(originalMembs.get(key).getGroupKey()).build());
});
}
return result;
}
use of org.apache.syncope.common.lib.patch.MembershipPatch in project syncope by apache.
the class MembershipITCase method misc.
@Test
public void misc() {
UserTO user = UserITCase.getUniqueSampleTO("memb@apache.org");
user.setRealm("/even/two");
user.getPlainAttrs().add(new AttrTO.Builder().schema("aLong").value("1976").build());
user.getPlainAttrs().remove(user.getPlainAttr("ctype").get());
// the group 034740a9-fa10-453b-af37-dc7897e98fb1 has USER type extensions for 'csv' and 'other'
// any type classes
MembershipTO membership = new MembershipTO.Builder().group("034740a9-fa10-453b-af37-dc7897e98fb1").build();
membership.getPlainAttrs().add(new AttrTO.Builder().schema("aLong").value("1977").build());
// 'fullname' is in 'minimal user', so it is not allowed for this membership
membership.getPlainAttrs().add(new AttrTO.Builder().schema("fullname").value("discarded").build());
user.getMemberships().add(membership);
// user creation fails because of fullname
try {
createUser(user);
fail("This should not happen");
} catch (SyncopeClientException e) {
assertEquals(ClientExceptionType.InvalidUser, e.getType());
assertTrue(e.getMessage().contains("InvalidPlainAttr: fullname not allowed for membership of group"));
}
// remove fullname and try again
membership.getPlainAttrs().remove(membership.getPlainAttr("fullname").get());
try {
user = createUser(user).getEntity();
// 1. verify that 'aLong' is correctly populated for user
assertEquals(1, user.getPlainAttr("aLong").get().getValues().size());
assertEquals("1976", user.getPlainAttr("aLong").get().getValues().get(0));
// 2. verify that 'aLong' is correctly populated for user's membership
assertEquals(1, user.getMemberships().size());
membership = user.getMembership("034740a9-fa10-453b-af37-dc7897e98fb1").get();
assertNotNull(membership);
assertEquals(1, membership.getPlainAttr("aLong").get().getValues().size());
assertEquals("1977", membership.getPlainAttr("aLong").get().getValues().get(0));
// 3. verify that derived attrbutes from 'csv' and 'other' are also populated for user's membership
assertFalse(membership.getDerAttr("csvuserid").get().getValues().isEmpty());
assertFalse(membership.getDerAttr("noschema").get().getValues().isEmpty());
// update user - change some values and add new membership attribute
UserPatch userPatch = new UserPatch();
userPatch.setKey(user.getKey());
userPatch.getPlainAttrs().add(new AttrPatch.Builder().attrTO(new AttrTO.Builder().schema("aLong").value("1977").build()).build());
MembershipPatch membershipPatch = new MembershipPatch.Builder().group(membership.getGroupKey()).build();
membershipPatch.getPlainAttrs().add(new AttrTO.Builder().schema("aLong").value("1976").build());
membershipPatch.getPlainAttrs().add(new AttrTO.Builder().schema("ctype").value("membership type").build());
userPatch.getMemberships().add(membershipPatch);
user = updateUser(userPatch).getEntity();
// 4. verify that 'aLong' is correctly populated for user
assertEquals(1, user.getPlainAttr("aLong").get().getValues().size());
assertEquals("1977", user.getPlainAttr("aLong").get().getValues().get(0));
assertFalse(user.getPlainAttr("ctype").isPresent());
// 5. verify that 'aLong' is correctly populated for user's membership
assertEquals(1, user.getMemberships().size());
membership = user.getMembership("034740a9-fa10-453b-af37-dc7897e98fb1").get();
assertNotNull(membership);
assertEquals(1, membership.getPlainAttr("aLong").get().getValues().size());
assertEquals("1976", membership.getPlainAttr("aLong").get().getValues().get(0));
// 6. verify that 'ctype' is correctly populated for user's membership
assertEquals("membership type", membership.getPlainAttr("ctype").get().getValues().get(0));
// finally remove membership
userPatch = new UserPatch();
userPatch.setKey(user.getKey());
membershipPatch = new MembershipPatch.Builder().group(membership.getGroupKey()).operation(PatchOperation.DELETE).build();
userPatch.getMemberships().add(membershipPatch);
user = updateUser(userPatch).getEntity();
assertTrue(user.getMemberships().isEmpty());
} finally {
if (user.getKey() != null) {
userService.delete(user.getKey());
}
}
}
Aggregations