Add new method to put permission taking String parameters

feature/appm-store/pbac
Gathika94 6 years ago
parent 6ba46facb1
commit 3bb2e8a5bf

@ -106,7 +106,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
application.setUser(new User(userName, tenantId)); application.setUser(new User(userName, tenantId));
if (log.isDebugEnabled()) { 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); validateAppCreatingRequest(application, tenantId);
//todo throw different exception //todo throw different exception
@ -576,7 +577,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(), LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(),
appLifecycleState.getCurrentState()); appLifecycleState.getCurrentState());
if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(), if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(),
newAppLifecycleState.getCurrentState())) { newAppLifecycleState.getCurrentState(),userName,tenantId)) {
this.lifecycleStateDAO this.lifecycleStateDAO
.addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(),
tenantId); tenantId);
@ -621,8 +622,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
(currentState)) { (currentState)) {
LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(), LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(),
appLifecycleState.getCurrentState()); appLifecycleState.getCurrentState());
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(), if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(),
newAppLifecycleState.getCurrentState())) { newAppLifecycleState.getCurrentState(),userName,tenantId)) {
this.lifecycleStateDAO this.lifecycleStateDAO
.addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(),
tenantId); tenantId);
@ -979,12 +981,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ " and application release UUID: " + releaseUuid); + " and application release UUID: " + releaseUuid);
} }
state.setPreviousState(currentState.getCurrentState()); state.setPreviousState(currentState.getCurrentState());
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName); state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null) { 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 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) //todo any other application release in PUBLISHED state for the application( i.e for the appid)
this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId); this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId);

@ -1,5 +1,7 @@
package org.wso2.carbon.device.application.mgt.core.lifecycle; 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.common.exception.LifecycleManagementException;
import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
@ -23,6 +25,7 @@ import java.util.Set;
public class LifecycleStateManger { public class LifecycleStateManger {
private Map<String, State> lifecycleStates; private Map<String, State> lifecycleStates;
private static Log log = LogFactory.getLog(LifecycleStateManger.class);
public void init(List<LifecycleState> states) throws LifecycleManagementException { public void init(List<LifecycleState> states) throws LifecycleManagementException {
lifecycleStates = new HashMap<>(); lifecycleStates = new HashMap<>();
@ -33,11 +36,11 @@ public class LifecycleStateManger {
lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(), lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(),
s.getProceedingStates(), s.getPermission(),s.isAppUpdatable(),s.isAppInstallable(), s.getProceedingStates(), s.getPermission(),s.isAppUpdatable(),s.isAppInstallable(),
s.isInitialState(),s.isEndState())); s.isInitialState(),s.isEndState()));
Permission permissionOfState = new Permission();
permissionOfState.setPath(s.getPermission());
try { try {
PermissionUtils.putPermission(permissionOfState); PermissionUtils.putPermission(s.getPermission());
} catch (PermissionManagementException e) { } catch (PermissionManagementException e) {
log.error("Error when adding permission " + s.getPermission() + " related to the state: "
+ s.getName(), e);
throw new LifecycleManagementException ( throw new LifecycleManagementException (
"Error when adding permission " + s.getPermission() + " related to the state: " "Error when adding permission " + s.getPermission() + " related to the state: "
+ s.getName(), e); + s.getName(), e);
@ -54,26 +57,31 @@ public class LifecycleStateManger {
UserRealm userRealm = null; UserRealm userRealm = null;
String permission = getPermissionForStateChange(nextState); String permission = getPermissionForStateChange(nextState);
try { if(permission != null) {
userRealm = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); try {
if(userRealm != null && userRealm.getAuthorizationManager() != null && userRealm = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId);
userRealm.getAuthorizationManager().isUserAuthorized(username, if (userRealm != null && userRealm.getAuthorizationManager() != null &&
PermissionUtils.getAbsolutePermissionPath(permission), userRealm.getAuthorizationManager().isUserAuthorized(username,
Constants.UI_EXECUTE)){ PermissionUtils.getAbsolutePermissionPath(permission),
if (currentState.equalsIgnoreCase(nextState)) { Constants.UI_EXECUTE)) {
return true; if (currentState.equalsIgnoreCase(nextState)) {
} return true;
State state = getMatchingState(currentState); }
if (state != null) { State state = getMatchingState(currentState);
return getMatchingNextState(state.getProceedingStates(), nextState); if (state != null) {
return getMatchingNextState(state.getProceedingStates(), nextState);
}
return false;
} }
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; }else{
} catch (UserStoreException e) { throw new LifecycleManagementException(
throw new LifecycleManagementException ( "Required permissions cannot be found for the state : "+nextState);
"UserStoreException exception from changing the state from : " + currentState + " to: "
+ nextState+" with username : "+ username+" and tenant Id : "+tenantId, e);
} }
} }

@ -3,6 +3,7 @@ package org.wso2.carbon.device.application.mgt.core;
import org.junit.Assert; import org.junit.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; 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.Configuration;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger;
@ -22,7 +23,7 @@ public class LifecycleManagementTest {
@BeforeClass @BeforeClass
public void init() { public void init() throws LifecycleManagementException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance(); ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Configuration configuration = configurationManager.getConfiguration(); Configuration configuration = configurationManager.getConfiguration();
lifecycleStates = configuration.getLifecycleStates(); lifecycleStates = configuration.getLifecycleStates();
@ -44,16 +45,4 @@ public class LifecycleManagementTest {
proceedingStates.contains(BOGUS_STATE.toUpperCase())); 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,));
}*/
} }

@ -80,9 +80,13 @@ public class PermissionUtils {
} }
public static boolean putPermission(Permission permission) throws PermissionManagementException { public static boolean putPermission(Permission permission) throws PermissionManagementException {
return putPermission(permission.getPath());
}
public static boolean putPermission(String permissionPath) throws PermissionManagementException {
boolean status; boolean status;
try { try {
StringTokenizer tokenizer = new StringTokenizer(permission.getPath(), "/"); StringTokenizer tokenizer = new StringTokenizer(permissionPath, "/");
String lastToken = "", currentToken, tempPath; String lastToken = "", currentToken, tempPath;
while (tokenizer.hasMoreTokens()) { while (tokenizer.hasMoreTokens()) {
currentToken = tokenizer.nextToken(); currentToken = tokenizer.nextToken();
@ -95,7 +99,7 @@ public class PermissionUtils {
status = true; status = true;
} catch (RegistryException e) { } catch (RegistryException e) {
throw new PermissionManagementException("Error occurred while persisting permission : " + throw new PermissionManagementException("Error occurred while persisting permission : " +
permission.getName(), e); permissionPath, e);
} }
return status; return status;
} }

Loading…
Cancel
Save