Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the Cleanup of Resource Sharing Policies and Attributes During Resource, Attribute, and Organization Deletion. #422

Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,49 @@ boolean addResourceSharingPolicyWithAttributes(ResourceSharingPolicy resourceSha
Map<String, Map<ResourceSharingPolicy, List<SharedResourceAttribute>>>
getResourceSharingPoliciesWithSharedAttributes(List<String> policyHoldingOrganizationIds)
throws ResourceSharingPolicyMgtException;

/**
* Deletes a resource sharing policy based on its resource type and resource ID.
* <p>
* This method should only be used when a resource (e.g., user) is being deleted independently of the policies.
* It ensures that all related resource sharing policies associated with the given resource are also deleted
* as part of the resource deletion process.
* </p>
BimsaraBodaragama marked this conversation as resolved.
Show resolved Hide resolved
*
* @param resourceType The {@link ResourceType} of the resource.
* @param resourceId The unique identifier of the resource whose sharing policy is to be deleted.
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the resource sharing policy.
*/
void deleteResourceSharingPolicyByResourceTypeAndId(ResourceType resourceType, String resourceId)
throws ResourceSharingPolicyMgtException;

/**
* Deletes a shared resource attribute based on its attribute type and unique identifier.
* <p>
* This method should only be used when a resource (e.g., roles) is being deleted independently of the policies.
* It ensures that all corresponding shared resource attributes associated with the given attribute are also
* deleted as part of the attribute deletion process.
* </p>
*
* @param attributeType The {@link SharedAttributeType} of the attribute to be deleted.
* @param attributeId The unique identifier of the attribute to be deleted.
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the shared resource attribute.
*/
void deleteSharedResourceAttributeByAttributeTypeAndId(SharedAttributeType attributeType, String attributeId)
throws ResourceSharingPolicyMgtException;

/**
* Deletes all resource sharing policies and shared resource attributes associated with a given organization.
* <p>
* This method should be called when an organization is being deleted. It ensures that all resource sharing
* policies and corresponding shared resource attributes related to the specified organization are also deleted
* as part of the organization's deletion process.
* </p>
*
* @param organizationId The unique identifier of the organization being deleted.
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the resource sharing policies or
* attributes.
*/
void deleteResourceSharingPoliciesAndAttributesByOrganizationId(String organizationId)
throws ResourceSharingPolicyMgtException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ public boolean addResourceSharingPolicyWithAttributes(ResourceSharingPolicy reso
policyHoldingOrganizationIds);
}

@Override
public void deleteResourceSharingPolicyByResourceTypeAndId(ResourceType resourceType, String resourceId)
throws ResourceSharingPolicyMgtException {

RESOURCE_SHARING_POLICY_HANDLER_DAO.deleteResourceSharingPolicyByResourceTypeAndId(resourceType, resourceId);
}

@Override
public void deleteSharedResourceAttributeByAttributeTypeAndId(SharedAttributeType attributeType, String attributeId)
throws ResourceSharingPolicyMgtException {

RESOURCE_SHARING_POLICY_HANDLER_DAO.deleteSharedResourceAttributeByAttributeTypeAndId(attributeType,
attributeId);
}

@Override
public void deleteResourceSharingPoliciesAndAttributesByOrganizationId(String organizationId)
throws ResourceSharingPolicyMgtException {

RESOURCE_SHARING_POLICY_HANDLER_DAO.deleteResourceSharingPoliciesAndAttributesByOrganizationId(organizationId);
}

private boolean isValidAttributeForTheResource(ResourceSharingPolicy resourceSharingPolicy,
SharedResourceAttribute sharedResourceAttribute) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ public class ResourceSharingSQLConstants {
"UM_INITIATING_ORG_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_INITIATING_ORG_ID + ";)";

