From 3bb2e8a5bf8194b0903cde2e220471a5ff63b536 Mon Sep 17 00:00:00 2001 From: Gathika94 Date: Wed, 27 Mar 2019 14:33:14 +0530 Subject: [PATCH] Add new method to put permission taking String parameters --- .../mgt/core/impl/ApplicationManagerImpl.java | 12 +++-- .../core/lifecycle/LifecycleStateManger.java | 48 +++++++++++-------- .../application/mgt/core/util/APIUtil.java | 1 - .../LifecycleManagementTest.java | 15 +----- .../core/permission/mgt/PermissionUtils.java | 8 +++- 5 files changed, 43 insertions(+), 41 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index b5cad145d1..ab34e6b549 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -106,7 +106,8 @@ public class ApplicationManagerImpl implements ApplicationManager { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); application.setUser(new User(userName, tenantId)); if (log.isDebugEnabled()) { - log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName); + log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + + userName); } validateAppCreatingRequest(application, tenantId); //todo throw different exception @@ -576,7 +577,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(), appLifecycleState.getCurrentState()); if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState())) { + newAppLifecycleState.getCurrentState(),userName,tenantId)) { this.lifecycleStateDAO .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), tenantId); @@ -621,8 +622,9 @@ public class ApplicationManagerImpl implements ApplicationManager { (currentState)) { LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(), appLifecycleState.getCurrentState()); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState())) { + newAppLifecycleState.getCurrentState(),userName,tenantId)) { this.lifecycleStateDAO .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), tenantId); @@ -979,12 +981,12 @@ public class ApplicationManagerImpl implements ApplicationManager { + " and application release UUID: " + releaseUuid); } state.setPreviousState(currentState.getCurrentState()); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); state.setUpdatedBy(userName); if (state.getCurrentState() != null && state.getPreviousState() != null) { - if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState(),userName,tenantId)) { + if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState(),userName, + tenantId)) { //todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there //todo any other application release in PUBLISHED state for the application( i.e for the appid) this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java index 6f5bc8bb8d..ac011a9401 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java @@ -1,5 +1,7 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; @@ -23,6 +25,7 @@ import java.util.Set; public class LifecycleStateManger { private Map lifecycleStates; + private static Log log = LogFactory.getLog(LifecycleStateManger.class); public void init(List states) throws LifecycleManagementException { lifecycleStates = new HashMap<>(); @@ -33,11 +36,11 @@ public class LifecycleStateManger { lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(), s.getProceedingStates(), s.getPermission(),s.isAppUpdatable(),s.isAppInstallable(), s.isInitialState(),s.isEndState())); - Permission permissionOfState = new Permission(); - permissionOfState.setPath(s.getPermission()); try { - PermissionUtils.putPermission(permissionOfState); + PermissionUtils.putPermission(s.getPermission()); } catch (PermissionManagementException e) { + log.error("Error when adding permission " + s.getPermission() + " related to the state: " + + s.getName(), e); throw new LifecycleManagementException ( "Error when adding permission " + s.getPermission() + " related to the state: " + s.getName(), e); @@ -54,26 +57,31 @@ public class LifecycleStateManger { UserRealm userRealm = null; String permission = getPermissionForStateChange(nextState); - try { - userRealm = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); - if(userRealm != null && userRealm.getAuthorizationManager() != null && - userRealm.getAuthorizationManager().isUserAuthorized(username, - PermissionUtils.getAbsolutePermissionPath(permission), - Constants.UI_EXECUTE)){ - if (currentState.equalsIgnoreCase(nextState)) { - return true; - } - State state = getMatchingState(currentState); - if (state != null) { - return getMatchingNextState(state.getProceedingStates(), nextState); + if(permission != null) { + try { + userRealm = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); + if (userRealm != null && userRealm.getAuthorizationManager() != null && + userRealm.getAuthorizationManager().isUserAuthorized(username, + PermissionUtils.getAbsolutePermissionPath(permission), + Constants.UI_EXECUTE)) { + if (currentState.equalsIgnoreCase(nextState)) { + return true; + } + State state = getMatchingState(currentState); + if (state != null) { + return getMatchingNextState(state.getProceedingStates(), nextState); + } + return false; } return false; + } catch (UserStoreException e) { + throw new LifecycleManagementException( + "UserStoreException exception from changing the state from : " + currentState + " to: " + + nextState + " with username : " + username + " and tenant Id : " + tenantId, e); } - return false; - } catch (UserStoreException e) { - throw new LifecycleManagementException ( - "UserStoreException exception from changing the state from : " + currentState + " to: " - + nextState+" with username : "+ username+" and tenant Id : "+tenantId, e); + }else{ + throw new LifecycleManagementException( + "Required permissions cannot be found for the state : "+nextState); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java index 449b8b3161..d71098660e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java @@ -159,7 +159,6 @@ public class APIUtil { } } } - return configManager; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java index a9d9aa8dec..7bb2f18bb0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java @@ -3,6 +3,7 @@ package org.wso2.carbon.device.application.mgt.core; import org.junit.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; @@ -22,7 +23,7 @@ public class LifecycleManagementTest { @BeforeClass - public void init() { + public void init() throws LifecycleManagementException { ConfigurationManager configurationManager = ConfigurationManager.getInstance(); Configuration configuration = configurationManager.getConfiguration(); lifecycleStates = configuration.getLifecycleStates(); @@ -44,16 +45,4 @@ public class LifecycleManagementTest { proceedingStates.contains(BOGUS_STATE.toUpperCase())); } - /*@Test - public void checkValidStateChange() { - Assert.assertTrue("Invalid state transition from: " + CURRENT_STATE + " to: " + NEXT_STATE, - lifecycleStateManger.isValidStateChange(CURRENT_STATE, NEXT_STATE)); - } - - @Test - public void checkInvalidStateChange() { - Assert.assertFalse("Invalid state transition from: " + CURRENT_STATE + " to: " + BOGUS_STATE, - lifecycleStateManger.isValidStateChange(CURRENT_STATE, BOGUS_STATE,)); - }*/ - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java index 6a8a5b75c1..758f163b52 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java @@ -80,9 +80,13 @@ public class PermissionUtils { } public static boolean putPermission(Permission permission) throws PermissionManagementException { + return putPermission(permission.getPath()); + } + + public static boolean putPermission(String permissionPath) throws PermissionManagementException { boolean status; try { - StringTokenizer tokenizer = new StringTokenizer(permission.getPath(), "/"); + StringTokenizer tokenizer = new StringTokenizer(permissionPath, "/"); String lastToken = "", currentToken, tempPath; while (tokenizer.hasMoreTokens()) { currentToken = tokenizer.nextToken(); @@ -95,7 +99,7 @@ public class PermissionUtils { status = true; } catch (RegistryException e) { throw new PermissionManagementException("Error occurred while persisting permission : " + - permission.getName(), e); + permissionPath, e); } return status; }