Skip to content

Commit

Permalink
Add api org visibility to registry property
Browse files Browse the repository at this point in the history
  • Loading branch information
chamilaadhi committed Aug 16, 2024
1 parent 62cd1b1 commit 6930c4e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public final class APIConstants {
public static final String API_OVERVIEW_VISIBILITY = "overview_visibility";
public static final String API_OVERVIEW_VISIBLE_ROLES = "overview_visibleRoles";
public static final String API_OVERVIEW_VISIBLE_TENANTS = "overview_visibleTenants";
public static final String API_OVERVIEW_VISIBLE_ORGANIZATIONS = "overview_visibleOrganizations";
public static final String API_OVERVIEW_ENVIRONMENTS = "overview_environments";
public static final String API_OVERVIEW_AUDIENCE = "overview_audience";
public static final String API_OVERVIEW_AUDIENCES = "overview_audiences";
Expand Down Expand Up @@ -204,6 +203,7 @@ public static class Monetization {
public static final String DISPLAY_PUBLISHER_ROLES = "display_publisher_roles";
public static final String ACCESS_CONTROL = "publisher_access_control";
public static final String NO_ACCESS_CONTROL = "all";
public static final String VISIBLE_ORGANIZATIONS = "visible_organizations";
public static final String NULL_USER_ROLE_LIST = "null";
public static final String API_RESTRICTED_VISIBILITY = "restricted";
public static final String API_PRIVATE_VISIBILITY = "private";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,15 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}

String visibleOrgs = APIConstants.DEFAULT_VISIBLE_ORG;
if (APIConstants.API_RESTRICTED_BY_ORG.equals(api.getVisibility())){
visibleOrgs = api.getVisibleOrganizations();
}

String publisherAccessControlRoles = api.getAccessControlRoles();
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, api.getAccessControl(),
api.getAdditionalProperties());
api.getAdditionalProperties(), visibleOrgs);
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(),
visibleRoles, artifactPath, registry);

Expand Down Expand Up @@ -547,8 +552,12 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
String[] visibleRoles = new String[0];
String publisherAccessControlRoles = api.getAccessControlRoles();

String visibleOrgs = APIConstants.DEFAULT_VISIBLE_ORG;
if (APIConstants.API_RESTRICTED_BY_ORG.equals(api.getVisibility())){
visibleOrgs = api.getVisibleOrganizations();
}
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, api.getAccessControl(),
api.getAdditionalProperties());
api.getAdditionalProperties(), visibleOrgs);

//propagate api status change and access control roles change to document artifact
String newStatus = updateApiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
Expand Down Expand Up @@ -1078,8 +1087,8 @@ public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String
log.debug("Requested query for devportal search: " + searchQuery);
String modifiedQuery = RegistrySearchUtil.getDevPortalSearchQuery(searchQuery, ctx,
isAllowDisplayAPIsWithMultipleStatus(), isAllowDisplayAPIsWithMultipleVersions());
modifiedQuery = modifiedQuery + "&visibleOrganizations=(" + APIConstants.DEFAULT_VISIBLE_ORG + " OR *"
+ ctx.getOrganization().getName() + "*)";
//modifiedQuery = modifiedQuery + "&" + APIConstants.VISIBLE_ORGANIZATIONS + "=("
// + APIConstants.DEFAULT_VISIBLE_ORG + " OR *" + ctx.getOrganization().getName() + "*)";
log.debug("Modified query for devportal search: " + modifiedQuery);
String userNameLocal;
if (holder.isAnonymousMode()) {
Expand All @@ -1093,7 +1102,12 @@ public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String
result = searchPaginatedDevPortalAPIsByDoc(userRegistry, tenantIDLocal, searchQuery.split(":")[1],
userNameLocal, start, offset);
} else {
searchSolr(
modifiedQuery + "&" + APIConstants.VISIBLE_ORGANIZATIONS + "=("
+ APIConstants.DEFAULT_VISIBLE_ORG + " OR *" + ctx.getOrganization().getName() + "*)",
tenantIDLocal, userRegistry, start, offset);
result = searchPaginatedDevPortalAPIs(userRegistry, tenantIDLocal, modifiedQuery, start, offset);

}
} catch (APIManagementException e) {
throw new APIPersistenceException("Error while searching APIs ", e);
Expand All @@ -1104,6 +1118,83 @@ public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String
}
return result;
}

