This is an automated email from the ASF dual-hosted git repository. ilgrosso pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/syncope.git
commit ae0ee6c4694d6810618d4ea67f72dece518ada5c Author: Francesco Chicchiriccò <ilgro...@apache.org> AuthorDate: Wed Dec 5 13:07:16 2018 +0100 [SYNCOPE-1408] Ensure all original, unmodified attributes are considered, during patch generation for update --- .../wizards/any/AnyObjectWizardBuilder.java | 2 +- .../console/wizards/any/AnyWizardBuilder.java | 41 +++++++++++++++++----- .../console/wizards/any/GroupWizardBuilder.java | 2 +- .../console/wizards/any/UserWizardBuilder.java | 2 +- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyObjectWizardBuilder.java b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyObjectWizardBuilder.java index 8e21f0f..779b682 100644 --- a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyObjectWizardBuilder.java +++ b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyObjectWizardBuilder.java @@ -72,7 +72,7 @@ public class AnyObjectWizardBuilder extends AnyWizardBuilder<AnyObjectTO> implem if (inner.getKey() == null) { result = anyObjectRestClient.create(inner); } else { - inner.getPlainAttrs().addAll(cleanEmptyPlainAttrs(inner.getPlainAttrs())); + fixPlainAndVirAttrs(inner, getOriginalItem().getInnerObject()); AnyObjectPatch patch = AnyOperations.diff(inner, getOriginalItem().getInnerObject(), false); // update just if it is changed diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyWizardBuilder.java b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyWizardBuilder.java index ec3ff78..783aa79 100644 --- a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyWizardBuilder.java +++ b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/AnyWizardBuilder.java @@ -18,9 +18,7 @@ */ package org.apache.syncope.client.console.wizards.any; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.apache.syncope.client.console.layout.AbstractAnyFormLayout; import org.apache.syncope.client.console.layout.AnyForm; import org.apache.syncope.client.console.layout.AnyObjectFormLayoutInfo; @@ -29,8 +27,8 @@ import org.apache.syncope.client.console.layout.UserFormLayoutInfo; import org.apache.syncope.client.console.wizards.AjaxWizard; import org.apache.syncope.client.console.wizards.AjaxWizardBuilder; import org.apache.syncope.common.lib.to.AnyTO; -import org.apache.syncope.common.lib.to.AttrTO; import org.apache.syncope.common.lib.to.GroupTO; +import org.apache.syncope.common.lib.to.GroupableRelatableTO; import org.apache.syncope.common.lib.to.UserTO; import org.apache.wicket.PageReference; import org.apache.wicket.extensions.wizard.WizardModel; @@ -175,10 +173,37 @@ public abstract class AnyWizardBuilder<A extends AnyTO> extends AjaxWizardBuilde } } - protected Set<AttrTO> cleanEmptyPlainAttrs(final Set<AttrTO> plainAttrs) { - Set<AttrTO> newPlainAttrs = new HashSet<>(plainAttrs); - plainAttrs.clear(); - plainAttrs.removeIf(attr -> attr.getValues().isEmpty()); - return newPlainAttrs; + protected void fixPlainAndVirAttrs(final AnyTO updated, final AnyTO original) { + // re-add to the updated object any missing plain or virtual attribute (compared to original): this to cope with + // form layout, which might have not included some plain or virtual attributes + original.getPlainAttrs().stream(). + filter(attr -> updated.getPlainAttr(attr.getSchema()).isPresent()). + forEach(attr -> updated.getPlainAttrs().add(attr)); + original.getVirAttrs().stream(). + filter(attr -> updated.getVirAttr(attr.getSchema()).isPresent()). + forEach(attr -> updated.getVirAttrs().add(attr)); + if (updated instanceof GroupableRelatableTO && original instanceof GroupableRelatableTO) { + GroupableRelatableTO.class.cast(original).getMemberships().forEach(oMemb -> { + GroupableRelatableTO.class.cast(updated).getMembership(oMemb.getGroupKey()).ifPresent(uMemb -> { + oMemb.getPlainAttrs().stream(). + filter(attr -> uMemb.getPlainAttr(attr.getSchema()).isPresent()). + forEach(attr -> uMemb.getPlainAttrs().add(attr)); + oMemb.getVirAttrs().stream(). + filter(attr -> uMemb.getVirAttr(attr.getSchema()).isPresent()). + forEach(attr -> uMemb.getVirAttrs().add(attr)); + }); + }); + } + + // remove from the updated object any plain or virtual attribute without values, thus triggering for removal in + // the generated patch + updated.getPlainAttrs().removeIf(attr -> attr.getValues().isEmpty()); + updated.getVirAttrs().removeIf(attr -> attr.getValues().isEmpty()); + if (updated instanceof GroupableRelatableTO) { + GroupableRelatableTO.class.cast(updated).getMemberships().forEach(memb -> { + memb.getPlainAttrs().removeIf(attr -> attr.getValues().isEmpty()); + memb.getVirAttrs().removeIf(attr -> attr.getValues().isEmpty()); + }); + } } } diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/GroupWizardBuilder.java b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/GroupWizardBuilder.java index 81c6648..a29078f 100644 --- a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/GroupWizardBuilder.java +++ b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/GroupWizardBuilder.java @@ -91,7 +91,7 @@ public class GroupWizardBuilder extends AnyWizardBuilder<GroupTO> implements Gro if (inner.getKey() == null) { result = groupRestClient.create(inner); } else { - inner.getPlainAttrs().addAll(cleanEmptyPlainAttrs(inner.getPlainAttrs())); + fixPlainAndVirAttrs(inner, getOriginalItem().getInnerObject()); GroupPatch patch = AnyOperations.diff(inner, getOriginalItem().getInnerObject(), false); GroupTO originaObj = getOriginalItem().getInnerObject(); diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/UserWizardBuilder.java b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/UserWizardBuilder.java index 12fd4d9..5e17524 100644 --- a/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/UserWizardBuilder.java +++ b/client/console/src/main/java/org/apache/syncope/client/console/wizards/any/UserWizardBuilder.java @@ -82,7 +82,7 @@ public class UserWizardBuilder extends AnyWizardBuilder<UserTO> implements UserF ? UserWrapper.class.cast(modelObject).isStorePasswordInSyncope() : StringUtils.isNotBlank(inner.getPassword())); } else { - inner.getPlainAttrs().addAll(cleanEmptyPlainAttrs(inner.getPlainAttrs())); + fixPlainAndVirAttrs(inner, getOriginalItem().getInnerObject()); UserPatch patch = AnyOperations.diff(inner, getOriginalItem().getInnerObject(), false); if (StringUtils.isNotBlank(inner.getPassword())) {