From 32e379b32c0770c831eb04669d88ba6fdfcd9965 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Thu, 21 Feb 2019 15:29:15 +0100 Subject: [PATCH] Bug 4827: adjusted and fixed OwnershipMigrationDialog and associated Updater in back end to work as defined --- .../MigrateGroupOwnershipDialog.java | 49 ++++++++++++++++--- .../gwt/ui/server/SailingServiceImpl.java | 3 +- .../MigrateGroupOwnerForHierarchyDTO.java | 8 ++- .../gateway/jaxrs/api/EventsResource.java | 7 ++- .../jaxrs/api/LeaderboardGroupsResource.java | 7 ++- .../jaxrs/api/LeaderboardsResourceV2.java | 7 ++- .../webservices/api/v1/eventMigrate.html | 1 + .../api/v1/leaderboardMigrate.html | 1 + .../SailingHierarchyOwnershipUpdater.java | 49 ++++++++++++++++++- 9 files changed, 116 insertions(+), 16 deletions(-) diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/MigrateGroupOwnershipDialog.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/MigrateGroupOwnershipDialog.java index bbea0b6de6c..1a5446795dd 100644 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/MigrateGroupOwnershipDialog.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/MigrateGroupOwnershipDialog.java @@ -34,6 +34,10 @@ public class MigrateGroupOwnershipDialog extends DataEntryDialog onRadioChange()); + toExistingGroup.addValueChangeHandler(v -> onRadioChange()); migrateCompetitors = createCheckbox(""); migrateBoats = createCheckbox(""); this.stringMessages = stringMessages; resolveUserGroup(); + + // TODO: i18n + lblCopyMembersAndRoles = new Label("Copy members and roles"); + lblCopyMembersAndRoles.setVisible(false); + + userManagementService.getUserGroupByName(tenant.getName(), new AsyncCallback() { + @Override + public void onFailure(Throwable caught) { + Notification.notify(stringMessages.errorObtainingUserGroup(caught.getMessage()), + NotificationType.ERROR); + } + + @Override + public void onSuccess(UserGroupDTO result) { + oldUserGroup = result; + } + }); + } + + private void onRadioChange() { + copyMembersAndRoles.setVisible(toNewGroup.getValue()); + lblCopyMembersAndRoles.setVisible(toNewGroup.getValue()); + } @Override protected Widget getAdditionalWidget() { - final Grid result = new Grid(5, 2); + final Grid result = new Grid(6, 2); result.setWidget(0, 0, new Label(stringMessages.currentGroupOwner())); result.setWidget(0, 1, currentGroupLabel); result.setWidget(1, 0, toExistingGroup); @@ -114,6 +148,8 @@ public class MigrateGroupOwnershipDialog extends DataEntryDialogOptional parameters: existingGroupId group that should be changed, required for createNewGroup = false newGroupName name of the new group, required for createNewGroup = true + copyMembersAndRoles determines if the members and roles are copied (default: true) Request method: diff --git a/java/com.sap.sailing.server.gateway/webservices/api/v1/leaderboardMigrate.html b/java/com.sap.sailing.server.gateway/webservices/api/v1/leaderboardMigrate.html index 3a23a0abb68..188b5cfb631 100644 --- a/java/com.sap.sailing.server.gateway/webservices/api/v1/leaderboardMigrate.html +++ b/java/com.sap.sailing.server.gateway/webservices/api/v1/leaderboardMigrate.html @@ -33,6 +33,7 @@ Optional parameters: existingGroupId group that should be changed, required for createNewGroup = false newGroupName name of the new group, required for createNewGroup = true + copyMembersAndRoles determines if the members and roles are copied (default: true) Request method: diff --git a/java/com.sap.sailing.server/src/com/sap/sailing/server/hierarchy/SailingHierarchyOwnershipUpdater.java b/java/com.sap.sailing.server/src/com/sap/sailing/server/hierarchy/SailingHierarchyOwnershipUpdater.java index 2045c9701f5..2d951c7641e 100644 --- a/java/com.sap.sailing.server/src/com/sap/sailing/server/hierarchy/SailingHierarchyOwnershipUpdater.java +++ b/java/com.sap.sailing.server/src/com/sap/sailing/server/hierarchy/SailingHierarchyOwnershipUpdater.java @@ -35,6 +35,7 @@ import com.sap.sse.security.shared.impl.UserGroupImpl; public class SailingHierarchyOwnershipUpdater { public static SailingHierarchyOwnershipUpdater createOwnershipUpdater(boolean createNewGroup, UUID existingGroupIdOrNull, String newGroupName, boolean migrateCompetitors, boolean migrateBoats, + boolean copyMembersAndRoles, RacingEventService service) { SecurityService securityService = service.getSecurityService(); @@ -42,9 +43,13 @@ public class SailingHierarchyOwnershipUpdater { final GroupOwnerUpdateStrategy updateStrategy; if (!createNewGroup) { - updateStrategy = createExitingGroupModifyingUpdate(sourceGroup); + updateStrategy = createExitingGroupModifyingUpdate(sourceGroup); } else { - updateStrategy = createNewGroupUsingUpdate(newGroupName, securityService, sourceGroup, service); + if (copyMembersAndRoles) { + updateStrategy = createNewGroupUsingUpdate(newGroupName, securityService, sourceGroup, service); + } else { + updateStrategy = createNewGroupWithoutCopying(newGroupName, securityService); + } } return new SailingHierarchyOwnershipUpdater(service, securityService, updateStrategy, migrateCompetitors, migrateBoats); @@ -187,6 +192,44 @@ public class SailingHierarchyOwnershipUpdater { return updateStrategy; } + /** + * Creates a new group without copying the members and roles from an old group but add the current user to the new + * group. + */ + private static GroupOwnerUpdateStrategy createNewGroupWithoutCopying(final String newGroupName, + final SecurityService securityService) { + if (newGroupName == null || newGroupName.isEmpty()) { + throw new RuntimeException("No name for new Group given"); + } + + final GroupOwnerUpdateStrategy updateStrategy; + updateStrategy = new GroupOwnerUpdateStrategy() { + + private UserGroup groupOwnerToSet; + + @Override + public boolean needsUpdate(QualifiedObjectIdentifier identifier, OwnershipAnnotation currentOwnership) { + return true; + } + + @Override + public UserGroup getNewGroupOwner() { + if (groupOwnerToSet == null) { + try { + groupOwnerToSet = securityService.createUserGroup(UUID.randomUUID(), newGroupName); + final QualifiedObjectIdentifier identifier = groupOwnerToSet.getIdentifier(); + securityService.setDefaultOwnership(identifier, identifier.toString()); + securityService.addUserToUserGroup(groupOwnerToSet, securityService.getCurrentUser()); + } catch (UserGroupManagementException e) { + throw new RuntimeException("Could not create user group"); + } + } + return groupOwnerToSet; + } + }; + return updateStrategy; + } + private static GroupOwnerUpdateStrategy createNewGroupUsingUpdate(String newGroupName, SecurityService securityService, final UserGroup sourceGroup, RacingEventService service) { if (newGroupName == null || newGroupName.isEmpty()) { @@ -215,6 +258,8 @@ public class SailingHierarchyOwnershipUpdater { // The migration may start at an object that currently has no group owner (e.g. in case // this owner was just deleted) -> in this case we just create a new group groupOwnerToSet = securityService.createUserGroup(UUID.randomUUID(), newGroupName); + final QualifiedObjectIdentifier identifier = groupOwnerToSet.getIdentifier(); + securityService.setDefaultOwnership(identifier, identifier.toString()); } } catch (UserGroupManagementException e) { throw new RuntimeException("Could not create user group");