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;
}
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);
}
}
}
}
}
}
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;
}
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;
}
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;
}
Aggregations