Search in sources :

Example 26 with Type

use of com.yahoo.elide.core.type.Type in project elide by yahoo.

the class TableType method resolveJoins.

/**
 * Must be called post construction of all the dynamic types to initialize table join fields.
 * @param tableTypes A map of table name to type.
 */
public void resolveJoins(Map<String, Type<?>> tableTypes) {
    table.getJoins().forEach(join -> {
        Type joinTableType = tableTypes.get(join.getTo());
        fields.put(join.getName(), new FieldType(join.getName(), joinTableType, buildAnnotations(join)));
    });
}
Also used : EnumType(javax.persistence.EnumType) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) JoinType(com.yahoo.elide.datastores.aggregation.annotation.JoinType) GenerationType(javax.persistence.GenerationType) Type(com.yahoo.elide.core.type.Type)

Example 27 with Type

use of com.yahoo.elide.core.type.Type in project elide by yahoo.

the class PermissionExpressionBuilder method buildUserCheckEntityAndAnyFieldExpression.

/**
 * Build an expression that strictly evaluates UserCheck's and ignores other checks for an entity.
 * expression = (entityRule AND (field1Rule OR field2Rule ... OR fieldNRule))
 * <p>
 * NOTE: This method returns _NO_ commit checks.
 *
 * @param resourceClass   Resource class
 * @param annotationClass Annotation class
 * @param scope    Request scope
 * @param <A>             type parameter
 * @return User check expression to evaluate
 */
public <A extends Annotation> Expression buildUserCheckEntityAndAnyFieldExpression(final Type<?> resourceClass, final Class<A> annotationClass, Set<String> requestedFields, final RequestScope scope) {
    final Function<Check, Expression> leafBuilderFn = (check) -> new CheckExpression(check, null, scope, null, cache);
    ParseTree classPermissions = entityDictionary.getPermissionsForClass(resourceClass, annotationClass);
    Expression entityExpression = normalizedExpressionFromParseTree(classPermissions, leafBuilderFn);
    Expression anyFieldExpression = buildAnyFieldOnlyExpression(new PermissionCondition(annotationClass, resourceClass), leafBuilderFn, requestedFields);
    if (entityExpression == null) {
        return anyFieldExpression;
    }
    return new AndExpression(entityExpression, anyFieldExpression);
}
Also used : CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) PermissionExpressionNormalizationVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionNormalizationVisitor) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) Function(java.util.function.Function) FAILURE(com.yahoo.elide.core.security.permissions.expressions.Expression.Results.FAILURE) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) SpecificFieldExpression(com.yahoo.elide.core.security.permissions.expressions.SpecificFieldExpression) PersistentResource(com.yahoo.elide.core.PersistentResource) PermissionExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionVisitor) ParseTree(org.antlr.v4.runtime.tree.ParseTree) NO_EVALUATION_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.NO_EVALUATION_EXPRESSION) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Check(com.yahoo.elide.core.security.checks.Check) PermissionToFilterExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) Set(java.util.Set) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) List(java.util.List) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Type(com.yahoo.elide.core.type.Type) Annotation(java.lang.annotation.Annotation) FALSE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.FALSE_USER_CHECK_EXPRESSION) TRUE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.TRUE_USER_CHECK_EXPRESSION) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) SpecificFieldExpression(com.yahoo.elide.core.security.permissions.expressions.SpecificFieldExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) Check(com.yahoo.elide.core.security.checks.Check) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression)

Example 28 with Type

use of com.yahoo.elide.core.type.Type in project elide by yahoo.

the class AbstractPermissionExecutor method checkPermissions.

/**
 * First attempts to check user permissions (by looking in the cache and if not present by executing user
 * permissions).  If user permissions don't short circuit the check, run the provided expression executor.
 *
 * @param <A> type parameter
 * @param resourceClass Resource class
 * @param annotationClass Annotation class
 * @param fields Set of all field names that is being accessed
 * @param expressionSupplier Builds a permission expression.
 * @param expressionExecutor Evaluates the expression (post user check evaluation)
 */
