Skip to content

Commit

Permalink
Merge pull request #12748 from chamilaadhi/org_visibility_remove_poli…
Browse files Browse the repository at this point in the history
…cy_org

Remove org visibilty in business policy
  • Loading branch information
chamilaadhi authored Jan 17, 2025
2 parents 43b2f8d + 40ba285 commit 5741008
Show file tree
Hide file tree
Showing 14 changed files with 11 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

package org.wso2.carbon.apimgt.api.model.policy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubscriptionPolicy extends Policy {
Expand All @@ -36,7 +34,6 @@ public class SubscriptionPolicy extends Policy {
private String tierQuotaType;
private int graphQLMaxDepth;
private int graphQLMaxComplexity;
private List<String> allowedOrganizations = new ArrayList<String>();

public SubscriptionPolicy(String name) {
super(name);
Expand Down Expand Up @@ -125,11 +122,11 @@ public void setGraphQLMaxComplexity(int graphQLMaxComplexity) {
@Override
public String toString() {
return "SubscriptionPolicy [rateLimitCount=" + rateLimitCount + ", rateLimitTimeUnit=" + rateLimitTimeUnit
+ ", subscriberCount=" + subscriberCount + ", customAttributes=" + Arrays.toString(customAttributes)
+ ", stopOnQuotaReach=" + stopOnQuotaReach + ", billingPlan=" + billingPlan + ", monetizationPlan="
+ monetizationPlan + ", monetizationPlanProperties=" + monetizationPlanProperties + ", tierQuotaType="
+ tierQuotaType + ", graphQLMaxDepth=" + graphQLMaxDepth + ", graphQLMaxComplexity="
+ graphQLMaxComplexity + ", allowedOrganizations=" + allowedOrganizations + "]";
+ ", customAttributes=" + Arrays.toString(customAttributes) + ", stopOnQuotaReach=" + stopOnQuotaReach
+ ", billingPlan=" + billingPlan + ", monetizationPlan=" + monetizationPlan
+ ", monetizationPlanProperties=" + monetizationPlanProperties + ", tierQuotaType=" + tierQuotaType
+ ", maxDepth=" + graphQLMaxDepth + ", maxComplexity=" + graphQLMaxComplexity
+ ", subscriberCount= " + subscriberCount + "]";
}

public int getSubscriberCount() {
Expand All @@ -139,12 +136,4 @@ public int getSubscriberCount() {
public void setSubscriberCount(int subscriberCount) {
this.subscriberCount = subscriberCount;
}

public List<String> getAllowedOrganizations() {
return allowedOrganizations;
}

public void setAllowedOrganizations(List<String> allowedOrganizations) {
this.allowedOrganizations = allowedOrganizations;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,6 @@ public enum RegistryResourceTypesForUI {
public static final String API_POLICY_API_LEVEL = "apiLevel";

public static final String BILLING_PLAN_FREE = "FREE";
public static final String ALLOWED_ORGANIZATIONS_DEFAULT = "ALL";
public static final String DEFAULT_VISIBLE_ORG = "all";
public static final String POLICY_RESET = "reset";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4036,7 +4036,6 @@ private API addTiersToAPI(API api, String organization) throws APIManagementExce

int tenantId = APIUtil.getInternalIdFromTenantDomainOrOrganization(organization);
Set<Tier> tierNames = api.getAvailableTiers();
apiMgtDAO.getSubscriptionPolicies(tenantId);
Map<String, Tier> definedTiers = APIUtil.getTiers(tenantId);

Set<Tier> availableTiers = new HashSet<Tier>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,5 @@ public class ThrottlePolicyConstants {
public static final String COLUMN_MAX_DEPTH = "MAX_DEPTH";

public static final String COLUMN_MAX_COMPLEXITY = "MAX_COMPLEXITY";

public static final String COLUMN_ALLOWED_ORGANIZATIONS = "ALLOWED_ORGANIZATIONS";

}
Original file line number Diff line number Diff line change
Expand Up @@ -11812,18 +11812,6 @@ public void addSubscriptionPolicy(SubscriptionPolicy policy) throws APIManagemen
policyStatement.setInt(26, policy.getSubscriberCount());
}

List<String> allowedOrgs = policy.getAllowedOrganizations();
if (allowedOrgs != null && !allowedOrgs.isEmpty()) {
try (PreparedStatement addVisibleOrgsStatement = conn
.prepareStatement(SQLConstants.PolicyOrgVisibilitySqlConstants.ADD_POLICY_ORG_VISIBILITY_SQL)) {
for (String org : allowedOrgs) {
addVisibleOrgsStatement.setString(1, policy.getUUID());
addVisibleOrgsStatement.setString(2, org);
addVisibleOrgsStatement.addBatch();
}
addVisibleOrgsStatement.executeBatch();
}
}
policyStatement.executeUpdate();
conn.commit();
} catch (SQLIntegrityConstraintViolationException e) {
Expand Down Expand Up @@ -12537,7 +12525,6 @@ public SubscriptionPolicy[] getSubscriptionPolicies(int tenantID) throws APIMana
monetizationPlanProperties.put(APIConstants.Monetization.CURRENCY,
rs.getString(ThrottlePolicyConstants.COLUMN_CURRENCY));
subPolicy.setMonetizationPlanProperties(monetizationPlanProperties);
subPolicy.setAllowedOrganizations(getPolicyVisibleOrgs(subPolicy.getUUID()));
InputStream binary = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
byte[] customAttrib = APIUtil.toByteArray(binary);
Expand Down Expand Up @@ -12965,7 +12952,6 @@ public SubscriptionPolicy getSubscriptionPolicy(String policyName, int tenantId)
policy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
policy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
policy.setSubscriberCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONNECTION_COUNT));
policy.setAllowedOrganizations(getPolicyVisibleOrgs(policy.getUUID()));
InputStream binary = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
byte[] customAttrib = APIUtil.toByteArray(binary);
Expand Down Expand Up @@ -13018,7 +13004,6 @@ public SubscriptionPolicy getSubscriptionPolicyByUUID(String uuid) throws APIMan
policy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
policy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
policy.setSubscriberCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONNECTION_COUNT));
policy.setAllowedOrganizations(getPolicyVisibleOrgs(policy.getUUID()));

InputStream binary = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
Expand Down Expand Up @@ -13050,29 +13035,6 @@ public SubscriptionPolicy getSubscriptionPolicyByUUID(String uuid) throws APIMan
return policy;
}

public List<String> getPolicyVisibleOrgs(String policyUUID) throws APIManagementException {
List<String> orgList = new ArrayList<String>();
try (Connection conn = APIMgtDBUtil.getConnection()) {
try {
String getPolicyPermissionQuery = SQLConstants.PolicyOrgVisibilitySqlConstants.GET_POLICY_ORG_VISIBILITY_SQL;
PreparedStatement ps = conn.prepareStatement(getPolicyPermissionQuery);
ps.setString(1, policyUUID);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
orgList.add(resultSet.getString(ThrottlePolicyConstants.COLUMN_ALLOWED_ORGANIZATIONS));
}
} catch (SQLException e) {
conn.rollback();
handleException("Failed to get policy allowed organizations " + policyUUID,
e);
}
} catch (SQLException e) {
throw new APIManagementException(
"Error while retrieving policy organizations with id " + policyUUID, e);
}
return orgList;
}

/**
* Retrieves list of pipelines for the policy with policy Id: <code>policyId</code>
*
Expand Down Expand Up @@ -13558,23 +13520,6 @@ public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManage
}
updateStatement.executeUpdate();

try (PreparedStatement deleteOrgsStatement = connection.prepareStatement(SQLConstants
.PolicyOrgVisibilitySqlConstants.DELETE_ALL_POLICY_ORG_VISIBILITY_SQL)) {
deleteOrgsStatement.setString(1, policy.getUUID());
deleteOrgsStatement.executeUpdate();
}
List<String> allowedOrgs = policy.getAllowedOrganizations();
if (allowedOrgs != null && !allowedOrgs.isEmpty()) {
try (PreparedStatement addOrgsStatement = connection.prepareStatement(SQLConstants
.PolicyOrgVisibilitySqlConstants.ADD_POLICY_ORG_VISIBILITY_SQL)) {
for (String org : allowedOrgs) {
addOrgsStatement.setString(1, policy.getUUID());
addOrgsStatement.setString(2, org);
addOrgsStatement.addBatch();
}
addOrgsStatement.executeBatch();
}
}
connection.commit();
} catch (SQLException e) {
if (connection != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3867,21 +3867,6 @@ public static class KeyManagerOrgVisibilitySqlConstants {
" WHERE KEY_MANAGER_UUID = ?";
}

public static class PolicyOrgVisibilitySqlConstants {

public static final String ADD_POLICY_ORG_VISIBILITY_SQL =
" INSERT INTO" +
" AM_POLICY_ALLOWED_ORGS (POLICY_UUID, ALLOWED_ORGANIZATIONS)" +
" VALUES(?, ?)";

public static final String DELETE_ALL_POLICY_ORG_VISIBILITY_SQL = "DELETE FROM AM_POLICY_ALLOWED_ORGS" +
" WHERE POLICY_UUID = ?";

public static final String GET_POLICY_ORG_VISIBILITY_SQL =
"SELECT ALLOWED_ORGANIZATIONS" +
" FROM AM_POLICY_ALLOWED_ORGS " +
" WHERE POLICY_UUID = ?";
}
/**
* Static class to hold database queries related to AM_TENANT_THEMES table
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10839,23 +10839,6 @@ public static List<ApplicationInfoKeyManager> getPaginatedApplicationList(
return applications.subList(offset, endIndex);
}

public static Set<Tier> getAllowedTiersForTheOrganization(Set<Tier> tiers, String organization,
String superOrganization) throws APIManagementException {
int tenantId = APIUtil.getInternalIdFromTenantDomainOrOrganization(superOrganization);
SubscriptionPolicy[] policies = ApiMgtDAO.getInstance().getSubscriptionPolicies(tenantId);
Set<Tier> allowedTiers = new HashSet<Tier>();
for (Tier tier : tiers) {
for (SubscriptionPolicy policy : policies) {
if (policy.getPolicyName().equals(tier.getName())
&& (policy.getAllowedOrganizations().isEmpty()
|| policy.getAllowedOrganizations().contains(organization))) {
allowedTiers.add(tier);
}
}
}
return allowedTiers;
}

public static String getAPIMVersion() {
return CarbonUtils.getServerConfiguration().getFirstProperty("Version");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class SubscriptionThrottlePolicyAllOfDTO {
private Boolean stopOnQuotaReach = false;
private String billingPlan = null;
private SubscriptionThrottlePolicyPermissionDTO permissions = null;
private List<String> allowedOrganizations = new ArrayList<String>();

/**
**/
Expand Down Expand Up @@ -201,23 +200,6 @@ public void setPermissions(SubscriptionThrottlePolicyPermissionDTO permissions)
this.permissions = permissions;
}

