Search in sources :

Example 71 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RdbmsStore method getNotPermittedSelectIndexes.

protected List<Integer> getNotPermittedSelectIndexes(QueryParser queryParser) {
    List<Integer> indexes = new ArrayList<>();
    if (isAuthorizationRequired()) {
        int index = 0;
        for (QueryParser.QueryPath path : queryParser.getQueryPaths()) {
            if (path.isSelectedPath()) {
                MetaClass metaClass = metadata.getClassNN(path.getEntityName());
                if (!Objects.equals(path.getPropertyPath(), path.getVariableName()) && !isEntityAttrViewPermitted(metaClass.getPropertyPath(path.getPropertyPath()))) {
                    indexes.add(index);
                }
                index++;
            }
        }
    }
    return indexes;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 72 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RdbmsStore method updateReferences.

protected void updateReferences(Entity entity, Entity refEntity, Set<Entity> visited) {
    if (entity == null || refEntity == null || visited.contains(entity))
        return;
    visited.add(entity);
    MetaClass refEntityMetaClass = refEntity.getMetaClass();
    for (MetaProperty property : entity.getMetaClass().getProperties()) {
        if (!property.getRange().isClass() || !property.getRange().asClass().equals(refEntityMetaClass))
            continue;
        if (PersistenceHelper.isLoaded(entity, property.getName())) {
            if (property.getRange().getCardinality().isMany()) {
                Collection collection = entity.getValue(property.getName());
                if (collection != null) {
                    for (Object obj : collection) {
                        updateReferences((Entity) obj, refEntity, visited);
                    }
                }
            } else {
                Entity value = entity.getValue(property.getName());
                if (value != null) {
                    if (value.getId().equals(refEntity.getId())) {
                        if (entity instanceof AbstractInstance) {
                            if (property.isReadOnly() && metadata.getTools().isNotPersistent(property)) {
                                continue;
                            }
                            ((AbstractInstance) entity).setValue(property.getName(), refEntity, false);
                        }
                    } else {
                        updateReferences(value, refEntity, visited);
                    }
                }
            }
        }
    }
}
Also used : AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 73 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RdbmsStore method checkValueQueryPermissions.

protected boolean checkValueQueryPermissions(QueryParser queryParser) {
    if (isAuthorizationRequired()) {
        queryParser.getQueryPaths().stream().filter(path -> !path.isSelectedPath()).forEach(path -> {
            MetaClass metaClass = metadata.getClassNN(path.getEntityName());
            MetaPropertyPath propertyPath = metaClass.getPropertyPath(path.getPropertyPath());
            if (propertyPath == null) {
                throw new IllegalStateException(String.format("query path '%s' is unresolved", path.getFullPath()));
            }
            if (!isEntityAttrViewPermitted(propertyPath)) {
                throw new AccessDeniedException(PermissionType.ENTITY_ATTR, metaClass + "." + path.getFullPath());
            }
        });
        MetaClass metaClass = metadata.getClassNN(queryParser.getEntityName());
        if (!isEntityOpPermitted(metaClass, EntityOp.READ)) {
            log.debug("reading of {} not permitted, returning empty list", metaClass);
            return false;
        }
        if (security.hasInMemoryConstraints(metaClass, ConstraintOperationType.READ, ConstraintOperationType.ALL)) {
            String msg = String.format("%s is not permitted for %s", ConstraintOperationType.READ, metaClass.getName());
            if (serverConfig.getDisableLoadValuesIfConstraints()) {
                throw new RowLevelSecurityException(msg, metaClass.getName(), ConstraintOperationType.READ);
            } else {
                log.debug(msg);
            }
        }
        Set<String> entityNames = queryParser.getAllEntityNames();
        entityNames.remove(metaClass.getName());
        for (String entityName : entityNames) {
            MetaClass entityMetaClass = metadata.getClassNN(entityName);
            if (!isEntityOpPermitted(entityMetaClass, EntityOp.READ)) {
                log.debug("reading of {} not permitted, returning empty list", entityMetaClass);
                return false;
            }
            if (security.hasConstraints(entityMetaClass)) {
                String msg = String.format("%s is not permitted for %s", ConstraintOperationType.READ, entityName);
                if (serverConfig.getDisableLoadValuesIfConstraints()) {
                    throw new RowLevelSecurityException(msg, entityName, ConstraintOperationType.READ);
                } else {
                    log.debug(msg);
                }
            }
        }
    }
    return true;
}
Also used : com.haulmont.cuba.core(com.haulmont.cuba.core) java.util(java.util) PermissionType(com.haulmont.cuba.security.entity.PermissionType) LoggerFactory(org.slf4j.LoggerFactory) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) NoResultException(javax.persistence.NoResultException) ConstraintOperationType(com.haulmont.cuba.security.entity.ConstraintOperationType) MetaClass(com.haulmont.chile.core.model.MetaClass) Scope(org.springframework.context.annotation.Scope) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) StringUtils.isBlank(org.apache.commons.lang.StringUtils.isBlank) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) EntityFetcher(com.haulmont.cuba.core.sys.EntityFetcher) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Collectors(java.util.stream.Collectors) Preconditions(com.haulmont.bali.util.Preconditions) DynamicAttributesManagerAPI(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesManagerAPI) Component(org.springframework.stereotype.Component) EntityOp(com.haulmont.cuba.security.entity.EntityOp) AppContext(com.haulmont.cuba.core.sys.AppContext) EntityAttrAccess(com.haulmont.cuba.security.entity.EntityAttrAccess) QueryResultsManagerAPI(com.haulmont.cuba.core.app.queryresults.QueryResultsManagerAPI) Session(com.haulmont.chile.core.model.Session) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 74 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RdbmsStore method load.