private void searchSolr(String query, int tenantId, Registry reg, int start, int offset) {
Map<String, String> attributes = processQuery(query);

try {
PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, getMaxPaginationLimit());
UserRegistry systemUserRegistry = ServiceReferenceHolder.getInstance().getRegistryService()
.getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, tenantId);
ContentBasedSearchService contentBasedSearchService = new ContentBasedSearchService();
SearchResultsBean resultsBean = contentBasedSearchService.searchByAttribute(attributes, systemUserRegistry);
log.debug(resultsBean);
} catch (IndexerException | RegistryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Map<String, String> processQuery(String query) {
// Map to hold the final output
Map<String, String> outputMap = new HashMap<>();

// Split the query by '&'
String[] parameters = query.split("&");

// Process each parameter
for (String parameter : parameters) {
// Split each parameter by '=' to get key and value
String[] keyValue = parameter.split("=");

// Extract the key and value
String key = keyValue[0];
String value = keyValue.length > 1 ? keyValue[1] : "";

// Map keys to the corresponding output format
switch (key) {
case "store_view_roles":
outputMap.put("propertyName", key);
outputMap.put("rightPropertyValue", value);
break;
case "visible_organizations":
outputMap.put("propertyName", key);
outputMap.put("rightPropertyValue", value);
break;
// case "name":
// outputMap.put("overview_name", value);
// break;
// case "enableStore":
// outputMap.put("overview_enableStore", value);
// break;
case "group":
outputMap.put("group", value);
break;
case "group.field":
outputMap.put("group.field", "overview_" + value);
break;
case "group.ngroups":
outputMap.put("group.ngroups", value);
break;
case "group.sort":
outputMap.put("group.sort", "overview_" + value);
break;
// case "status":
// outputMap.put("overview_status", value.toLowerCase());
// break;
default:
// Add any other cases if needed
outputMap.put("overview_" + key, value);
break;
}
}

// Add additional mappings if needed
outputMap.put("mediaType", "application/vnd.wso2-api+xml");
outputMap.put("rightOp", "eq");

return outputMap;
}

private DevPortalAPISearchResult searchPaginatedDevPortalAPIs(Registry userRegistry, int tenantIDLocal,
String searchQuery, int start, int offset) throws APIManagementException {
Expand Down Expand Up @@ -2981,7 +3072,7 @@ private void saveAPIStatus(Registry registry, String artifactId, String apiStatu
* @throws RegistryException Registry Exception.
*/
private void updateRegistryResources(Registry registry, String artifactPath, String publisherAccessControlRoles,
String publisherAccessControl, Map<String, String> additionalProperties)
String publisherAccessControl, Map<String, String> additionalProperties, String visibleOrganizations)
throws RegistryException {
publisherAccessControlRoles = (publisherAccessControlRoles == null || publisherAccessControlRoles.trim()
.isEmpty()) ? APIConstants.NULL_USER_ROLE_LIST : publisherAccessControlRoles;
Expand Down Expand Up @@ -3014,6 +3105,7 @@ private void updateRegistryResources(Registry registry, String artifactPath, Str
// the roles that were specified can be maintained.
apiResource.setProperty(APIConstants.DISPLAY_PUBLISHER_ROLES, publisherAccessControlRoles);
apiResource.setProperty(APIConstants.ACCESS_CONTROL, publisherAccessControl);
apiResource.setProperty(APIConstants.VISIBLE_ORGANIZATIONS, visibleOrganizations);
apiResource.removeProperty(APIConstants.CUSTOM_API_INDEXER_PROPERTY);
if (additionalProperties != null && additionalProperties.size() != 0) {
for (Map.Entry<String, String> entry : additionalProperties.entrySet()) {
Expand Down Expand Up @@ -3269,8 +3361,12 @@ public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct p
}

String publisherAccessControlRoles = apiProduct.getAccessControlRoles();
String visibleOrgs = APIConstants.DEFAULT_VISIBLE_ORG;
if (APIConstants.API_RESTRICTED_BY_ORG.equals(apiProduct.getVisibility())){
//visibleOrgs = apiProduct.getVisibleOrganizations(); TODO fix for products
}
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(),
apiProduct.getAdditionalProperties());
apiProduct.getAdditionalProperties(), visibleOrgs);
RegistryPersistenceUtil.setResourcePermissions(apiProduct.getId().getProviderName(),
apiProduct.getVisibility(), visibleRoles, artifactPath, registry);

Expand Down Expand Up @@ -3495,8 +3591,12 @@ public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduc
applyTags(apiProduct.getTags(), registry, artifactPath);
String publisherAccessControlRoles = apiProduct.getAccessControlRoles();

String visibleOrgs = APIConstants.DEFAULT_VISIBLE_ORG;
if (APIConstants.API_RESTRICTED_BY_ORG.equals(apiProduct.getVisibility())){
//visibleOrgs = apiProduct.getVisibleOrganizations(); TODO fix for products
}
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(),
apiProduct.getAdditionalProperties());
apiProduct.getAdditionalProperties(), visibleOrgs);
RegistryPersistenceUtil.setResourcePermissions(apiProduct.getId().getProviderName(),
apiProduct.getVisibility(), visibleRoles, artifactPath, registry);
registry.commitTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ public static GenericArtifact createAPIArtifactContent(GenericArtifact artifact,
artifact.setAttribute(APIConstants.API_OVERVIEW_ENABLE_STORE, Boolean.toString(api.isEnableStore()));
artifact.setAttribute(APIConstants.API_OVERVIEW_TESTKEY, api.getTestKey());
artifact.setAttribute(APIConstants.API_OVERVIEW_VERSION_COMPARABLE, api.getVersionTimestamp());
if (APIConstants.API_RESTRICTED_BY_ORG.equals(api.getVisibility())) {
artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBLE_ORGANIZATIONS, api.getVisibleOrganizations());
} else {
// Set a default value if org visibility is not set. This is done to generate search query for public apis
artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBLE_ORGANIZATIONS, APIConstants.DEFAULT_VISIBLE_ORG);
}

