Skip to content

Commit

Permalink
Merge pull request wso2#12528 from chamilaadhi/test-org
Browse files Browse the repository at this point in the history
Test org
  • Loading branch information
chamilaadhi authored Aug 28, 2024
2 parents 6f04ad5 + 75f595d commit 3a95871
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 20 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 @@ -173,10 +173,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 @@ -548,8 +553,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 @@ -1080,7 +1089,7 @@ public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String
String modifiedQuery = RegistrySearchUtil.getDevPortalSearchQuery(searchQuery, ctx,
isAllowDisplayAPIsWithMultipleStatus(), isAllowDisplayAPIsWithMultipleVersions());
if (!PersistenceUtil.isAdminUser(ctx)) {
modifiedQuery = modifiedQuery + "&visibleOrganizations=(" + APIConstants.DEFAULT_VISIBLE_ORG + " OR *"
modifiedQuery = modifiedQuery + "&visible_organizations=(" + APIConstants.DEFAULT_VISIBLE_ORG + " OR *"
+ ctx.getOrganization().getName() + "*)";
}
log.debug("Modified query for devportal search: " + modifiedQuery);
Expand All @@ -1107,6 +1116,55 @@ public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String
}
return result;
}

private List<GovernanceArtifact> searchDevportalAPIs(String query, int tenantId, Registry reg, int start, int offset)
throws APIManagementException {
List<GovernanceArtifact> artifacts = new ArrayList<GovernanceArtifact>();
query = query.replace("PUBLISHED", "published").replace("PROTOTYPED", "prototyped");// convert to lowercase
Map<String, String> fields = RegistryPersistenceUtil.getFields(query);
//since store_view_roles and overview_visible_organizations are passed as property search value, remove this.
fields.remove("overview_store_view_roles");
fields.remove("overview_visible_organizations");
String filterQuery = RegistryPersistenceUtil.buildFQStringForProperties(query);
String modifiedQuery = "q=* TO *&" + filterQuery;

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(modifiedQuery, fields, systemUserRegistry);
if (log.isDebugEnabled()) {
log.debug("Search Result: " + resultsBean);
}

ResourceData[] resourceDataList = resultsBean.getResourceDataList();
int errorCount = 0; // We use this to check how many errors occurred.
for (ResourceData resourceData : resourceDataList) {
GovernanceArtifact governanceArtifact = null;
String path = resourceData.getResourcePath().substring(RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH.length());
try {
governanceArtifact = GovernanceUtils.retrieveGovernanceArtifactByPath(reg, path);
} catch (GovernanceException e) {
// We do not through any exception here. Only logging is done.
// We increase the error count for each error. If all the paths failed, then we throw an error
errorCount++;
log.error("Error occurred while retrieving governance artifact by path : " + path, e);
}
if (governanceArtifact != null) {
artifacts.add(governanceArtifact);
}
}
if (errorCount != 0 && errorCount == resourceDataList.length) {
// This means that all the paths have failed. So we throw an error.
throw new APIManagementException("Error occurred while retrieving all the governance artifacts");
}
} catch (IndexerException | RegistryException e) {
String msg = "Failed to search APIs";
throw new APIManagementException(msg, e);
}
return artifacts;
}

private DevPortalAPISearchResult searchPaginatedDevPortalAPIs(Registry userRegistry, int tenantIDLocal,
String searchQuery, int start, int offset) throws APIManagementException {
Expand All @@ -1118,9 +1176,10 @@ private DevPortalAPISearchResult searchPaginatedDevPortalAPIs(Registry userRegis

PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
log.debug("Dev portal list apis query " + searchQuery);
List<GovernanceArtifact> governanceArtifacts = GovernanceUtils
.findGovernanceArtifacts(searchQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE,
true);
//List<GovernanceArtifact> governanceArtifacts = GovernanceUtils
// .findGovernanceArtifacts(searchQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE,
// true);
List<GovernanceArtifact> governanceArtifacts = searchDevportalAPIs(searchQuery, tenantIDLocal, userRegistry, start, offset);
totalLength = PaginationContext.getInstance().getLength();
boolean isFound = true;
if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
Expand Down Expand Up @@ -2984,7 +3043,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 @@ -3017,6 +3076,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 @@ -3272,8 +3332,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 @@ -3498,8 +3562,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 @@ -81,6 +81,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -178,12 +179,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 +602,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 +800,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 Expand Up @@ -1843,4 +1838,78 @@ public static String extractProvider(String apiPath, String apiName) {
private static RegistryService getRegistryService() {
return ServiceReferenceHolder.getInstance().getRegistryService();
}

public static Map<String, String> getFields(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 "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;
default:
// Add any other cases if needed
outputMap.put("overview_" + key, value);
break;
}
}

outputMap.put("mediaType", "application/vnd.wso2-api+xml");

return outputMap;
}

public static String buildFQStringForProperties(String query) {
String fq = "";
boolean hasStoreViewRoles = query.contains("store_view_roles");
boolean hasVisibleOrganizations = query.contains("visible_organizations");

// Build fq string based on the availability of store_view_roles and visible_organizations
if (hasStoreViewRoles) {
String storeViewRoles = extractValue(query, "store_view_roles");
fq += "fq=store_view_roles_ss:" + storeViewRoles;
}

if (hasVisibleOrganizations) {
if (!fq.isEmpty()) {
fq += "&";
}
String visibleOrganizations = extractValue(query, "visible_organizations");
fq += "fq=visible_organizations_ss:" + visibleOrganizations;
}

return fq;
}

private static String extractValue(String query, String paramName) {
String paramPrefix = paramName + "=";
int startIndex = query.indexOf(paramPrefix) + paramPrefix.length();
int endIndex = query.indexOf("&", startIndex);
if (endIndex == -1) {
endIndex = query.length();
}
return query.substring(startIndex, endIndex);
}
}
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 3a95871

Please sign in to comment.