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 @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.organization.resource.sharing.policy.management;

import org.apache.commons.lang.NotImplementedException;
BimsaraBodaragama marked this conversation as resolved.
Show resolved Hide resolved
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceType;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.SharedAttributeType;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.exception.ResourceSharingPolicyMgtException;
Expand Down Expand Up @@ -56,7 +57,7 @@ int addResourceSharingPolicy(ResourceSharingPolicy resourceSharingPolicy)
* @param resourceSharingPolicyId The unique identifier of the resource sharing policy to be retrieved.
* Must be a valid ID greater than zero.
* @return An {@link Optional} containing the {@link ResourceSharingPolicy} if found,
* or an empty {@link Optional} if no matching resource sharing policy exists.
* or an empty {@link Optional} if no matching resource sharing policy exists.
* @throws ResourceSharingPolicyMgtException If an error occurs while retrieving the resource sharing policy.
*/
Optional<ResourceSharingPolicy> getResourceSharingPolicyById(int resourceSharingPolicyId)
Expand Down Expand Up @@ -120,7 +121,7 @@ void deleteResourceSharingPolicyRecordById(int resourceSharingPolicyId, String s
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the resource sharing policy.
*/
void deleteResourceSharingPolicyByResourceTypeAndId(ResourceType resourceType, String resourceId,
String sharingPolicyInitiatedOrgId)
String sharingPolicyInitiatedOrgId)
throws ResourceSharingPolicyMgtException;

/**
Expand Down Expand Up @@ -204,8 +205,8 @@ List<SharedResourceAttribute> getSharedResourceAttributesByTypeAndId(SharedAttri
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the shared resource attributes.
*/
void deleteSharedResourceAttributesByResourceSharingPolicyId(int resourceSharingPolicyId,
SharedAttributeType sharedAttributeType,
String sharingPolicyInitiatedOrgId)
SharedAttributeType sharedAttributeType,
String sharingPolicyInitiatedOrgId)
throws ResourceSharingPolicyMgtException;

/**
Expand All @@ -220,17 +221,17 @@ void deleteSharedResourceAttributesByResourceSharingPolicyId(int resourceSharing
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the shared resource attribute.
*/
void deleteSharedResourceAttributeByAttributeTypeAndId(SharedAttributeType attributeType, String attributeId,
String sharingPolicyInitiatedOrgId)
String sharingPolicyInitiatedOrgId)
throws ResourceSharingPolicyMgtException;

/**
* Adds a resource sharing policy along with its associated shared resource attributes in a single transaction.
*
* @param resourceSharingPolicy The {@link ResourceSharingPolicy} containing details such as resource type,
* initiating organization, policy holding organization, and sharing policy.
* Must not be {@code null}.
* @param sharedResourceAttributes A list of {@link SharedResourceAttribute} objects associated with the resource
* sharing policy. Must not be {@code null} or empty.
* @param resourceSharingPolicy The {@link ResourceSharingPolicy} containing details such as resource type,
* initiating organization, policy holding organization, and sharing policy.
* Must not be {@code null}.
* @param sharedResourceAttributes A list of {@link SharedResourceAttribute} objects associated with the resource
* sharing policy. Must not be {@code null} or empty.
* @return {@code true} if both the resource sharing policy and the shared resource attributes were added
* successfully.
* @throws ResourceSharingPolicyMgtException If an error occurs while adding the resource sharing policy or the
Expand Down Expand Up @@ -258,4 +259,57 @@ 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.
* 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.
*
* @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.
*/
default void deleteResourceSharingPolicyByResourceTypeAndId(ResourceType resourceType, String resourceId)
throws ResourceSharingPolicyMgtException {

throw new NotImplementedException(
"deleteResourceSharingPolicyByResourceTypeAndId method is not implemented in " + this.getClass());
}

/**
* Deletes a shared resource attribute based on its attribute type and unique identifier.
* 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.
*
* @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.
*/
default void deleteSharedResourceAttributeByAttributeTypeAndId(SharedAttributeType attributeType,
String attributeId)
throws ResourceSharingPolicyMgtException {

throw new NotImplementedException(
"deleteSharedResourceAttributeByAttributeTypeAndId method is not implemented in " + this.getClass());
}

/**
* Deletes all resource sharing policies and shared resource attributes associated with a given organization.
* 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.
*
* @param organizationId The unique identifier of the organization being deleted.
* @throws ResourceSharingPolicyMgtException If an error occurs while deleting the resource sharing policies or
* attributes.
*/
default void deleteResourceSharingPoliciesAndAttributesByOrganizationId(String organizationId)
throws ResourceSharingPolicyMgtException {

throw new NotImplementedException(
"deleteResourceSharingPoliciesAndAttributesByOrganizationId method is not implemented in " +
this.getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,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 @@ -146,6 +146,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
Loading
Loading