Skip to content

Commit

Permalink
Add Entity Type condition to limit the scope of NOT IN
Browse files Browse the repository at this point in the history
  • Loading branch information
mohityadav766 committed Dec 12, 2024
1 parent 04cfde7 commit db1d0dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.openmetadata.service.util.FullyQualifiedName;

public class ListFilter extends Filter<ListFilter> {
public static final String NULL_PARAM = "null";

public ListFilter() {
this(Include.NON_DELETED);
}
Expand Down Expand Up @@ -117,13 +119,17 @@ public String getTestSuiteFQNCondition() {
private String getDomainCondition(String tableName) {
String domainId = getQueryParam("domainId");
String entityIdColumn = nullOrEmpty(tableName) ? "id" : (tableName + ".id");

if (domainId == null) {
return "";
} else if ("null".equals(domainId)) {
} else if (NULL_PARAM.equals(domainId)) {
String entityType = getQueryParam("entityType");
String entityTypeCondition =
nullOrEmpty(entityType)
? ""
: String.format("AND entity_relationship.toEntity='%s'", entityType);
return String.format(
"(%s NOT IN (SELECT entity_relationship.toId FROM entity_relationship WHERE entity_relationship.fromEntity='domain' AND relation=10))",
entityIdColumn);
"(%s NOT IN (SELECT entity_relationship.toId FROM entity_relationship WHERE entity_relationship.fromEntity='domain' %s AND relation=10))",
entityIdColumn, entityTypeCondition);
} else {
return String.format(
"(%s in (SELECT entity_relationship.toId FROM entity_relationship WHERE entity_relationship.fromEntity='domain' AND entity_relationship.fromId IN (%s) AND "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public ResultList<T> listInternal(
authorizer.authorize(securityContext, operationContext, resourceContext);

// Add Domain Filter
EntityUtil.addDomainQueryParam(securityContext, filter);
EntityUtil.addDomainQueryParam(securityContext, filter, entityType);

// List
ResultList<T> resultList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty;
import static org.openmetadata.schema.type.Include.ALL;
import static org.openmetadata.schema.type.Include.NON_DELETED;
import static org.openmetadata.service.jdbi3.ListFilter.NULL_PARAM;
import static org.openmetadata.service.jdbi3.RoleRepository.DOMAIN_ONLY_ACCESS_ROLE;
import static org.openmetadata.service.security.DefaultAuthorizer.getSubjectContext;

Expand Down Expand Up @@ -688,19 +689,18 @@ public static List<EntityReference> mergedInheritedEntityRefs(
return result.stream().toList();
}

public static void addDomainQueryParam(SecurityContext securityContext, ListFilter filter) {
public static void addDomainQueryParam(
SecurityContext securityContext, ListFilter filter, String entityType) {
SubjectContext subjectContext = getSubjectContext(securityContext);
// If the User is admin then no need to add domainId in the query param
// Also if there are domain restriction on the subject context via role
if (!subjectContext.isAdmin()
&& !subjectContext.isBot()
&& subjectContext.hasAnyRole(DOMAIN_ONLY_ACCESS_ROLE)) {
if (!subjectContext.isAdmin() && subjectContext.hasAnyRole(DOMAIN_ONLY_ACCESS_ROLE)) {
if (!nullOrEmpty(subjectContext.getUserDomains())) {
filter.addQueryParam(
"domainId", getCommaSeparatedIdsFromRefs(subjectContext.getUserDomains()));
} else {
// TODO: Hack :(
filter.addQueryParam("domainId", "null");
filter.addQueryParam("domainId", NULL_PARAM);
filter.addQueryParam("entityType", entityType);
}
}
}
Expand Down

0 comments on commit db1d0dd

Please sign in to comment.