From 8e9c0653dd6c6862123c9609ae34e1206d86456e Mon Sep 17 00:00:00 2001 From: talig Date: Wed, 20 Dec 2017 14:30:43 +0200 Subject: Add collaboration feature Issue-ID: SDC-767 Change-Id: I14fb4c1f54086ed03a56a7ff7fab9ecd40381795 Signed-off-by: talig --- .../itempermissions/ItemPermissionsManager.java | 23 +++ .../ItemPermissionsManagerFactory.java | 15 ++ .../impl/ItemPermissionsManagerFactoryImpl.java | 25 ++++ .../dao/impl/ItemPermissionsManagerImpl.java | 159 +++++++++++++++++++++ .../src/main/resources/factoryConfiguration.json | 3 + .../dao/impl/ItemPermissionsManagerImplTest.java | 86 +++++++++++ .../src/test/resources/logback.xml | 15 ++ 7 files changed, 326 insertions(+) create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManager.java create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManagerFactory.java create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerFactoryImpl.java create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImpl.java create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/resources/factoryConfiguration.json create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImplTest.java create mode 100644 openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/resources/logback.xml (limited to 'openecomp-be/backend/openecomp-sdc-item-permissions-manager/src') diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManager.java b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManager.java new file mode 100644 index 0000000000..74fc3d3c46 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManager.java @@ -0,0 +1,23 @@ +package org.openecomp.sdc.itempermissions; + +import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; + +import java.util.Collection; +import java.util.Set; + +/** + * Created by ayalaben on 6/18/2017. + */ +public interface ItemPermissionsManager { + + Collection listItemPermissions(String itemId); + + void updateItemPermissions(String itemId, String permission, Set addedUsersIds, + Set removedUsersIds); + + boolean isAllowed(String itemId,String userId,String action); + + String getUserItemPermiission(String itemId, String userId); + + +} diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManagerFactory.java b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManagerFactory.java new file mode 100644 index 0000000000..b983314b31 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/ItemPermissionsManagerFactory.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.itempermissions; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; + +/** + * Created by ayalaben on 6/18/2017. + */ +public abstract class ItemPermissionsManagerFactory extends + AbstractComponentFactory { + + public static ItemPermissionsManagerFactory getInstance() { + return AbstractFactory.getInstance(ItemPermissionsManagerFactory.class); + } +} diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerFactoryImpl.java b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerFactoryImpl.java new file mode 100644 index 0000000000..2255ad5fc3 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerFactoryImpl.java @@ -0,0 +1,25 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import org.openecomp.sdc.itempermissions.ItemPermissionsManager; +import org.openecomp.sdc.itempermissions.ItemPermissionsManagerFactory; +import org.openecomp.sdc.itempermissions.PermissionsServicesFactory; +import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory; +import org.openecomp.sdc.notification.factories.SubscriptionServiceFactory; +import org.openecomp.sdc.versioning.ItemManagerFactory; + +/** + * Created by ayalaben on 6/18/2017 + */ +public class ItemPermissionsManagerFactoryImpl extends ItemPermissionsManagerFactory { + + private static final ItemPermissionsManager INSTANCE = + new ItemPermissionsManagerImpl(PermissionsServicesFactory.getInstance().createInterface(), + ItemManagerFactory.getInstance().createInterface(), + NotificationPropagationManagerFactory.getInstance().createInterface(), + SubscriptionServiceFactory.getInstance().createInterface()); + + @Override + public ItemPermissionsManager createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImpl.java b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImpl.java new file mode 100644 index 0000000000..b9fcfcdbed --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImpl.java @@ -0,0 +1,159 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.itempermissions.ItemPermissionsManager; +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.notification.services.SubscriptionService; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.types.Item; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.ITEM_ID_PROP; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.ITEM_NAME_PROP; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_CHANGED; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_GRANTED; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_ITEM; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; + +/** + * Created by ayalaben on 6/18/2017. + */ +public class ItemPermissionsManagerImpl implements ItemPermissionsManager { + + private static final Logger LOGGER = LoggerFactory.getLogger(ItemPermissionsManagerImpl.class); + private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; + + private PermissionsServices permissionsServices; + private ItemManager itemManager; + private NotificationPropagationManager notifier; + private SubscriptionService subscriptionService; + + public ItemPermissionsManagerImpl(PermissionsServices permissionsServices, + ItemManager itemManager, + NotificationPropagationManager notificationPropagationManager, + SubscriptionService subscriptionService) { + this.permissionsServices = permissionsServices; + this.itemManager = itemManager; + this.notifier = notificationPropagationManager; + this.subscriptionService = subscriptionService; + } + + @Override + public Collection listItemPermissions(String itemId) { + + return permissionsServices.listItemPermissions(itemId); + } + + @Override + public void updateItemPermissions(String itemId, String permission, Set addedUsersIds, + Set removedUsersIds) { + + String currentUser = + SessionContextProviderFactory.getInstance().createInterface().get().getUser().getUserId(); + + if (!permissionsServices.isAllowed(itemId, currentUser, CHANGE_PERMISSIONS)) { + throw new CoreException(new ErrorCode.ErrorCodeBuilder() + .withMessage(Messages.PERMISSIONS_ERROR.getErrorMessage()) + .withId(Messages.PERMISSIONS_ERROR.getErrorMessage()) + .withCategory(ErrorCategory.SECURITY).build()); + } + + permissionsServices + .updateItemPermissions(itemId, permission, addedUsersIds, removedUsersIds); + sendNotifications(itemId, permission, addedUsersIds, removedUsersIds, currentUser); + } + + private void sendNotifications(String itemId, String permission, Set addedUsersIds, + Set removedUsersIds, String userName) { + + Item item = itemManager.get(itemId); + addedUsersIds.forEach(affectedUser -> { + notifyUser(userName, true, item.getName(), itemId, affectedUser, permission); + subscriptionService.subscribe(affectedUser, itemId); + }); + removedUsersIds.forEach(affectedUser -> { + notifyUser(userName, false, item.getName(), itemId, affectedUser, permission); + subscriptionService.unsubscribe(affectedUser, itemId); + }); + + } + + private void notifyUser(String userName, boolean granted, String itemName, String itemId, + String affectedUser, String permission) { + Map details = new HashMap<>(); + details.put(PERMISSION_ITEM, permission); + details.put(ITEM_ID_PROP, itemId); + details.put(ITEM_NAME_PROP, itemName); + details.put(PERMISSION_GRANTED, granted); + details.put(PERMISSION_USER, userName); + PermissionEvent permissionEvent = new PermissionEvent(PERMISSION_CHANGED, affectedUser, + details, affectedUser); + + try { + notifier.directNotification(permissionEvent, affectedUser); + } catch (Exception e) { + LOGGER.error("Failed to send notification on permission changed for user '" + + affectedUser + "'"); + } + + } + + @Override + public boolean isAllowed(String itemId, String userId, String action) { + return permissionsServices.isAllowed(itemId, userId, action); + } + + @Override + public String getUserItemPermiission(String itemId, String userId) { + return permissionsServices.getUserItemPermiission(itemId, userId); + } + + private class PermissionEvent implements Event { + + private String eventType; + private String originatorId; + private Map attributes; + private String entityId; + + private PermissionEvent(String eventType, String originatorId, + Map attributes, String entityId) { + this.eventType = eventType; + this.originatorId = originatorId; + this.attributes = attributes; + this.entityId = entityId; + } + + @Override + public String getEventType() { + return eventType; + } + + @Override + public String getOriginatorId() { + return originatorId; + } + + @Override + public Map getAttributes() { + return attributes; + } + + @Override + public String getEntityId() { + return entityId; + } + } +} diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/resources/factoryConfiguration.json b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..a98f9c07f9 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/main/resources/factoryConfiguration.json @@ -0,0 +1,3 @@ +{ + "org.openecomp.sdc.itempermissions.ItemPermissionsManagerFactory": "org.openecomp.sdc.itempermissions.dao.impl.ItemPermissionsManagerFactoryImpl" +} \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImplTest.java b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImplTest.java new file mode 100644 index 0000000000..e4618246d8 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsManagerImplTest.java @@ -0,0 +1,86 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.notification.services.SubscriptionService; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.types.Item; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +/** + * Created by ayalaben on 7/6/2017 + */ +public class ItemPermissionsManagerImplTest { + + private static final String ITEM1_ID = "1"; + private static final String PERMISSION = "Contributor"; + private static final String ACTION = "Change_Item_Permissions"; + private static final String USER = "user"; + private static final String AFFECTED_USER1 = "affected_user1"; + private static final String AFFECTED_USER2 = "affected_user2"; + private static final String AFFECTED_USER3 = "affected_user3"; + + @Mock + private PermissionsServices permissionsServicesMock; + @Mock + private ItemManager itemManagerMock; + @Mock + private SubscriptionService subscriptionServiceMock; + @Mock + private NotificationPropagationManager notifierMock; + @InjectMocks + private ItemPermissionsManagerImpl permissionsManager; + + @BeforeMethod + public void setUp() throws Exception { + SessionContextProviderFactory.getInstance().createInterface().create(USER); + MockitoAnnotations.initMocks(this); + } + + @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp = "Permissions " + + "Error. The user does not have permission to perform this action.") + public void testUpdateItemPermissionsWhenNotAllowed() { + doReturn(false).when(permissionsServicesMock).isAllowed(ITEM1_ID, USER, ACTION); + + permissionsManager + .updateItemPermissions(ITEM1_ID, PERMISSION, Collections.singleton(AFFECTED_USER1), + new HashSet<>()); + } + + @Test + public void testUpdateItemPermissions() { + doReturn(true).when(permissionsServicesMock).isAllowed(ITEM1_ID, USER, ACTION); + Item item = new Item(); + item.setName("Item 1 Name"); + doReturn(item).when(itemManagerMock).get(ITEM1_ID); + + Set addedUsersIds = + Stream.of(AFFECTED_USER1, AFFECTED_USER2).collect(Collectors.toSet()); + Set removedUsersIds = Collections.singleton(AFFECTED_USER3); + permissionsManager + .updateItemPermissions(ITEM1_ID, PERMISSION, addedUsersIds, removedUsersIds); + + verify(permissionsServicesMock) + .updateItemPermissions(ITEM1_ID, PERMISSION, addedUsersIds, removedUsersIds); + verify(subscriptionServiceMock).subscribe(AFFECTED_USER1, ITEM1_ID); + verify(subscriptionServiceMock).subscribe(AFFECTED_USER2, ITEM1_ID); + verify(subscriptionServiceMock).unsubscribe(AFFECTED_USER3, ITEM1_ID); + + // TODO: 12/18/2017 verify notification + } +} \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/resources/logback.xml b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/resources/logback.xml new file mode 100644 index 0000000000..c1932e31e8 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/src/test/resources/logback.xml @@ -0,0 +1,15 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file -- cgit 1.2.3-korg