protected <A extends Annotation> ExpressionResult checkPermissions(Type<?> resourceClass, Class<A> annotationClass, Set<String> fields, Supplier<Expression> expressionSupplier, Optional<Function<Expression, ExpressionResult>> expressionExecutor) {
    // If the user check has already been evaluated before, return the result directly and save the building cost
    ImmutableSet<String> immutableFields = fields == null ? null : ImmutableSet.copyOf(fields);
    ExpressionResult expressionResult = userPermissionCheckCache.get(Triple.of(annotationClass, resourceClass, immutableFields));
    if (expressionResult == PASS) {
        return expressionResult;
    }
    Expression expression = expressionSupplier.get();
    if (expressionResult == null) {
        expressionResult = executeExpressions(expression, annotationClass, Expression.EvaluationMode.USER_CHECKS_ONLY);
        userPermissionCheckCache.put(Triple.of(annotationClass, resourceClass, immutableFields), expressionResult);
        if (expressionResult == PASS) {
            return expressionResult;
        }
    }
    return expressionExecutor.map(executor -> executor.apply(expression)).orElse(expressionResult);
}
Also used : PASS(com.yahoo.elide.core.security.permissions.ExpressionResult.PASS) Getter(lombok.Getter) HashMap(java.util.HashMap) DEFERRED(com.yahoo.elide.core.security.permissions.ExpressionResult.DEFERRED) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DeletePermission(com.yahoo.elide.annotation.DeletePermission) PermissionExpressionBuilder(com.yahoo.elide.core.security.permissions.PermissionExpressionBuilder) Map(java.util.Map) Triple(org.apache.commons.lang3.tuple.Triple) RequestScope(com.yahoo.elide.core.RequestScope) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Set(java.util.Set) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) ForbiddenAccessException(com.yahoo.elide.core.exceptions.ForbiddenAccessException) ExpressionResult(com.yahoo.elide.core.security.permissions.ExpressionResult) ReadPermission(com.yahoo.elide.annotation.ReadPermission) ExpressionResultCache(com.yahoo.elide.core.security.permissions.ExpressionResultCache) Type(com.yahoo.elide.core.type.Type) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Queue(java.util.Queue) AllArgsConstructor(lombok.AllArgsConstructor) FAIL(com.yahoo.elide.core.security.permissions.ExpressionResult.FAIL) ExpressionResult(com.yahoo.elide.core.security.permissions.ExpressionResult) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression)

Example 29 with Type

use of com.yahoo.elide.core.type.Type in project elide by yahoo.

the class QueryTranslator method visitQuery.