@Nullable
@Override
public <E extends Entity> E load(LoadContext<E> context) {
    if (log.isDebugEnabled()) {
        log.debug("load: metaClass={}, id={}, view={}", context.getMetaClass(), context.getId(), context.getView());
    }
    final MetaClass metaClass = metadata.getSession().getClassNN(context.getMetaClass());
    if (!isEntityOpPermitted(metaClass, EntityOp.READ)) {
        log.debug("reading of {} not permitted, returning null", metaClass);
        return null;
    }
    E result = null;
    boolean needToApplyConstraints = needToApplyByPredicate(context);
    try (Transaction tx = createLoadTransaction()) {
        final EntityManager em = persistence.getEntityManager(storeName);
        if (!context.isSoftDeletion())
            em.setSoftDeletion(false);
        persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints());
        // If maxResults=1 and the query is not by ID we should not use getSingleResult() for backward compatibility
        boolean singleResult = !(context.getQuery() != null && context.getQuery().getMaxResults() == 1 && context.getQuery().getQueryString() != null);
        View view = createRestrictedView(context);
        com.haulmont.cuba.core.Query query = createQuery(em, context, singleResult);
        query.setView(view);
        // noinspection unchecked
        List<E> resultList = executeQuery(query, singleResult);
        if (!resultList.isEmpty()) {
            result = resultList.get(0);
        }
        if (result != null && needToApplyInMemoryReadConstraints(context) && security.filterByConstraints(result)) {
            result = null;
        }
        if (result instanceof BaseGenericIdEntity && context.isLoadDynamicAttributes()) {
            dynamicAttributesManagerAPI.fetchDynamicAttributes(Collections.singletonList((BaseGenericIdEntity) result), collectEntityClassesWithDynamicAttributes(context.getView()));
        }
        if (result != null && needToApplyConstraints) {
            security.calculateFilteredData(result);
        }
        if (result != null) {
            attributeSecurity.onLoad(result, view);
        }
        tx.commit();
    }
    if (result != null) {
        if (needToApplyConstraints) {
            security.applyConstraints(result);
        }
        attributeSecurity.afterLoad(result);
    }
    return result;
}
Also used : com.haulmont.cuba.core(com.haulmont.cuba.core) MetaClass(com.haulmont.chile.core.model.MetaClass) Nullable(javax.annotation.Nullable)

Example 75 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RelatedEntitiesServiceBean method getRelatedIds.

@SuppressWarnings("unchecked")
@Override
public List<Object> getRelatedIds(List<Object> parentIds, String parentMetaClass, String relationProperty) {
    checkNotNullArgument(parentIds, "parents argument is null");
    checkNotNullArgument(parentMetaClass, "parentMetaClass argument is null");
    checkNotNullArgument(relationProperty, "relationProperty argument is null");
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(metadata.getClassNN(parentMetaClass));
    Class parentClass = metaClass.getJavaClass();
    MetaProperty metaProperty = metaClass.getPropertyNN(relationProperty);
    // return empty list only after all argument checks
    if (parentIds.isEmpty()) {
        return Collections.emptyList();
    }
    MetaClass propertyMetaClass = extendedEntities.getEffectiveMetaClass(metaProperty.getRange().asClass());
    Class propertyClass = propertyMetaClass.getJavaClass();
    List<Object> relatedIds = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String parentPrimaryKey = metadata.getTools().getPrimaryKeyName(metaClass);
        String queryString = "select x from " + parentMetaClass + " x where x." + parentPrimaryKey + " in :ids";
        Query query = em.createQuery(queryString);
        String relatedPrimaryKey = metadata.getTools().getPrimaryKeyName(propertyMetaClass);
        View view = new View(parentClass);
        view.addProperty(relationProperty, new View(propertyClass).addProperty(relatedPrimaryKey));
        query.setView(view);
        query.setParameter("ids", parentIds);
        List<Entity> resultList = query.getResultList();
        for (Entity e : resultList) {
            Object value = e.getValue(relationProperty);
            if (value instanceof Entity) {
                relatedIds.add(((Entity) value).getId());
            } else if (value instanceof Collection) {
                for (Object collectionItem : (Collection) value) {
                    relatedIds.add(((Entity) collectionItem).getId());
                }
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return relatedIds;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) View(com.haulmont.cuba.core.global.View) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10