diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core')
8 files changed, 426 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/pom.xml b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/pom.xml new file mode 100644 index 0000000000..a420b23f6e --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/pom.xml @@ -0,0 +1,63 @@ +<?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>openecomp-item-permissions-lib</artifactId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <artifactId>openecomp-item-permissions-core</artifactId> + + <dependencies> + + <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-session-lib</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.core</groupId> + <artifactId>openecomp-facade-core</artifactId> + <version>${project.version}</version> + </dependency> + + + <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-nosqldb-core</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.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>RELEASE</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>RELEASE</version> + </dependency> + + </dependencies> + +</project>
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java new file mode 100644 index 0000000000..87c0338c25 --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoFactoryImpl.java @@ -0,0 +1,19 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + + +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDaoFactory; + + +/** + * Created by ayalaben on 6/18/2017. + */ +public class ItemPermissionsDaoFactoryImpl extends ItemPermissionsDaoFactory { + + private static ItemPermissionsDao INSTANCE =new ItemPermissionsDaoImpl(); + + @Override + public ItemPermissionsDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java new file mode 100644 index 0000000000..e1a9d1c874 --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/ItemPermissionsDaoImpl.java @@ -0,0 +1,66 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; +import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; + +import java.util.Collection; +import java.util.Set; + +/** + * Created by ayalaben on 6/20/2017. + */ +public class ItemPermissionsDaoImpl implements ItemPermissionsDao { + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static ItemPermissionsAccessor accessor = + noSqlDb.getMappingManager().createAccessor(ItemPermissionsAccessor.class); + + @Override + public Collection<ItemPermissionsEntity> listItemPermissions(String itemId) { + return accessor.getItemPermissions(itemId).all(); + } + + @Override + public void updateItemPermissions(String itemId, String permission, Set<String> addedUsersIds, + Set<String> removedUsersIds) { + addedUsersIds.forEach(userId -> accessor.addPermission(itemId,userId,permission)); + removedUsersIds.forEach(userId -> accessor.deletePermission(itemId,userId)); + } + + @Override + public void addUserPermission(String itemId, String userId, String permission){ + accessor.addPermission(itemId,userId,permission); + } + + @Override + public String getUserItemPermiission(String itemId, String userId) { + + ResultSet result = accessor.getUserItemPermission(itemId,userId); + if (result.getAvailableWithoutFetching() < 1) { + return null; + } + return result.one().getString(0); + } + + + @Accessor + interface ItemPermissionsAccessor { + @Query("select * from dox.item_permissions WHERE item_id = ?") + Result<ItemPermissionsEntity> getItemPermissions(String itemId); + + @Query("select permission from dox.item_permissions WHERE item_id = ? AND user_id=?") + ResultSet getUserItemPermission(String itemId,String userId); + + @Query("delete from dox.item_permissions where item_id = ? and user_id = ?") + void deletePermission(String itemId, String userId); + + @Query("insert into dox.item_permissions (item_id,user_id,permission) values (?,?,?)") + void addPermission(String itemId,String userId, String permission); + + } +} diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java new file mode 100644 index 0000000000..ae40ea8d97 --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImpl.java @@ -0,0 +1,70 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.itempermissions.PermissionsRules; +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; +import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; + +import java.util.Collection; +import java.util.Set; + + +/** + * Created by ayalaben on 6/22/2017. + */ +public class PermissionsServicesImpl implements PermissionsServices { + + private ItemPermissionsDao permissionsDao; + + private PermissionsRules permissionsRules; + + private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; + + public PermissionsServicesImpl(PermissionsRules permissionsRules, + ItemPermissionsDao permissionsDao) { + this.permissionsDao = permissionsDao; + this.permissionsRules = permissionsRules; + } + + + @Override + public Collection<ItemPermissionsEntity> listItemPermissions(String itemId) { + return permissionsDao.listItemPermissions(itemId); + } + + @Override + public void updateItemPermissions(String itemId, String permission, Set<String> addedUsersIds, + Set<String> removedUsersIds) { + + String currentUserId = SessionContextProviderFactory.getInstance() + .createInterface().get().getUser().getUserId(); + + permissionsRules.executeAction(itemId,currentUserId,CHANGE_PERMISSIONS); + + permissionsRules.updatePermission(itemId,currentUserId,permission,addedUsersIds, + removedUsersIds); + + permissionsDao.updateItemPermissions(itemId, permission, + addedUsersIds, removedUsersIds); + + } + + @Override + public boolean isAllowed(String itemId,String userId,String action) { + + String userPermission = permissionsDao.getUserItemPermiission(itemId,userId); + return permissionsRules.isAllowed(userPermission,action); + } + + @Override + public void execute(String itemId,String userId,String action) { + permissionsRules.executeAction(itemId, userId, action); + } + + @Override + public String getUserItemPermiission(String itemId, String userId) { + return permissionsDao.getUserItemPermiission(itemId,userId); + } + +} diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PrmissionsServicesFactoryImpl.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PrmissionsServicesFactoryImpl.java new file mode 100644 index 0000000000..34532cb384 --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/dao/impl/PrmissionsServicesFactoryImpl.java @@ -0,0 +1,21 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import org.openecomp.sdc.itempermissions.PermissionsRulesFactory; +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.openecomp.sdc.itempermissions.PermissionsServicesFactory; +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDaoFactory; + +/** + * Created by ayalaben on 6/22/2017 + */ +public class PrmissionsServicesFactoryImpl extends PermissionsServicesFactory { + + private static final PermissionsServices INSTANCE = + new org.openecomp.sdc.itempermissions.dao.impl.PermissionsServicesImpl(PermissionsRulesFactory.getInstance().createInterface(), + ItemPermissionsDaoFactory.getInstance().createInterface()); + + @Override + public PermissionsServices createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java new file mode 100644 index 0000000000..21acc07733 --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/java/org/openecomp/sdc/itempermissions/servlet/PermissionsFilter.java @@ -0,0 +1,72 @@ +package org.openecomp.sdc.itempermissions.servlet; + +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.openecomp.sdc.itempermissions.PermissionsServicesFactory; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by ayalaben on 6/27/2017. + */ +public class PermissionsFilter implements Filter { + + private final PermissionsServices permissionsServices; + public static final String IRRELEVANT_REQUEST = "Irrelevant_Request"; + public static final String EDIT_ITEM = "Edit_Item"; + + public PermissionsFilter() { + this.permissionsServices = PermissionsServicesFactory.getInstance().createInterface(); + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, + FilterChain filterChain) throws IOException, ServletException { + + if (servletRequest instanceof HttpServletRequest) { + if (((HttpServletRequest) servletRequest).getMethod().equals("POST") + || ((HttpServletRequest) servletRequest).getMethod().equals("PUT")) { + + String userId = ((HttpServletRequest) servletRequest).getHeader("USER_ID"); + String itemId = parseItemIdFromPath(((HttpServletRequest) servletRequest).getPathInfo()); + if ( ! itemId.equals(IRRELEVANT_REQUEST)) { + if ( ! permissionsServices.isAllowed(itemId,userId,EDIT_ITEM)) { + ((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_FORBIDDEN); + servletResponse.getWriter().print("Permissions Error. The user does not have " + + "permission to perform" + + " this action."); + return; + } + } + } + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private String parseItemIdFromPath(String pathInfo) { + String[] tokens = pathInfo.split("/"); + if (tokens.length < 4) { + return IRRELEVANT_REQUEST; + } else { + return tokens[3]; + } + } + + @Override + public void destroy() { + + } +} diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/resources/factoryConfiguration.json b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..d23948437f --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/resources/factoryConfiguration.json @@ -0,0 +1,4 @@ +{ + "org.openecomp.sdc.itempermissions.dao.ItemPermissionsDaoFactory": "org.openecomp.sdc.itempermissions.dao.impl.ItemPermissionsDaoFactoryImpl", +"org.openecomp.sdc.itempermissions.PermissionsServicesFactory" : "org.openecomp.sdc.itempermissions.dao.impl.PrmissionsServicesFactoryImpl" +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java new file mode 100644 index 0000000000..0a87c29f50 --- /dev/null +++ b/openecomp-be/lib/openecomp-item-permissions-lib/openecomp-item-permissions-core/src/main/test/java/org/openecomp/sdc/itempermissions/dao/impl/PermissionsServicesImplTest.java @@ -0,0 +1,111 @@ +package org.openecomp.sdc.itempermissions.dao.impl; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.itempermissions.PermissionsRules; +import org.openecomp.sdc.itempermissions.dao.ItemPermissionsDao; +import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Created by ayalaben on 7/9/2017 + */ +public class PermissionsServicesImplTest { + + private static final String ITEM1_ID = "1"; + private static final String PERMISSION = "Owner"; + private static final String USER2_ID = "testUser2"; + private static final String USER1_ID = "testUser1"; + private static final String ACTION_SUBMIT = "Submit_Item"; + private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions"; + + static { + SessionContextProviderFactory.getInstance().createInterface().create("testUser1"); + } + + @Mock + private ItemPermissionsDao permissionsDaoMock; + @Mock + private PermissionsRules permissionsRules; + @InjectMocks + @Spy + private PermissionsServicesImpl permissionsServices; + + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testListItemPermissionsWhenNone() { + Collection<ItemPermissionsEntity> permissions = + permissionsServices.listItemPermissions(ITEM1_ID); + Assert.assertEquals(permissions.size(), 0); + } + + + @Test + public void testListItemPermissions(){ + doReturn(Arrays.asList( + createPermissionEntity(ITEM1_ID, USER1_ID, PERMISSION), + createPermissionEntity(ITEM1_ID, USER2_ID, PERMISSION))) + .when(permissionsDaoMock).listItemPermissions(anyObject()); + + Collection<ItemPermissionsEntity> actual = + permissionsServices.listItemPermissions(ITEM1_ID); + Assert.assertEquals(actual.size(), 2); + } + + + @Test + public void testIsAllowed(){ + when(permissionsDaoMock.getUserItemPermiission(ITEM1_ID,USER1_ID)).thenReturn(PERMISSION); + when(permissionsRules.isAllowed(PERMISSION,ACTION_SUBMIT)).thenReturn(true); + + Boolean result = permissionsServices.isAllowed(ITEM1_ID,USER1_ID,ACTION_SUBMIT); + + Assert.assertTrue(result); + + } + @Test + public void testUpdatePermissions(){ + + Set<String> addedUsers = new HashSet<String>(); + addedUsers.add(USER2_ID); + + permissionsServices.updateItemPermissions(ITEM1_ID,PERMISSION,addedUsers, + new HashSet<String>()); + + verify(permissionsRules).executeAction(ITEM1_ID,USER1_ID,CHANGE_PERMISSIONS); + verify(permissionsRules).updatePermission(ITEM1_ID,USER1_ID,PERMISSION,addedUsers,new HashSet<String>()); + verify(permissionsDaoMock).updateItemPermissions(ITEM1_ID,PERMISSION,addedUsers,new + HashSet<String>()); + } + + + public static ItemPermissionsEntity createPermissionEntity(String itemId, String + userId, String permission) { + ItemPermissionsEntity permissionsEntity = new ItemPermissionsEntity(); + permissionsEntity.setItemId(itemId); + permissionsEntity.setUserId(userId); + permissionsEntity.setPermission(permission); + return permissionsEntity; + } + +}
\ No newline at end of file |