Skip to content

Commit

Permalink
logic moved to service
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelGaro committed Nov 6, 2024
1 parent 57977e4 commit b3cff33
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ private static <T, K extends AbstractReplacePatchItem<T>> K replacePatchItem(
}

private static void diff(
final AnyTO updated,
final AnyTO original,
final AnyUR result,
final boolean incremental,
final boolean preserve) {
final AnyTO updated, final AnyTO original, final AnyUR result, final boolean incremental) {

// check same key
if (updated.getKey() == null && original.getKey() != null
Expand All @@ -97,7 +93,7 @@ private static void diff(
// 2. auxilairy classes
result.getAuxClasses().clear();

if (!incremental && !preserve) {
if (!incremental) {
original.getAuxClasses().stream().filter(auxClass -> !updated.getAuxClasses().contains(auxClass)).
forEach(auxClass -> result.getAuxClasses().add(new StringPatchItem.Builder().
operation(PatchOperation.DELETE).value(auxClass).build()));
Expand Down Expand Up @@ -147,7 +143,7 @@ private static void diff(
// 5. resources
result.getResources().clear();

if (!incremental && !preserve) {
if (!incremental) {
original.getResources().stream().filter(resource -> !updated.getResources().contains(resource)).
forEach(resource -> result.getResources().add(new StringPatchItem.Builder().
operation(PatchOperation.DELETE).value(resource).build()));
Expand All @@ -171,7 +167,7 @@ public static AnyObjectUR diff(

AnyObjectUR result = new AnyObjectUR();

diff(updated, original, result, incremental, incremental);
diff(updated, original, result, incremental);

// 1. name
result.setName(replacePatchItem(updated.getName(), original.getName(), new StringReplacePatchItem()));
Expand Down Expand Up @@ -235,14 +231,12 @@ private static void diff(
* @param updated updated UserTO
* @param original original UserTO
* @param incremental perform incremental diff (without removing existing info)
* @param preserve doesn't remove existing info for auxClasses, resources, roles, relationships and linked accounts
* @return {@link UserUR} containing differences
*/
public static UserUR diff(
final UserTO updated, final UserTO original, final boolean incremental, final boolean preserve) {
public static UserUR diff(final UserTO updated, final UserTO original, final boolean incremental) {
UserUR result = new UserUR();

diff(updated, original, result, incremental, preserve);
diff(updated, original, result, incremental);

// 1. password
if (updated.getPassword() != null
Expand Down Expand Up @@ -274,7 +268,7 @@ public static UserUR diff(
updated.isMustChangePassword(), original.isMustChangePassword(), new BooleanReplacePatchItem()));

// 4. roles
if (!incremental && !preserve) {
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()));
Expand All @@ -295,7 +289,7 @@ public static UserUR diff(
forEach(entry -> result.getRelationships().add(new RelationshipUR.Builder(entry.getValue()).
operation(PatchOperation.ADD_REPLACE).build()));

if (!incremental && !preserve) {
if (!incremental) {
originalRels.keySet().stream().filter(relationship -> !updatedRels.containsKey(relationship)).
forEach(key -> result.getRelationships().add(new RelationshipUR.Builder(originalRels.get(key)).
operation(PatchOperation.DELETE).build()));
Expand Down Expand Up @@ -333,7 +327,7 @@ public static UserUR diff(
linkedAccountTO(entry.getValue()).build());
});

if (!incremental && !preserve) {
if (!incremental) {
originalAccounts.keySet().stream().filter(account -> !updatedAccounts.containsKey(account)).
forEach(key -> {
result.getLinkedAccounts().add(new LinkedAccountUR.Builder().
Expand All @@ -351,14 +345,12 @@ public static UserUR diff(
* @param updated updated GroupTO
* @param original original GroupTO
* @param incremental perform incremental diff (without removing existing info)
* @param preserve doesn't remove existing info for auxClasses and resources
* @return {@link GroupUR} containing differences
*/
public static GroupUR diff(
final GroupTO updated, final GroupTO original, final boolean incremental, final boolean preserve) {
public static GroupUR diff(final GroupTO updated, final GroupTO original, final boolean incremental) {
GroupUR result = new GroupUR();

diff(updated, original, result, incremental, preserve);
diff(updated, original, result, incremental);

// 1. name
result.setName(replacePatchItem(updated.getName(), original.getName(), new StringReplacePatchItem()));
Expand All @@ -384,9 +376,9 @@ public static <TO extends AnyTO, P extends AnyUR> P diff(
final TO updated, final TO original, final boolean incremental) {

if (updated instanceof UserTO && original instanceof UserTO) {
return (P) diff((UserTO) updated, (UserTO) original, incremental, incremental);
return (P) diff((UserTO) updated, (UserTO) original, incremental);
} else if (updated instanceof GroupTO && original instanceof GroupTO) {
return (P) diff((GroupTO) updated, (GroupTO) original, incremental, incremental);
return (P) diff((GroupTO) updated, (GroupTO) original, incremental);
} else if (updated instanceof AnyObjectTO && original instanceof AnyObjectTO) {
return (P) diff((AnyObjectTO) updated, (AnyObjectTO) original, incremental);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.common.lib.AnyOperations;
import org.apache.syncope.common.lib.SyncopeConstants;
import org.apache.syncope.common.lib.request.GroupUR;
import org.apache.syncope.common.lib.request.MembershipUR;
import org.apache.syncope.common.lib.request.UserUR;
import org.apache.syncope.common.lib.to.GroupTO;
Expand Down Expand Up @@ -187,8 +188,10 @@ public Response replace(final String id, final SCIMGroup group) {
Set<String> beforeMembers = members(id);

// update group, don't change members
ProvisioningResult<GroupTO> result = groupLogic.update(
AnyOperations.diff(binder.toGroupTO(group, true), groupLogic.read(id), false, true), false);
GroupUR req = AnyOperations.diff(binder.toGroupTO(group, true), groupLogic.read(id), false);
req.getResources().clear();
req.getAuxClasses().clear();
ProvisioningResult<GroupTO> result = groupLogic.update(req, false);

// assign new members
Set<String> afterMembers = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,13 @@ public Response replace(final String id, final SCIMUser user) {

UserTO before = userLogic.read(id);

ProvisioningResult<UserTO> result = userLogic.update(
AnyOperations.diff(binder.toUserTO(user, true), before, false, true), false);
UserUR req = AnyOperations.diff(binder.toUserTO(user, true), before, false);
req.getResources().clear();
req.getAuxClasses().clear();
req.getRelationships().clear();
req.getRoles().clear();
req.getLinkedAccounts().clear();
ProvisioningResult<UserTO> result = userLogic.update(req, false);

if (before.isSuspended() == user.isActive()) {
StatusR statusR = new StatusR.Builder(
Expand Down

0 comments on commit b3cff33

Please sign in to comment.