Search in sources :

Example 1 with CubaEntityFetchGroup

use of com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup in project cuba by cuba-platform.

the class AttributeSecuritySupport method applySecurityToFetchGroup.

protected void applySecurityToFetchGroup(Entity entity) {
    if (entity == null) {
        return;
    }
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    FetchGroupTracker fetchGroupTracker = (FetchGroupTracker) entity;
    FetchGroup fetchGroup = fetchGroupTracker._persistence_getFetchGroup();
    SecurityState securityState = getSecurityState(entity);
    if (fetchGroup != null) {
        List<String> attributesToRemove = new ArrayList<>();
        for (String attrName : fetchGroup.getAttributeNames()) {
            String[] parts = attrName.split("\\.");
            if (parts.length > 0 && BaseEntityInternalAccess.isHiddenOrReadOnly(securityState, parts[0])) {
                attributesToRemove.add(attrName);
            } else {
                MetaClass currentMetaClass = metaClass;
                for (String part : parts) {
                    if (!security.isEntityAttrUpdatePermitted(currentMetaClass, part)) {
                        attributesToRemove.add(attrName);
                        break;
                    }
                    MetaProperty metaProperty = currentMetaClass.getPropertyNN(part);
                    if (metaProperty.getRange().isClass()) {
                        currentMetaClass = metaProperty.getRange().asClass();
                    }
                }
            }
        }
        if (!attributesToRemove.isEmpty()) {
            List<String> attributeNames = new ArrayList<>(fetchGroup.getAttributeNames());
            attributeNames.removeAll(attributesToRemove);
            fetchGroupTracker._persistence_setFetchGroup(new CubaEntityFetchGroup(attributeNames));
        }
    } else {
        List<String> attributeNames = new ArrayList<>();
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            String propertyName = metaProperty.getName();
            if (metadataTools.isSystem(metaProperty)) {
                attributeNames.add(propertyName);
            }
            if (security.isEntityAttrUpdatePermitted(metaClass, propertyName) && !BaseEntityInternalAccess.isHiddenOrReadOnly(securityState, propertyName)) {
                attributeNames.add(metaProperty.getName());
            }
        }
        fetchGroupTracker._persistence_setFetchGroup(new CubaEntityFetchGroup(attributeNames));
    }
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) CubaEntityFetchGroup(com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup) MetaClass(com.haulmont.chile.core.model.MetaClass) FetchGroup(org.eclipse.persistence.queries.FetchGroup) CubaEntityFetchGroup(com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 CubaEntityFetchGroup (com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup)1 FetchGroup (org.eclipse.persistence.queries.FetchGroup)1 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)1