//Validate if the API has an unsupported context before setting it in the artifact
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
Expand Down Expand Up @@ -607,7 +601,6 @@ public static API getAPI(GovernanceArtifact artifact, Registry registry)
api.setVisibility(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY));
api.setVisibleRoles(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES));
api.setVisibleTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS));
api.setVisibleOrganizations(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ORGANIZATIONS));
api.setEndpointSecured(Boolean.parseBoolean(artifact.getAttribute(
APIConstants.API_OVERVIEW_ENDPOINT_SECURED)));
api.setEndpointAuthDigest(Boolean.parseBoolean(artifact.getAttribute(
Expand Down Expand Up @@ -806,6 +799,7 @@ private static API setResourceProperties(API api, Resource apiResource, String a
}
}
api.setAccessControl(apiResource.getProperty(APIConstants.ACCESS_CONTROL));
api.setVisibleOrganizations(apiResource.getProperty(APIConstants.VISIBLE_ORGANIZATIONS));

String accessControlRoles = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@
<field type="text">
<name>Visible Tenants</name>
</field>
<field type="text">
<name label="Visible Organizations">visibleOrganizations</name>
</field>
<field type="options">
<name label="IsLatest">Is Latest</name>
<values>
Expand Down

0 comments on commit 6930c4e

Please sign in to comment.