diff options
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-item-permissions-manager')
8 files changed, 397 insertions, 0 deletions
diff --git a/openecomp-be/backend/openecomp-sdc-item-permissions-manager/pom.xml b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/pom.xml new file mode 100644 index 0000000000..3212205620 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-item-permissions-manager/pom.xml @@ -0,0 +1,71 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>backend</artifactId> + <version>1.2.0-SNAPSHOT</version> + + </parent> + + <artifactId>openecomp-sdc-item-permissions-manager</artifactId> + + <dependencies> + + <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-session-lib</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-facade-core</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-item-permissions-api</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-item-permissions-impl</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-item-permissions-core</artifactId> + <version>${project.version}</version> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-versioning-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-notification-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>RELEASE</version> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>RELEASE</version> + </dependency> +</dependencies> + +</project>
\ No newline at end of file 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<ItemPermissionsEntity> listItemPermissions(String itemId); + + void updateItemPermissions(String itemId, String permission, Set<String> addedUsersIds, + Set<String> 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<ItemPermissionsManager> { + + 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<ItemPermissionsEntity> listItemPermissions(String itemId) { + + return permissionsServices.listItemPermissions(itemId); + } + + @Override + public void updateItemPermissions(String itemId, String permission, Set<String> addedUsersIds, + Set<String> 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<String> addedUsersIds, + Set<String> 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<String, Object> 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<String, Object> attributes; + private String entityId; + + private PermissionEvent(String eventType, String originatorId, + Map<String, Object> 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<String, Object> 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<String> addedUsersIds = + Stream.of(AFFECTED_USER1, AFFECTED_USER2).collect(Collectors.toSet()); + Set<String> 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 @@ +<configuration> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <!-- encoders are assigned the type + ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <root level="warn"> + <appender-ref ref="STDOUT" /> + </root> + +</configuration>
\ No newline at end of file |