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

Implement organization support in unified search and fix bugs identified via integration tests #12717

Open
wants to merge 7 commits into
base: org_visibility
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,15 @@ boolean isKeyManagerByNameAllowedForUser(String keyManagerName, String organizat
*/
Map<String, Object> searchPaginatedAPIs(String searchQuery, OrganizationInfo organizationInfo, int start, int end,
String sortBy, String sortOrder) throws APIManagementException;

/**
* @param searchQuery search query
* @param organizationInfo Information about the organization
* @param start
* @param end
* @return
* @throws APIManagementException
*/
Map<String, Object> searchPaginatedContent(String searchQuery, OrganizationInfo organizationInfo, int start, int end)
throws APIManagementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,31 @@ public API getLightweightAPI(APIIdentifier identifier, String orgId) throws APIM
public Map<String, Object> searchPaginatedContent(String searchQuery, String organization, int start, int end)
throws APIManagementException {

String userame = (userNameWithoutChange != null) ? userNameWithoutChange : username;
Organization org = new Organization(organization);
Map<String, Object> properties = APIUtil.getUserProperties(userame);
String[] roles = APIUtil.getFilteredUserRoles(userame);
;
UserContext ctx = new UserContext(userame, org, properties, roles);
return searchPaginatedContent(org, searchQuery, start, end, ctx);
}

@Override
public Map<String, Object> searchPaginatedContent(String searchQuery, OrganizationInfo organizationInfo,
int start, int end) throws APIManagementException {

String userName = (userNameWithoutChange != null) ? userNameWithoutChange : username;
Organization org = new Organization(organizationInfo.getSuperOrganization());
Map<String, Object> properties = APIUtil.getUserProperties(userName);
String[] roles = APIUtil.getFilteredUserRoles(userName);

UserContext ctx = new UserContext(userName, new Organization(organizationInfo.getOrganizationSelector()),
properties, roles);
return searchPaginatedContent(org, searchQuery, start, end, ctx);
}

private Map<String, Object> searchPaginatedContent(Organization org, String searchQuery, int start, int end,
UserContext ctx) throws APIManagementException {
ArrayList<Object> compoundResult = new ArrayList<Object>();
Map<Documentation, API> docMap = new HashMap<Documentation, API>();
Map<String, Object> result = new HashMap<String, Object>();
Expand All @@ -4200,13 +4225,6 @@ public Map<String, Object> searchPaginatedContent(String searchQuery, String org
List<APIDefinitionContentSearchResult> defSearchList = new ArrayList<>();
int totalLength = 0;

String userame = (userNameWithoutChange != null) ? userNameWithoutChange : username;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to remove this section? maybe we could keep this code unchanged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code block is not removed. Instead preserved the previous logic as it is and introduced a separate function for org visibility enabled flow

Organization org = new Organization(organization);
Map<String, Object> properties = APIUtil.getUserProperties(userame);
String[] roles = APIUtil.getFilteredUserRoles(userame);
;
UserContext ctx = new UserContext(userame, org, properties, roles);

try {
DevPortalContentSearchResult sResults = apiPersistenceInstance.searchContentForDevPortal(org, searchQuery,
start, end, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A
}
resource.setContent(api.getSwaggerDefinition());
resource.setMediaType("application/json");
resource.setProperty(APIConstants.VISIBLE_ORGANIZATIONS, visibleOrgs);
registry.put(resourcePath, resource);
//Need to set anonymous if the visibility is public
RegistryPersistenceUtil.clearResourcePermissions(resourcePath, api.getId(),
Expand All @@ -217,6 +218,7 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A
}
resource.setContent(api.getAsyncApiDefinition());
resource.setMediaType(APIConstants.APPLICATION_JSON_MEDIA_TYPE); //add a constant for app.json
resource.setProperty(APIConstants.VISIBLE_ORGANIZATIONS, visibleOrgs);
registry.put(resourcePath, resource);
RegistryPersistenceUtil.clearResourcePermissions(resourcePath, api.getId(),
((UserRegistry) registry).getTenantId());
Expand All @@ -231,6 +233,7 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(),
visibleRoles, docLocation);
updateRegistryResourcesForArtifacts(registry, artifact.getId(), docLocation);

registry.commitTransaction();
api.setUuid(artifact.getId());
Expand Down Expand Up @@ -606,6 +609,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(),
visibleRoles, resourcePath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), resourcePath);
}

// Update api def file permissions, required for API definition content search functionality
Expand All @@ -619,6 +623,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(),
visibleRoles, resourcePath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), resourcePath);
}
} else if (api.isAsync()) {
String resourcePath = RegistryPersistenceUtil.getOpenAPIDefinitionFilePath(api.getId().getName(),
Expand All @@ -629,6 +634,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(),
visibleRoles, resourcePath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), resourcePath);
}
} else if (APIConstants.API_TYPE_SOAP.equals(api.getType())) {
String resourcePath = RegistryPersistenceUtil.getOpenAPIDefinitionFilePath(api.getId().getName(),
Expand All @@ -640,6 +646,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(),
visibleRoles, resourcePath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), resourcePath);
}
}