// SQL for deleting resource sharing policy by resource type and ID at Resource deletion.
public static final String DELETE_RESOURCE_SHARING_POLICY_BY_RESOURCE_TYPE_AND_ID_AT_RESOURCE_DELETION =
"DELETE FROM UM_RESOURCE_SHARING_POLICY WHERE " +
"UM_RESOURCE_TYPE = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_RESOURCE_TYPE + "; AND " +
"UM_RESOURCE_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_RESOURCE_ID + ";";

// SQL for deleting shared resource attribute by attribute type and ID at Attribute deletion.
public static final String DELETE_SHARED_RESOURCE_ATTRIBUTE_BY_ATTRIBUTE_TYPE_AND_ID_AT_ATTRIBUTE_DELETION =
"DELETE FROM UM_SHARED_RESOURCE_ATTRIBUTES WHERE " +
"UM_SHARED_ATTRIBUTE_TYPE = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARED_ATTRIBUTE_TYPE + "; AND " +
"UM_SHARED_ATTRIBUTE_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARED_ATTRIBUTE_ID + ";";

// SQL for deleting resource sharing policy by org ID.
public static final String DELETE_RESOURCE_SHARING_POLICY_BY_ORG_ID_AT_ATTRIBUTE_DELETION =
"DELETE FROM UM_RESOURCE_SHARING_POLICY WHERE " +
"UM_INITIATING_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_INITIATING_ORG_ID + "; OR " +
"UM_POLICY_HOLDING_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_POLICY_HOLDING_ORG_ID + ";";

private ResourceSharingSQLConstants() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,54 @@ boolean addResourceSharingPolicyWithAttributes(ResourceSharingPolicy resourceSha
Map<String, Map<ResourceSharingPolicy, List<SharedResourceAttribute>>>
getResourceSharingPoliciesWithSharedAttributes(List<String> policyHoldingOrganizationIds)
throws ResourceSharingPolicyMgtServerException;

/**
* Deletes a resource sharing policy based on its resource type and resource ID.
* <p>
* This method is intended to be used when a resource (e.g., user) is being deleted independently of the policies.
* It ensures that all related resource sharing policies associated with the specified resource are deleted
* as part of the resource deletion process.
* </p>
*
* @param resourceType The {@link ResourceType} of the resource whose policy needs to be deleted.
* Must not be {@code null}.
* @param resourceId The unique identifier of the resource whose sharing policy is to be deleted.
* Must not be {@code null} or empty.
* @throws ResourceSharingPolicyMgtServerException If an error occurs while deleting the resource sharing policy.
*/
void deleteResourceSharingPolicyByResourceTypeAndId(ResourceType resourceType, String resourceId)
throws ResourceSharingPolicyMgtServerException;

/**
* Deletes a shared resource attribute based on its attribute type and unique identifier.
* <p>
* This method is intended to be used when a resource (e.g., roles) is being deleted independently of the policies.
* It ensures that all corresponding shared resource attributes associated with the specified attribute are deleted
* as part of the attribute deletion process.
* </p>
*
* @param attributeType The {@link SharedAttributeType} of the attribute to be deleted.
* Must not be {@code null}.
* @param attributeId The unique identifier of the attribute to be deleted.
* Must not be {@code null} or empty.
* @throws ResourceSharingPolicyMgtServerException If an error occurs while deleting the shared resource attribute.
*/
void deleteSharedResourceAttributeByAttributeTypeAndId(SharedAttributeType attributeType, String attributeId)
throws ResourceSharingPolicyMgtServerException;

/**
* Deletes all resource sharing policies and shared resource attributes associated with a given organization.
* <p>
* This method is intended to be used when an organization is being deleted. It ensures that all resource sharing
* policies and corresponding shared resource attributes related to the specified organization are also deleted
* as part of the organization's deletion process.
* </p>
*
* @param organizationId The unique identifier of the organization whose policies and attributes need to be deleted.
* Must not be {@code null} or empty.
* @throws ResourceSharingPolicyMgtServerException If an error occurs while deleting the resource sharing policies
* or attributes.
*/
void deleteResourceSharingPoliciesAndAttributesByOrganizationId(String organizationId)
throws ResourceSharingPolicyMgtServerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.CREATE_RESOURCE_SHARING_POLICY;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.CREATE_SHARED_RESOURCE_ATTRIBUTE;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_RESOURCE_SHARING_POLICY;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_RESOURCE_SHARING_POLICY_BY_ORG_ID_AT_ATTRIBUTE_DELETION;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_RESOURCE_SHARING_POLICY_BY_RESOURCE_TYPE_AND_ID;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_RESOURCE_SHARING_POLICY_BY_RESOURCE_TYPE_AND_ID_AT_RESOURCE_DELETION;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_SHARED_RESOURCE_ATTRIBUTE;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_SHARED_RESOURCE_ATTRIBUTE_BY_ATTRIBUTE_TYPE_AND_ID;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.DELETE_SHARED_RESOURCE_ATTRIBUTE_BY_ATTRIBUTE_TYPE_AND_ID_AT_ATTRIBUTE_DELETION;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.GET_RESOURCE_SHARING_POLICIES_BY_ORG_IDS_HEAD;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.GET_RESOURCE_SHARING_POLICIES_WITH_SHARED_ATTRIBUTES_BY_POLICY_HOLDING_ORGS_HEAD;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceSharingSQLConstants.GET_RESOURCE_SHARING_POLICY_BY_ID;
Expand Down Expand Up @@ -361,6 +364,60 @@ public boolean addResourceSharingPolicyWithAttributes(ResourceSharingPolicy reso
}
}

