Skip to content

Commit

Permalink
Merge pull request #5808 from ashanthamara/actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanthamara authored Jul 23, 2024
2 parents c19536d + e2494eb commit 46fa0ab
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.wso2.carbon.identity.action.management;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.action.management.constant.ActionMgtConstants;
Expand Down Expand Up @@ -88,9 +87,9 @@ public Action updateAction(String actionType, String actionId, Action action, St
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Updating Action for Action Type: %s and Action ID: %s.", actionType, actionId));
}
Action existingAction = checkIfActionExists(actionId, tenantDomain);
action = mergeActionWithExisting(action, existingAction);
return CACHE_BACKED_DAO.updateAction(getActionTypeFromPath(actionType), actionId, action,
String resolvedActionType = getActionTypeFromPath(actionType);
Action existingAction = checkIfActionExists(resolvedActionType, actionId, tenantDomain);
return CACHE_BACKED_DAO.updateAction(resolvedActionType, actionId, action, existingAction,
IdentityTenantUtil.getTenantId(tenantDomain));
}

Expand All @@ -100,8 +99,9 @@ public void deleteAction(String actionType, String actionId, String tenantDomain
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Deleting Action for Action Type: %s and Action ID: %s", actionType, actionId));
}
Action action = checkIfActionExists(actionId, tenantDomain);
CACHE_BACKED_DAO.deleteAction(getActionTypeFromPath(actionType), actionId, action,
String resolvedActionType = getActionTypeFromPath(actionType);
Action action = checkIfActionExists(resolvedActionType, actionId, tenantDomain);
CACHE_BACKED_DAO.deleteAction(resolvedActionType, actionId, action,
IdentityTenantUtil.getTenantId(tenantDomain));
}

Expand All @@ -111,8 +111,9 @@ public Action activateAction(String actionType, String actionId, String tenantDo
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Activating Action for Action Type: %s and Action ID: %s.", actionType, actionId));
}
checkIfActionExists(actionId, tenantDomain);
return CACHE_BACKED_DAO.activateAction(getActionTypeFromPath(actionType), actionId,
String resolvedActionType = getActionTypeFromPath(actionType);
checkIfActionExists(resolvedActionType, actionId, tenantDomain);
return CACHE_BACKED_DAO.activateAction(resolvedActionType, actionId,
IdentityTenantUtil.getTenantId(tenantDomain));
}

Expand All @@ -123,8 +124,9 @@ public Action deactivateAction(String actionType, String actionId, String tenant
LOG.debug(String.format("Deactivating Action for Action Type: %s and Action ID: %s.", actionType,
actionId));
}
checkIfActionExists(actionId, tenantDomain);
return CACHE_BACKED_DAO.deactivateAction(getActionTypeFromPath(actionType), actionId,
String resolvedActionType = getActionTypeFromPath(actionType);
checkIfActionExists(resolvedActionType, actionId, tenantDomain);
return CACHE_BACKED_DAO.deactivateAction(resolvedActionType, actionId,
IdentityTenantUtil.getTenantId(tenantDomain));
}

Expand All @@ -150,13 +152,14 @@ public Action getActionByActionId(String actionId, String tenantDomain) throws A
public Action updateActionEndpointAuthentication(String actionType, String actionId, AuthType authentication,
String tenantDomain) throws ActionMgtException {

Action existingAction = checkIfActionExists(actionId, tenantDomain);
String resolvedActionType = getActionTypeFromPath(actionType);
Action existingAction = checkIfActionExists(resolvedActionType, actionId, tenantDomain);
if (existingAction.getEndpoint().getAuthentication().getType().equals(authentication.getType())) {
// Only need to update the properties since the authType is same.
return updateEndpointAuthenticationProperties(actionType, actionId, authentication, tenantDomain);
return updateEndpointAuthenticationProperties(resolvedActionType, actionId, authentication, tenantDomain);
} else {
// Need to update the authentication type and properties.
return updateEndpoint(actionType, actionId, existingAction, authentication, tenantDomain);
return updateEndpoint(resolvedActionType, actionId, existingAction, authentication, tenantDomain);
}
}

