Skip to content

Commit

Permalink
Merge branch 'main' into extending_org_search_feature
Browse files Browse the repository at this point in the history
  • Loading branch information
HasiniSama committed Jul 22, 2024
2 parents 9c61d49 + 723ee63 commit 82faa29
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>org.wso2.carbon.identity.organization.management.core</groupId>
<artifactId>identity-organization-management-core</artifactId>
<version>1.1.9-SNAPSHOT</version>
<version>1.1.12-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,24 @@ default Organization addRootOrganization(int tenantId, Organization organization

throw new NotImplementedException();
}

/**
* List of organizations' meta attributes.
*
* @param limit The maximum number of records to be returned.
* @param after The pointer to next page.
* @param before The pointer to previous page.
* @param sortOrder The sort order, ascending or descending.
* @param filter The filter string.
* @param recursive Determines whether records should be retrieved in a recursive manner.
* @return The list of organizations' meta attributes.
* @throws OrganizationManagementException The exception thrown when listing organizations' meta attributes.
*/
default List<String> getOrganizationsMetaAttributes(Integer limit, String after, String before, String sortOrder,
String filter, boolean recursive)
throws OrganizationManagementException {

throw new OrganizationManagementServerException("getOrganizationsMetaAttributes is not implemented in "
+ this.getClass().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.AND;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ASC_SORT_ORDER;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CO;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CREATOR_EMAIL;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CREATOR_ID;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CREATOR_USERNAME;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.DESC_SORT_ORDER;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EW;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ACTIVE_CHILD_ORGANIZATIONS_EXIST;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ATTRIBUTE_KEY_MISSING;
Expand Down Expand Up @@ -322,7 +324,7 @@ private List<BasicOrganization> getOrganizationsBasicInfo(boolean authorizedSubO
String applicationAudience)
throws OrganizationManagementException {

List<ExpressionNode> expressionNodes = getExpressionNodes(filter, after, before);
List<ExpressionNode> expressionNodes = getExpressionNodes(filter, after, before, DESC_SORT_ORDER);
List<ExpressionNode> filteringByParentIdExpressionNodes = getParentIdExpressionNodes(expressionNodes);
String orgId = resolveOrganizationId(getTenantDomain());
expressionNodes.removeAll(filteringByParentIdExpressionNodes);
Expand All @@ -338,7 +340,7 @@ private List<Organization> getOrganizationList(Integer limit, String after, Stri
String filter, boolean recursive)
throws OrganizationManagementException {

List<ExpressionNode> expressionNodes = getExpressionNodes(filter, after, before);
List<ExpressionNode> expressionNodes = getExpressionNodes(filter, after, before, DESC_SORT_ORDER);
List<ExpressionNode> filteringByParentIdExpressionNodes = getParentIdExpressionNodes(expressionNodes);
String orgId = resolveOrganizationId(getTenantDomain());
expressionNodes.removeAll(filteringByParentIdExpressionNodes);
Expand Down Expand Up @@ -575,6 +577,17 @@ public Organization addRootOrganization(int tenantId, Organization organization)
return organization;
}

@Override
public List<String> getOrganizationsMetaAttributes(Integer limit, String after, String before, String sortOrder,
String filter, boolean recursive)
throws OrganizationManagementException {

List<ExpressionNode> expressionNodes = getExpressionNodes(filter, after, before, ASC_SORT_ORDER);
String orgId = resolveOrganizationId(getTenantDomain());
return organizationManagementDAO.getOrganizationsMetaAttributes(recursive, limit, orgId, sortOrder,
expressionNodes);
}

private void updateTenantStatus(String status, String organizationId) throws OrganizationManagementServerException {

if (StringUtils.equals(ACTIVE.toString(), status)) {
Expand Down Expand Up @@ -902,14 +915,19 @@ private void validateOrganizationStatusUpdate(String value, String organizationI
}
}

private List<ExpressionNode> getExpressionNodes(String filter, String after, String before)
private List<ExpressionNode> getExpressionNodes(String filter, String after, String before,
String paginationSortOrder)
throws OrganizationManagementClientException {

List<ExpressionNode> expressionNodes = new ArrayList<>();
if (StringUtils.isBlank(filter)) {
filter = StringUtils.EMPTY;
}
String paginatedFilter = getPaginatedFilter(filter, after, before);
// paginationSortOrder specifies the sorting order for the pagination cursor.
// E.g., descending for creation time (most recent first) or ascending for metadata name (alphabetical).
String paginatedFilter = paginationSortOrder.equals(ASC_SORT_ORDER) ?
getPaginatedFilterForAscendingOrder(filter, after, before) :
getPaginatedFilterForDescendingOrder(filter, after, before);
try {
if (StringUtils.isNotBlank(paginatedFilter)) {
FilterTreeBuilder filterTreeBuilder = new FilterTreeBuilder(paginatedFilter);
Expand All @@ -922,8 +940,27 @@ private List<ExpressionNode> getExpressionNodes(String filter, String after, Str
return expressionNodes;
}

private String getPaginatedFilter(String paginatedFilter, String after, String before) throws
OrganizationManagementClientException {
private String getPaginatedFilterForAscendingOrder(String paginatedFilter, String after, String before)
throws OrganizationManagementClientException {

try {
if (StringUtils.isNotBlank(before)) {
String decodedString = new String(Base64.getDecoder().decode(before), StandardCharsets.UTF_8);
paginatedFilter += StringUtils.isNotBlank(paginatedFilter) ? " and before lt "
+ decodedString : "before lt " + decodedString;
} else if (StringUtils.isNotBlank(after)) {
String decodedString = new String(Base64.getDecoder().decode(after), StandardCharsets.UTF_8);
paginatedFilter += StringUtils.isNotBlank(paginatedFilter) ? " and after gt "
+ decodedString : "after gt " + decodedString;
}
} catch (IllegalArgumentException e) {
throw handleClientException(ERROR_CODE_INVALID_CURSOR_FOR_PAGINATION);
}
return paginatedFilter;
}

private String getPaginatedFilterForDescendingOrder(String paginatedFilter, String after, String before)
throws OrganizationManagementClientException {

try {
if (StringUtils.isNotBlank(before)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public class OrganizationManagementConstants {
public static final List<String> ALL_ORGANIZATION_PERMISSIONS = Collections.unmodifiableList(Arrays
.asList(CREATE_ORGANIZATION_PERMISSION, VIEW_ORGANIZATION_PERMISSION, UPDATE_ORGANIZATION_PERMISSION,
DELETE_ORGANIZATION_PERMISSION));
public static final String ROOT_TENANT_DOMAIN = "RootTenantDomain";
public static final String DESC_SORT_ORDER = "DESC";
public static final String ASC_SORT_ORDER = "ASC";

public static final String EQ = "eq";
public static final String CO = "co";
Expand Down Expand Up @@ -231,8 +232,8 @@ public enum ErrorMessages {
"The complex query used for filtering is not supported."),
ERROR_CODE_INVALID_PAGINATION_PARAMETER_NEGATIVE_LIMIT("60025", "Invalid pagination parameters.",
"'limit' shouldn't be negative."),
ERROR_CODE_INVALID_CURSOR_FOR_PAGINATION("60026", "Unable to retrieve organizations.", "Invalid " +
"cursor used for pagination."),
ERROR_CODE_INVALID_CURSOR_FOR_PAGINATION("60026", "Unable to retrieve paginated result.",
"Invalid cursor used for pagination."),
ERROR_CODE_UNSUPPORTED_ORGANIZATION_STATUS("60028", "Unsupported status provided.",
"Organization status must be 'ACTIVE' or 'DISABLED'."),
ERROR_CODE_ACTIVE_CHILD_ORGANIZATIONS_EXIST("60029", "Active child organizations exist.",
Expand Down Expand Up @@ -724,7 +725,10 @@ public enum ErrorMessages {
"organization with ID: %s."),
ERROR_CODE_ERROR_REVOKING_SHARED_APP_TOKENS("65138", "Error while revoking tokens issued for " +
"shared application.", "Server encountered an error while revoking tokens issued for application: " +
"%s in organization with ID: %s");
"%s in organization with ID: %s"),
ERROR_CODE_ERROR_RETRIEVING_ORGANIZATIONS_META_ATTRIBUTES("65139", "Unable to retrieve " +
"the organizations' meta attributes.", "Server encountered an error while retrieving " +
"the organizations' meta attributes.");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,28 @@ public class SQLConstants {
"UM_ORG_ATTRIBUTE.UM_ATTRIBUTE_VALUE FROM UM_ORG INNER JOIN UM_ORG_ATTRIBUTE ON UM_ORG.UM_ID " +
"= UM_ORG_ATTRIBUTE.UM_ORG_ID INNER JOIN (%s) AS FILTERED_ORG ON UM_ORG.UM_ID = FILTERED_ORG.UM_ID;";

public static final String GET_ORGANIZATIONS_META_ATTRIBUTES = "SELECT DISTINCT " +
"UM_ORG_ATTRIBUTE.UM_ATTRIBUTE_KEY FROM UM_ORG_ATTRIBUTE " +
"JOIN UM_ORG ON UM_ORG_ATTRIBUTE.UM_ORG_ID = UM_ORG.UM_ID WHERE ";

public static final String GET_ORGANIZATIONS_META_ATTRIBUTES_TAIL = "UM_ORG.UM_ID IN " +
"(SELECT O.UM_ID FROM UM_ORG O JOIN UM_ORG_HIERARCHY OH ON O.UM_ID = OH.UM_ID " +
"WHERE OH.UM_PARENT_ID = (SELECT UM_ID FROM UM_ORG WHERE %s) " +
"AND OH.DEPTH %s) ORDER BY UM_ORG_ATTRIBUTE.UM_ATTRIBUTE_KEY %s " +
"LIMIT :" + SQLPlaceholders.DB_SCHEMA_LIMIT + ";";

public static final String GET_ORGANIZATIONS_META_ATTRIBUTES_TAIL_ORACLE = "UM_ORG.UM_ID IN " +
"(SELECT O.UM_ID FROM UM_ORG O JOIN UM_ORG_HIERARCHY OH ON O.UM_ID = OH.UM_ID " +
"WHERE OH.UM_PARENT_ID = (SELECT UM_ID FROM UM_ORG WHERE %s) " +
"AND OH.DEPTH %s) ORDER BY UM_ORG_ATTRIBUTE.UM_ATTRIBUTE_KEY %s " +
"FETCH FIRST :" + SQLPlaceholders.DB_SCHEMA_LIMIT + "; ROWS ONLY;";

public static final String GET_ORGANIZATIONS_META_ATTRIBUTES_TAIL_MSSQL = "UM_ORG.UM_ID IN " +
"(SELECT O.UM_ID FROM UM_ORG O JOIN UM_ORG_HIERARCHY OH ON O.UM_ID = OH.UM_ID " +
"WHERE OH.UM_PARENT_ID = (SELECT UM_ID FROM UM_ORG WHERE %s) " +
"AND OH.DEPTH %s) ORDER BY UM_ORG_ATTRIBUTE.UM_ATTRIBUTE_KEY %s " +
"OFFSET 0 ROWS FETCH NEXT :" + SQLPlaceholders.DB_SCHEMA_LIMIT + "; ROWS ONLY;";

/**
* SQL Placeholders.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,24 @@ String getAnAncestorOrganizationIdInGivenDepth(String organizationId, int depth)
* @throws OrganizationManagementServerException The server exception thrown when adding a root organization.
*/
void addRootOrganization(Organization rootOrganization) throws OrganizationManagementServerException;

/**
* Retrieve the list of organizations' meta attributes.
*
* @param recursive Determines whether records should be retrieved in a recursive manner.
* @param limit The maximum number of records to be returned.
* @param organizationId The super organization ID.
* @param sortOrder The sort order, ascending or descending.
* @param expressionNodes The list of filters.
* @return the list of organizations' meta attributes.
* @throws OrganizationManagementServerException The server exception thrown when retrieving the organizations'
* meta attributes.
*/
default List<String> getOrganizationsMetaAttributes(boolean recursive, Integer limit, String organizationId,
String sortOrder, List<ExpressionNode> expressionNodes)
throws OrganizationManagementServerException {

throw new OrganizationManagementServerException("getOrganizationsMetaAttributes is not implemented in "
+ this.getClass().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@ public void addRootOrganization(Organization organization) throws OrganizationMa
organizationMgtDAO.addRootOrganization(organization);
}

@Override
public List<String> getOrganizationsMetaAttributes(boolean recursive, Integer limit, String organizationId,
String sortOrder, List<ExpressionNode> expressionNodes)
throws OrganizationManagementServerException {

return organizationMgtDAO.getOrganizationsMetaAttributes(recursive, limit, organizationId, sortOrder,
expressionNodes);
}

private TenantDomainCacheEntry getTenantDomainFromCache(String organizationId) {

OrganizationIdCacheKey cacheKey = new OrganizationIdCacheKey(organizationId);
Expand Down
Loading

0 comments on commit 82faa29

Please sign in to comment.