Expand Down Expand Up @@ -675,6 +682,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
+ doc.getName();
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(),
api.getVisibility(), visibleRoles, documentationPath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), documentationPath);
if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType())
|| Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {

Expand All @@ -683,12 +691,14 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
+ RegistryConstants.PATH_SEPARATOR + doc.getName();
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(),
api.getVisibility(), visibleRoles, contentPath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), contentPath);
} else if (Documentation.DocumentSourceType.FILE.equals(doc.getSourceType())
&& doc.getFilePath() != null) {
String filePath = RegistryPersistenceDocUtil.getDocumentationFilePath(api.getId(),
doc.getFilePath().split("files" + RegistryConstants.PATH_SEPARATOR)[1]);
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(),
api.getVisibility(), visibleRoles, filePath, registry);
updateRegistryResourcesForArtifacts(registry, api.getUuid(), filePath);
}
}
}
Expand Down Expand Up @@ -1961,6 +1971,7 @@ public void saveWSDL(Organization org, String apiId, ResourceFile wsdlResourceFi
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
updateRegistryResourcesForArtifacts(registry, apiId, wsdlResourcePath);
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, visibleRoles,
wsdlResourcePath, registry);

Expand Down Expand Up @@ -2113,6 +2124,7 @@ public void saveOASDefinition(Organization org, String apiId, String apiDefiniti
// Need to set anonymous if the visibility is public
RegistryPersistenceUtil.clearResourcePermissions(resourcePath,
new APIIdentifier(apiProviderName, apiName, apiVersion), ((UserRegistry) registry).getTenantId());
updateRegistryResourcesForArtifacts(registry, apiId, resourcePath);
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, visibleRolesArr,
resourcePath, registry);

Expand Down Expand Up @@ -2213,7 +2225,7 @@ public void saveAsyncDefinition(Organization org, String apiId, String apiDefini
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, visibleRolesArr, resourcePath
, registry);

updateRegistryResourcesForArtifacts(registry, apiId, resourcePath);
} catch (RegistryException | APIPersistenceException | APIManagementException e) {
throw new AsyncSpecPersistenceException("Error while adding AsyncApi Definition for " + apiId, e);
} finally {
Expand Down Expand Up @@ -2304,6 +2316,7 @@ public void saveGraphQLSchemaDefinition(Organization org, String apiId, String s
((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.apiProvider, api.visibility, api.visibleRoles,
saveResourcePath, registry);
updateRegistryResourcesForArtifacts(registry, apiId, saveResourcePath);

} catch (RegistryException | APIManagementException | APIPersistenceException e) {
throw new GraphQLPersistenceException("Error while adding Graphql Definition for api " + apiId, e);
Expand Down Expand Up @@ -2369,9 +2382,10 @@ public Documentation addDocumentation(Organization org, String apiId, Documentat
GenericArtifactManager docArtifactManager = new GenericArtifactManager(registry,
APIConstants.DOCUMENTATION_KEY);
GenericArtifact docArtifact = docArtifactManager.newGovernanceArtifact(new QName(documentation.getName()));
docArtifactManager.addGenericArtifact(RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact,
apiName, apiVersion, apiProviderName, documentation));

GenericArtifact genericDocArtifact = RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact,
apiName, apiVersion, apiProviderName, documentation);
docArtifactManager.addGenericArtifact(genericDocArtifact);
String docArtifactPath = GovernanceUtils.getArtifactPath(registry, genericDocArtifact.getId());
String apiPath = RegistryPersistenceUtil.getAPIPath(apiName, apiVersion, apiProviderName);
String docVisibility = documentation.getVisibility().name();
String[] authorizedRoles = RegistryPersistenceUtil.getAuthorizedRoles(apiPath, tenantDomain);
Expand All @@ -2385,6 +2399,7 @@ public Documentation addDocumentation(Organization org, String apiId, Documentat
visibility = APIConstants.DOC_OWNER_VISIBILITY;
}
}
updateRegistryResourcesForArtifacts(registry, apiId, docArtifactPath);
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, docArtifact
.getPath(), registry);
String docFilePath = docArtifact.getAttribute(APIConstants.DOC_FILE_PATH);
Expand All @@ -2398,6 +2413,7 @@ public Documentation addDocumentation(Organization org, String apiId, Documentat
String filePath = docFilePath.substring(startIndex, docFilePath.length());
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, filePath,
registry);
updateRegistryResourcesForArtifacts(registry, apiId, filePath);
}
}
documentation.setId(docArtifact.getId());
Expand Down Expand Up @@ -2457,6 +2473,7 @@ public Documentation updateDocumentation(Organization org, String apiId, Documen

RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles,
artifact.getPath(), registry);
updateRegistryResourcesForArtifacts(registry, apiId, artifact.getPath());