Expand Down Expand Up @@ -197,41 +200,22 @@ private void validateMaxActionsPerType(String actionType, String tenantDomain) t
/**
* Check if the action exists.
*
* @param actionType Action Type.
* @param actionId Action ID.
* @param tenantDomain Tenant Domain.
* @throws ActionMgtException If the action does not exist.
*/
private Action checkIfActionExists(String actionId, String tenantDomain) throws ActionMgtException {
private Action checkIfActionExists(String actionType, String actionId, String tenantDomain)
throws ActionMgtException {

Action action = CACHE_BACKED_DAO.getActionByActionId(actionId, IdentityTenantUtil.getTenantId(tenantDomain));
if (action == null) {
if (action == null || !actionType.equals(action.getType().name())) {
throw ActionManagementUtil.handleClientException(
ActionMgtConstants.ErrorMessages.ERROR_NO_ACTION_CONFIGURED_ON_GIVEN_ID);
ActionMgtConstants.ErrorMessages.ERROR_NO_ACTION_CONFIGURED_ON_GIVEN_ACTION_TYPE_AND_ID);
}
return action;
}

/**
* Merge the updating action with the existing action.
*
* @param updatingAction Action object with updating information.
* @param existingAction Action object with existing information.
* @return Action object with merged information.
*/
private Action mergeActionWithExisting(Action updatingAction, Action existingAction) {

return new Action.ActionRequestBuilder()
.name(StringUtils.isEmpty(updatingAction.getName()) ? existingAction.getName() :
updatingAction.getName())
.description(StringUtils.isEmpty(updatingAction.getDescription()) ? existingAction.getDescription() :
updatingAction.getDescription())
.endpoint(new EndpointConfig.EndpointConfigBuilder()
.uri(StringUtils.isEmpty(updatingAction.getEndpoint().getUri()) ?
existingAction.getEndpoint().getUri() : updatingAction.getEndpoint().getUri())
.build())
.build();
}

/**
* Update the authentication type and properties of the action endpoint.
*
Expand All @@ -254,7 +238,7 @@ private Action updateEndpoint(String actionType, String actionId, Action existin
EndpointConfig endpoint = new EndpointConfig.EndpointConfigBuilder()
.uri(existingAction.getEndpoint().getUri())
.authentication(authentication).build();
return CACHE_BACKED_DAO.updateActionEndpoint(getActionTypeFromPath(actionType), actionId, endpoint,
return CACHE_BACKED_DAO.updateActionEndpoint(actionType, actionId, endpoint,
existingAction.getEndpoint().getAuthentication(), IdentityTenantUtil.getTenantId(tenantDomain));
}

Expand All @@ -277,6 +261,5 @@ private Action updateEndpointAuthenticationProperties(String actionType, String
}
return CACHE_BACKED_DAO.updateActionEndpointAuthProperties(actionId, authentication,
IdentityTenantUtil.getTenantId(tenantDomain));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public enum ErrorMessages {
"Invalid action type used for path parameter."),
ERROR_MAXIMUM_ACTIONS_PER_ACTION_TYPE_REACHED("60002", "Unable to create an Action.",
"Maximum number of actions per action type is reached."),
ERROR_NO_ACTION_CONFIGURED_ON_GIVEN_ID("60003", "Unable to perform the operation.",
"No Action is configured on the given action Id."),
ERROR_NO_ACTION_CONFIGURED_ON_GIVEN_ACTION_TYPE_AND_ID("60003",
"Unable to perform the operation.",
"No Action is configured on the given Action Type and Id."),

// Server errors.
ERROR_WHILE_ADDING_ACTION("65001", "Error while adding Action.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ public interface ActionManagementDAO {
/**
* Update {@link Action} by given Action type and Action ID.
*
* @param actionType Action Type.
* @param actionId Action ID.
* @param action Action update model.
* @param tenantId Tenant Id.
* @param actionType Action Type.
* @param actionId Action ID.
* @param updatingAction Action update model.
* @param existingAction Existing Action.
* @param tenantId Tenant Id.
* @return Updated <code>Action</code>.
* @throws ActionMgtException If an error occurs while updating the Action.
*/
Action updateAction(String actionType, String actionId, Action action, Integer tenantId) throws ActionMgtException;
Action updateAction(String actionType, String actionId, Action updatingAction, Action existingAction,
Integer tenantId) throws ActionMgtException;

/**
* Delete {@link Action} by given Action Type.
Expand Down
Loading

0 comments on commit 46fa0ab

Please sign in to comment.