|
|
|
@ -1,6 +1,15 @@
|
|
|
|
|
package org.wso2.carbon.device.application.mgt.core.lifecycle;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.permission.mgt.Permission;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.search.mgt.Constants;
|
|
|
|
|
import org.wso2.carbon.user.api.UserRealm;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
@ -15,14 +24,24 @@ public class LifecycleStateManger {
|
|
|
|
|
|
|
|
|
|
private Map<String, State> lifecycleStates;
|
|
|
|
|
|
|
|
|
|
public void init(List<LifecycleState> states){
|
|
|
|
|
public void init(List<LifecycleState> states) throws LifecycleManagementException {
|
|
|
|
|
lifecycleStates = new HashMap<>();
|
|
|
|
|
for (LifecycleState s : states) {
|
|
|
|
|
if (s.getProceedingStates() != null) {
|
|
|
|
|
s.getProceedingStates().replaceAll(String::toUpperCase);
|
|
|
|
|
}
|
|
|
|
|
lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(), s.getProceedingStates()));
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
} catch (PermissionManagementException e) {
|
|
|
|
|
throw new LifecycleManagementException (
|
|
|
|
|
"Error when adding permission " + s.getPermission() + " related to the state: "
|
|
|
|
|
+ s.getName(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -30,16 +49,32 @@ public class LifecycleStateManger {
|
|
|
|
|
return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isValidStateChange(String currentState, String nextState) {
|
|
|
|
|
if (currentState.equalsIgnoreCase(nextState)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public boolean isValidStateChange(String currentState, String nextState, String username,
|
|
|
|
|
int tenantId) throws LifecycleManagementException {
|
|
|
|
|
|
|
|
|
|
State state = getMatchingState(currentState);
|
|
|
|
|
if (state != null) {
|
|
|
|
|
return getMatchingNextState(state.getProceedingStates(), nextState);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private State getMatchingState(String currentState) {
|
|
|
|
@ -54,8 +89,8 @@ public class LifecycleStateManger {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean getMatchingNextState(Set<String> proceedingStates, String nextState) {
|
|
|
|
|
|
|
|
|
|
private boolean getMatchingNextState(Set<String> proceedingStates, String nextState) {
|
|
|
|
|
for (String state: proceedingStates) {
|
|
|
|
|
if (state.equalsIgnoreCase(nextState)) {
|
|
|
|
|
return true;
|
|
|
|
@ -63,4 +98,18 @@ public class LifecycleStateManger {
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getPermissionForStateChange(String nextState){
|
|
|
|
|
Iterator it = lifecycleStates.entrySet().iterator();
|
|
|
|
|
State nextLifecycleState;
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
Map.Entry pair = (Map.Entry)it.next();
|
|
|
|
|
if(pair.getKey().toString().equalsIgnoreCase(nextState)) {
|
|
|
|
|
nextLifecycleState = lifecycleStates.get(nextState);
|
|
|
|
|
return nextLifecycleState.getPermission();
|
|
|
|
|
}
|
|
|
|
|
it.remove();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|