String docFilePath = artifact.getAttribute(APIConstants.DOC_FILE_PATH);
if (docFilePath != null && !"".equals(docFilePath)) {
Expand All @@ -2469,6 +2486,7 @@ public Documentation updateDocumentation(Organization org, String apiId, Documen
String filePath = docFilePath.substring(startIndex, docFilePath.length());
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, filePath,
registry);
updateRegistryResourcesForArtifacts(registry, apiId, filePath);
}
return documentation;
} catch (RegistryException | APIManagementException | APIPersistenceException e) {
Expand Down Expand Up @@ -2638,6 +2656,7 @@ public DocumentContent addDocumentationContent(Organization org, String apiId, S
RegistryPersistenceUtil.setResourcePermissions(
RegistryPersistenceUtil.replaceEmailDomain(apiProviderName), visibility, visibleRoles, filePath,
registry);
updateRegistryResourcesForArtifacts(registry, apiId, filePath);
//documentation.setFilePath(addResourceFile(apiId, filePath, icon));
String savedFilePath = addResourceFile(filePath, resource, registry, tenantDomain);
//doc.setFilePath(savedFilePath);
Expand Down Expand Up @@ -2677,6 +2696,7 @@ public DocumentContent addDocumentationContent(Organization org, String apiId, S
}
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles,
contentPath, registry);
updateRegistryResourcesForArtifacts(registry, apiId, contentPath);
GenericArtifact updateDocArtifact = RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact,
apiProviderName, apiName, apiVersion, doc);
Boolean toggle = Boolean.parseBoolean(updateDocArtifact.getAttribute("toggle"));
Expand Down Expand Up @@ -3165,6 +3185,31 @@ private void updateRegistryResources(Registry registry, String artifactPath, Str
}
}

private void updateRegistryResourcesForArtifacts(Registry registry, String apiId, String artifactResourcePath)
throws RegistryException {

//get path to API in registry
String artifactPath = GovernanceUtils.getArtifactPath(registry, apiId);

//get API
Resource apiResource = registry.get(artifactPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this method, we do couple of registry get actions to get the API's organization. I think we could pass the organization visibility from the method calling location instead of doing multiple registry calls. For example, API artifact is already retrieved in https://github.com/wso2/carbon-apimgt/pull/12717/files#diff-e193f41af36b63c9f226e35c981eeae30ee45850c880876c502785b547dbe00cR2635. you could get the organization information there and pass it to this method


//get orgs of the API
String visibleOrgs = apiResource.getProperty(APIConstants.VISIBLE_ORGANIZATIONS);

//add the org value for api-artifact in the registry
if (registry.resourceExists(artifactResourcePath)) {
Resource artifactResource = registry.get(artifactResourcePath);
if (artifactResource != null) {
if (!StringUtils.isEmpty(visibleOrgs) && visibleOrgs.contains(" ")) {
visibleOrgs = visibleOrgs.replace(" ", "+");
}
artifactResource.setProperty(APIConstants.VISIBLE_ORGANIZATIONS, visibleOrgs);
registry.put(artifactResourcePath, artifactResource);
}
}
}

protected static int getMaxPaginationLimit() {

return Integer.MAX_VALUE;
Expand Down Expand Up @@ -3411,7 +3456,8 @@ public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct p
String publisherAccessControlRoles = apiProduct.getAccessControlRoles();
String visibleOrgs = APIConstants.DEFAULT_VISIBLE_ORG;
// if (StringUtils.isEmpty(apiProduct.getVisibleOrganizations())) {
//visibleOrgs = apiProduct.getVisibleOrganizations(); TODO fix for products
//visibleOrgs = apiProduct.getVisibleOrganizations();
// TODO fix for products and do needful for unified search
// }
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(),
apiProduct.getAdditionalProperties(), visibleOrgs);
Expand Down Expand Up @@ -3641,7 +3687,8 @@ public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduc

String visibleOrgs = APIConstants.DEFAULT_VISIBLE_ORG;
// if (APIConstants.API_RESTRICTED_BY_ORG.equals(apiProduct.getVisibility())){
//visibleOrgs = apiProduct.getVisibleOrganizations(); TODO fix for products
//visibleOrgs = apiProduct.getVisibleOrganizations();
// TODO fix for products and do needful for unified search
// }
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(),
apiProduct.getAdditionalProperties(), visibleOrgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,12 @@ public static Map<String, String> getFields(String query) {
case "group.sort":
outputMap.put("group.sort", "overview_" + value);
break;
case "tags":
outputMap.put("tags", value);
break;
case "apiCategories_categoryName":
outputMap.put("apiCategories_categoryName", value.toLowerCase());
break;
default:
// Add any other cases if needed
outputMap.put("overview_" + key, value);
Expand Down
Loading