/**
**/
public SubscriptionThrottlePolicyAllOfDTO allowedOrganizations(List<String> allowedOrganizations) {
this.allowedOrganizations = allowedOrganizations;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("allowedOrganizations")
public List<String> getAllowedOrganizations() {
return allowedOrganizations;
}
public void setAllowedOrganizations(List<String> allowedOrganizations) {
this.allowedOrganizations = allowedOrganizations;
}


@Override
public boolean equals(java.lang.Object o) {
Expand All @@ -236,13 +218,12 @@ public boolean equals(java.lang.Object o) {
Objects.equals(customAttributes, subscriptionThrottlePolicyAllOf.customAttributes) &&
Objects.equals(stopOnQuotaReach, subscriptionThrottlePolicyAllOf.stopOnQuotaReach) &&
Objects.equals(billingPlan, subscriptionThrottlePolicyAllOf.billingPlan) &&
Objects.equals(permissions, subscriptionThrottlePolicyAllOf.permissions) &&
Objects.equals(allowedOrganizations, subscriptionThrottlePolicyAllOf.allowedOrganizations);
Objects.equals(permissions, subscriptionThrottlePolicyAllOf.permissions);
}

@Override
public int hashCode() {
return Objects.hash(defaultLimit, monetization, rateLimitCount, rateLimitTimeUnit, subscriberCount, customAttributes, stopOnQuotaReach, billingPlan, permissions, allowedOrganizations);
return Objects.hash(defaultLimit, monetization, rateLimitCount, rateLimitTimeUnit, subscriberCount, customAttributes, stopOnQuotaReach, billingPlan, permissions);
}

@Override
Expand All @@ -259,7 +240,6 @@ public String toString() {
sb.append(" stopOnQuotaReach: ").append(toIndentedString(stopOnQuotaReach)).append("\n");
sb.append(" billingPlan: ").append(toIndentedString(billingPlan)).append("\n");
sb.append(" permissions: ").append(toIndentedString(permissions)).append("\n");
sb.append(" allowedOrganizations: ").append(toIndentedString(allowedOrganizations)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class SubscriptionThrottlePolicyDTO extends ThrottlePolicyDTO {
private Boolean stopOnQuotaReach = false;
private String billingPlan = null;
private SubscriptionThrottlePolicyPermissionDTO permissions = null;
private List<String> allowedOrganizations = new ArrayList<String>();

/**
* Maximum Complexity of the GraphQL query
Expand Down Expand Up @@ -242,23 +241,6 @@ public void setPermissions(SubscriptionThrottlePolicyPermissionDTO permissions)
this.permissions = permissions;
}

/**
**/
public SubscriptionThrottlePolicyDTO allowedOrganizations(List<String> allowedOrganizations) {
this.allowedOrganizations = allowedOrganizations;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("allowedOrganizations")
public List<String> getAllowedOrganizations() {
return allowedOrganizations;
}
public void setAllowedOrganizations(List<String> allowedOrganizations) {
this.allowedOrganizations = allowedOrganizations;
}


@Override
public boolean equals(java.lang.Object o) {
Expand All @@ -279,13 +261,12 @@ public boolean equals(java.lang.Object o) {
Objects.equals(customAttributes, subscriptionThrottlePolicy.customAttributes) &&
Objects.equals(stopOnQuotaReach, subscriptionThrottlePolicy.stopOnQuotaReach) &&
Objects.equals(billingPlan, subscriptionThrottlePolicy.billingPlan) &&
Objects.equals(permissions, subscriptionThrottlePolicy.permissions) &&
Objects.equals(allowedOrganizations, subscriptionThrottlePolicy.allowedOrganizations);
Objects.equals(permissions, subscriptionThrottlePolicy.permissions);
}

@Override
public int hashCode() {
return Objects.hash(graphQLMaxComplexity, graphQLMaxDepth, defaultLimit, monetization, rateLimitCount, rateLimitTimeUnit, subscriberCount, customAttributes, stopOnQuotaReach, billingPlan, permissions, allowedOrganizations);
return Objects.hash(graphQLMaxComplexity, graphQLMaxDepth, defaultLimit, monetization, rateLimitCount, rateLimitTimeUnit, subscriberCount, customAttributes, stopOnQuotaReach, billingPlan, permissions);
}

@Override
Expand All @@ -304,7 +285,6 @@ public String toString() {
sb.append(" stopOnQuotaReach: ").append(toIndentedString(stopOnQuotaReach)).append("\n");
sb.append(" billingPlan: ").append(toIndentedString(billingPlan)).append("\n");
sb.append(" permissions: ").append(toIndentedString(permissions)).append("\n");
sb.append(" allowedOrganizations: ").append(toIndentedString(allowedOrganizations)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(
policyDTO.setGraphQLMaxComplexity(subscriptionPolicy.getGraphQLMaxComplexity());
policyDTO.setGraphQLMaxDepth(subscriptionPolicy.getGraphQLMaxDepth());
policyDTO.setSubscriberCount(subscriptionPolicy.getSubscriberCount());
policyDTO.setAllowedOrganizations(subscriptionPolicy.getAllowedOrganizations());

byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
if (customAttributes != null) {
Expand Down Expand Up @@ -141,7 +140,6 @@ public static SubscriptionPolicy fromSubscriptionThrottlePolicyDTOToModel(Subscr
subscriptionPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, subscriptionPolicy);
subscriptionPolicy.setBillingPlan(dto.getBillingPlan());
subscriptionPolicy.setStopOnQuotaReach(dto.isStopOnQuotaReach());
subscriptionPolicy.setAllowedOrganizations(dto.getAllowedOrganizations());
if (!PolicyConstants.AI_API_QUOTA_TYPE_ENUM_VALUE.equals(quotaType)) {
subscriptionPolicy.setRateLimitTimeUnit(dto.getRateLimitTimeUnit());
if (dto.getRateLimitCount() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4061,10 +4061,6 @@ components:
example: FREE
permissions:
$ref: '#/components/schemas/SubscriptionThrottlePolicyPermission'
allowedOrganizations:
type: array
items:
type: string
SubscriptionThrottlePolicyPermission:
title: SubscriptionThrottlePolicyPermission
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4061,10 +4061,6 @@ components:
example: FREE
permissions:
$ref: '#/components/schemas/SubscriptionThrottlePolicyPermission'
allowedOrganizations:
type: array
items:
type: string
SubscriptionThrottlePolicyPermission:
title: SubscriptionThrottlePolicyPermission
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1194,13 +1194,6 @@ private APIDTO getAPIByAPIId(String apiId, String organization, OrganizationInfo

if (APIConstants.PUBLISHED.equals(status) || APIConstants.PROTOTYPED.equals(status)
|| APIConstants.DEPRECATED.equals(status)) {
if (!api.isAPIProduct()) {
// Add only organization specific tiers
Set<Tier> tiers = APIUtil.getAllowedTiersForTheOrganization(api.getApi().getAvailableTiers(),
userOrgInfo.getOrganizationSelector(), userOrgInfo.getSuperOrganization());
api.getApi().removeAllTiers();
api.getApi().setAvailableTiers(tiers);
}

APIDTO apidto = APIMappingUtil.fromAPItoDTO(api, organization);
long subscriptionCountOfAPI = apiConsumer.getSubscriptionCountOfAPI(apiId, organization);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2146,13 +2146,6 @@ CREATE TABLE IF NOT EXISTS AM_KEY_MANAGER_ALLOWED_ORGS (
FOREIGN KEY (KEY_MANAGER_UUID) REFERENCES AM_KEY_MANAGER(UUID) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS AM_POLICY_ALLOWED_ORGS (
ALLOWED_ORGS_ID INT NOT NULL AUTO_INCREMENT,
POLICY_UUID VARCHAR(50) NOT NULL,
ALLOWED_ORGANIZATIONS VARCHAR(50) NOT NULL,
PRIMARY KEY (ALLOWED_ORGS_ID)
);

-- AM_GW_PUBLISHED_API_DETAILS & AM_GW_API_ARTIFACTS are independent tables for Artifact synchronizer feature which --
-- should not have any referential integrity constraints with other tables in AM database--
CREATE TABLE IF NOT EXISTS AM_GW_PUBLISHED_API_DETAILS (
Expand Down

0 comments on commit 5741008

Please sign in to comment.