@Override
public void deleteResourceSharingPolicyByResourceTypeAndId(ResourceType resourceType, String resourceId)
throws ResourceSharingPolicyMgtServerException {

NamedJdbcTemplate namedJdbcTemplate = getNewTemplate();
try {
namedJdbcTemplate.executeUpdate(DELETE_RESOURCE_SHARING_POLICY_BY_RESOURCE_TYPE_AND_ID_AT_RESOURCE_DELETION,
namedPreparedStatement -> {
namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_RESOURCE_TYPE,
resourceType.name());
namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_RESOURCE_ID,
resourceId);
});
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_RESOURCE_SHARING_POLICY_DELETION_BY_RESOURCE_TYPE_AND_ID_FAILED);
}
}

@Override
public void deleteSharedResourceAttributeByAttributeTypeAndId(SharedAttributeType attributeType, String attributeId)
throws ResourceSharingPolicyMgtServerException {

NamedJdbcTemplate namedJdbcTemplate = getNewTemplate();
try {
namedJdbcTemplate.executeUpdate(
DELETE_SHARED_RESOURCE_ATTRIBUTE_BY_ATTRIBUTE_TYPE_AND_ID_AT_ATTRIBUTE_DELETION,
namedPreparedStatement -> {
namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SHARED_ATTRIBUTE_TYPE,
attributeType.name());
namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SHARED_ATTRIBUTE_ID,
attributeId);
});
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_SHARED_RESOURCE_ATTRIBUTE_DELETION_BY_ATTRIBUTE_TYPE_AND_ID_FAILED);
}
}

@Override
public void deleteResourceSharingPoliciesAndAttributesByOrganizationId(String organizationId)
throws ResourceSharingPolicyMgtServerException {

NamedJdbcTemplate namedJdbcTemplate = getNewTemplate();
try {
namedJdbcTemplate.executeUpdate(
DELETE_RESOURCE_SHARING_POLICY_BY_ORG_ID_AT_ATTRIBUTE_DELETION,
namedPreparedStatement -> {
namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_INITIATING_ORG_ID, organizationId);
namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_POLICY_HOLDING_ORG_ID, organizationId);
});
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_RESOURCE_SHARING_POLICY_DELETION_FAILED);
}
}

private List<SharedResourceAttribute> getSharedResourceAttributes(String query, SharedAttributeType attributeType,
String attributeId)
throws ResourceSharingPolicyMgtServerException {
Expand Down
Loading
Loading