Add new method to put permission taking String parameters

merge-requests/61/head
Gathika94 6 years ago
parent 6ba46facb1
commit 3bb2e8a5bf

@ -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);

@ -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<String, State> lifecycleStates;
private static Log log = LogFactory.getLog(LifecycleStateManger.class);
public void init(List<LifecycleState> 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,12 +57,13 @@ public class LifecycleStateManger {
UserRealm userRealm = null;
String permission = getPermissionForStateChange(nextState);
if(permission != null) {
try {
userRealm = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId);
if(userRealm != null && userRealm.getAuthorizationManager() != null &&
if (userRealm != null && userRealm.getAuthorizationManager() != null &&
userRealm.getAuthorizationManager().isUserAuthorized(username,
PermissionUtils.getAbsolutePermissionPath(permission),
Constants.UI_EXECUTE)){
Constants.UI_EXECUTE)) {
if (currentState.equalsIgnoreCase(nextState)) {
return true;
}
@ -71,9 +75,13 @@ public class LifecycleStateManger {
}
return false;
} catch (UserStoreException e) {
throw new LifecycleManagementException (
throw new LifecycleManagementException(
"UserStoreException exception from changing the state from : " + currentState + " to: "
+ nextState+" with username : "+ username+" and tenant Id : "+tenantId, e);
+ nextState + " with username : " + username + " and tenant Id : " + tenantId, e);
}
}else{
throw new LifecycleManagementException(
"Required permissions cannot be found for the state : "+nextState);
}
}

@ -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,));
}*/
}

@ -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;
}

Loading…
Cancel
Save