@Override
public NativeQuery.NativeQueryBuilder visitQuery(Query query) {
    NativeQuery.NativeQueryBuilder builder = query.getSource().accept(this);
    if (query.isNested()) {
        NativeQuery innerQuery = builder.build();
        builder = NativeQuery.builder().fromClause(getFromClause("(" + innerQuery + ")", applyQuotes(query.getSource().getAlias()), dialect));
    }
    Set<String> joinExpressions = new LinkedHashSet<>();
    builder.projectionClause(constructProjectionWithReference(query));
    // Handles join for all type of column projects - dimensions, metrics and time dimention
    joinExpressions.addAll(extractJoinExpressions(query));
    Set<ColumnProjection> groupByDimensions = query.getAllDimensionProjections().stream().map(SQLColumnProjection.class::cast).filter(SQLColumnProjection::isProjected).collect(Collectors.toCollection(LinkedHashSet::new));
    if (!groupByDimensions.isEmpty()) {
        if (!query.getMetricProjections().isEmpty()) {
            builder.groupByClause("GROUP BY " + groupByDimensions.stream().map(SQLColumnProjection.class::cast).map((column) -> column.toSQL(query, metaDataStore)).collect(Collectors.joining(", ")));
        }
    }
    if (query.getWhereFilter() != null) {
        builder.whereClause("WHERE " + translateFilterExpression(query.getWhereFilter(), path -> generatePredicatePathReference(path, query)));
        joinExpressions.addAll(extractJoinExpressions(query, query.getWhereFilter()));
    }
    if (query.getHavingFilter() != null) {
        builder.havingClause("HAVING " + translateFilterExpression(query.getHavingFilter(), (path) -> constructHavingClauseWithReference(path, query)));
        joinExpressions.addAll(extractJoinExpressions(query, query.getHavingFilter()));
    }
    if (query.getSorting() != null) {
        Map<Path, Sorting.SortOrder> sortClauses = query.getSorting().getSortingPaths();
        builder.orderByClause(extractOrderBy(sortClauses, query));
        joinExpressions.addAll(extractJoinExpressions(query, sortClauses));
    }
    Pagination pagination = query.getPagination();
    if (pagination != null) {
        builder.offsetLimitClause(dialect.generateOffsetLimitClause(pagination.getOffset(), pagination.getLimit()));
    }
    return builder.joinClause(String.join(" ", joinExpressions));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Path(com.yahoo.elide.core.Path) Function(java.util.function.Function) Argument(com.yahoo.elide.core.request.Argument) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) Map(java.util.Map) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) FilterTranslator(com.yahoo.elide.datastores.jpql.filter.FilterTranslator) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) QueryVisitor(com.yahoo.elide.datastores.aggregation.query.QueryVisitor) TableContext(com.yahoo.elide.datastores.aggregation.metadata.TableContext) JoinExpressionExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor) Sorting(com.yahoo.elide.core.request.Sorting) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext) Collection(java.util.Collection) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Set(java.util.Set) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Stream(java.util.stream.Stream) Pagination(com.yahoo.elide.core.request.Pagination) Type(com.yahoo.elide.core.type.Type) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) Path(com.yahoo.elide.core.Path) Pagination(com.yahoo.elide.core.request.Pagination) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Example 30 with Type

use of com.yahoo.elide.core.type.Type in project elide by yahoo.

the class JoinPathTest method init.

@BeforeAll
public static void init() {
    Set<Type<?>> models = new HashSet<>();
    models.add(ClassType.of(PlayerStats.class));
    models.add(ClassType.of(CountryView.class));
    models.add(ClassType.of(Country.class));
    models.add(ClassType.of(SubCountry.class));
    models.add(ClassType.of(Player.class));
    models.add(ClassType.of(PlayerRanking.class));
    models.add(ClassType.of(CountryViewNested.class));
    models.add(ClassType.of(PlayerStatsWithView.class));
    EntityDictionary dictionary = EntityDictionary.builder().build();
    models.stream().forEach(dictionary::bindEntity);
    store = new MetaDataStore(dictionary.getScanner(), models, true);
    store.populateEntityDictionary(dictionary);
    DataSource mockDataSource = mock(DataSource.class);
    // The query engine populates the metadata store with actual tables.
    new SQLQueryEngine(store, (unused) -> new ConnectionDetails(mockDataSource, SQLDialectFactory.getDefaultDialect()));
}
Also used : SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) Player(example.Player) CountryViewNested(example.dimensions.CountryViewNested) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) PlayerStats(example.PlayerStats) SubCountry(example.dimensions.SubCountry) DataSource(javax.sql.DataSource) ClassType(com.yahoo.elide.core.type.ClassType) Type(com.yahoo.elide.core.type.Type) PlayerStatsWithView(example.PlayerStatsWithView) PlayerRanking(example.PlayerRanking) SubCountry(example.dimensions.SubCountry) Country(example.dimensions.Country) CountryView(example.dimensions.CountryView) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) HashSet(java.util.HashSet) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Type (com.yahoo.elide.core.type.Type)35 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)22 Set (java.util.Set)17 List (java.util.List)16 Map (java.util.Map)16 ClassType (com.yahoo.elide.core.type.ClassType)15 Function (java.util.function.Function)14 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)13 Annotation (java.lang.annotation.Annotation)12 Collectors (java.util.stream.Collectors)12 Collection (java.util.Collection)11 ReadPermission (com.yahoo.elide.annotation.ReadPermission)10 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)9 Optional (java.util.Optional)9 RequestScope (com.yahoo.elide.core.RequestScope)8 Objects (java.util.Objects)8 Path (com.yahoo.elide.core.Path)7 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)7