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

Support filtering by organization name for listing discovery attributes of organizations method #266

Merged
merged 2 commits into from
Oct 22, 2023
Merged
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 @@ -79,11 +79,14 @@
org.wso2.carbon.identity.organization.config.service.constant;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.config.service.exception;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.config.service.model;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.discovery.service.constant;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.discovery.service.dao;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.discovery.service.model;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.constant;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.exception;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.filter;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.model;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.util;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.core;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.util;version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ boolean isDiscoveryAttributeValueAvailable(String organizationId, String type, S
/**
* List the discovery attributes of all the organizations under the root organization.
*
* @param filter The filter to be applied.
* @return The discovery attributes of the organizations.
* @throws OrganizationManagementException The exception thrown when listing discovery attributes of the
* organizations.
*/
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes() throws OrganizationManagementException;
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String filter)
throws OrganizationManagementException;

/**
* Get attribute based organization discovery handlers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,36 @@
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementClientException;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.organization.management.service.filter.ExpressionNode;
import org.wso2.carbon.identity.organization.management.service.filter.FilterTreeBuilder;
import org.wso2.carbon.identity.organization.management.service.filter.Node;
import org.wso2.carbon.identity.organization.management.service.filter.OperationNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.organization.discovery.service.constant.DiscoveryConstants.ORGANIZATION_NAME;
import static org.wso2.carbon.identity.organization.discovery.service.constant.DiscoveryConstants.SUPPORTED_OPERATIONS;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.AND;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_ATTRIBUTE_ALREADY_ADDED_FOR_ORGANIZATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_ATTRIBUTE_TAKEN;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_DISABLED;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DUPLICATE_DISCOVERY_ATTRIBUTE_TYPES;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_EMPTY_DISCOVERY_ATTRIBUTES;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_FILTER_FORMAT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNAUTHORIZED_ORG_FOR_DISCOVERY_ATTRIBUTE_MANAGEMENT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_COMPLEX_QUERY_IN_FILTER;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_DISCOVERY_ATTRIBUTE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.getOrganizationId;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.handleClientException;

Expand Down Expand Up @@ -134,9 +147,11 @@ public boolean isDiscoveryAttributeValueAvailable(String organizationId, String
}

@Override
public List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes() throws OrganizationManagementException {
public List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String filter)
throws OrganizationManagementException {

return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(getOrganizationId());
List<ExpressionNode> expressionNodes = getExpressionNodes(filter);
return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(getOrganizationId(), expressionNodes);
}

@Override
Expand Down Expand Up @@ -211,4 +226,55 @@ private void validateOrganizationDiscoveryAttributes(boolean excludeCurrentOrgan
}
}
}

private List<ExpressionNode> getExpressionNodes(String filter) throws OrganizationManagementClientException {

List<ExpressionNode> expressionNodes = new ArrayList<>();
if (StringUtils.isBlank(filter)) {
filter = StringUtils.EMPTY;
}
try {
if (StringUtils.isNotBlank(filter)) {
FilterTreeBuilder filterTreeBuilder = new FilterTreeBuilder(filter);
Node rootNode = filterTreeBuilder.buildTree();
setExpressionNodeList(rootNode, expressionNodes);
}
} catch (IOException e) {
throw handleClientException(ERROR_CODE_INVALID_FILTER_FORMAT);
}
return expressionNodes;
}

private void setExpressionNodeList(Node node, List<ExpressionNode> expression) throws
OrganizationManagementClientException {

if (node instanceof ExpressionNode) {
ExpressionNode expressionNode = (ExpressionNode) node;
String attributeValue = expressionNode.getAttributeValue();
String operation = expressionNode.getOperation();

if (StringUtils.isNotBlank(attributeValue)) {
if (!isFilteringAttributeSupported(attributeValue)) {
throw handleClientException(ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, attributeValue);
}
if (!SUPPORTED_OPERATIONS.contains(operation)) {
throw handleClientException(ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE,
operation, attributeValue);
}
expression.add(expressionNode);
}
} else if (node instanceof OperationNode) {
String operation = ((OperationNode) node).getOperation();
if (!StringUtils.equalsIgnoreCase(AND, operation)) {
throw handleClientException(ERROR_CODE_UNSUPPORTED_COMPLEX_QUERY_IN_FILTER);
}
setExpressionNodeList(node.getLeftNode(), expression);
setExpressionNodeList(node.getRightNode(), expression);
}
}

private boolean isFilteringAttributeSupported(String attributeValue) {

return ORGANIZATION_NAME.equalsIgnoreCase(attributeValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@

package org.wso2.carbon.identity.organization.discovery.service.constant;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CO;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EQ;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EW;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.SW;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_NAME_COLUMN;

/**
* This class holds the constants related to organization discovery service.
*/
public class DiscoveryConstants {

public static final String ENABLE_CONFIG = ".enable";
public static final String PRE_ADD_USER_EMAIL_DOMAIN_VALIDATE = "PRE_ADD_USER_EMAIL_DOMAIN_VALIDATE";
public static final String ORGANIZATION_NAME = "organizationName";
public static final Set<String> SUPPORTED_OPERATIONS;
public static final Map<String, String> ATTRIBUTE_COLUMN_MAP;

static {
Set<String> operations = new HashSet<>(Arrays.asList(SW, EW, CO, EQ));
SUPPORTED_OPERATIONS = Collections.unmodifiableSet(operations);
}

static {
Map<String, String> attributeMap = new HashMap<>();
attributeMap.put(ORGANIZATION_NAME, VIEW_NAME_COLUMN);
ATTRIBUTE_COLUMN_MAP = Collections.unmodifiableMap(attributeMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public class SQLConstants {
"UM_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ID + ";";

public static final String GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES = "SELECT UM_ORG_ID, UM_DISCOVERY_TYPE, " +
"UM_DISCOVERY_VALUE, UM_ORG_NAME FROM UM_ORG_DISCOVERY JOIN UM_ORG ON " +
"UM_ORG.UM_ID = UM_ORG_ID WHERE UM_ROOT_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID + ";";
"UM_DISCOVERY_VALUE, UM_ORG_NAME FROM UM_ORG_DISCOVERY JOIN UM_ORG ON UM_ORG.UM_ID = UM_ORG_ID WHERE ";

public static final String GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES_TAIL = "UM_ROOT_ORG_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID + ";";

public static final String GET_ORGANIZATION_ID_BY_DISCOVERY_ATTRIBUTE = "SELECT UM_ORG_ID FROM UM_ORG_DISCOVERY " +
"WHERE UM_DISCOVERY_TYPE = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TYPE + "; AND UM_DISCOVERY_VALUE = :"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.wso2.carbon.identity.organization.discovery.service.model.OrgDiscoveryAttribute;
import org.wso2.carbon.identity.organization.discovery.service.model.OrganizationDiscovery;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementServerException;
import org.wso2.carbon.identity.organization.management.service.filter.ExpressionNode;

import java.util.List;

Expand Down Expand Up @@ -102,12 +103,13 @@ void updateOrganizationDiscoveryAttributes(String organizationId, List<OrgDiscov
* List the discovery attributes of all the organizations under the given root organization.
*
* @param rootOrganizationId The root organization ID.
* @param expressionNodes The list of filters.
* @return The discovery attributes of the organizations.
* @throws OrganizationManagementServerException The server exception thrown when listing discovery attributes of
* the organizations.
*/
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String rootOrganizationId) throws
OrganizationManagementServerException;
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String rootOrganizationId, List<ExpressionNode>
expressionNodes) throws OrganizationManagementServerException;

/**
* Get the organization ID by discovery attribute in the hierarchy.
Expand Down
Loading
Loading