Search in sources :

Example 1 with SecurityState

use of com.haulmont.cuba.core.entity.SecurityState in project cuba by cuba-platform.

the class JSONConverter method asJavaTree.

protected void asJavaTree(CommitRequest commitRequest, Entity entity, MetaClass metaClass, JSONObject json) throws JSONException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, IntrospectionException, ParseException {
    Iterator iter = json.keys();
    while (iter.hasNext()) {
        String propertyName = (String) iter.next();
        if ("id".equals(propertyName)) {
            // id was parsed already
            continue;
        }
        // version is readonly property
        if ("version".equals(propertyName)) {
            continue;
        }
        if (entity instanceof BaseGenericIdEntity && "__securityToken".equals(propertyName)) {
            byte[] securityToken = Base64.getDecoder().decode(json.getString("__securityToken"));
            SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState((BaseGenericIdEntity) entity);
            BaseEntityInternalAccess.setSecurityToken(securityState, securityToken);
            continue;
        }
        MetaPropertyPath metaPropertyPath = metadata.getTools().resolveMetaPropertyPath(metaClass, propertyName);
        checkNotNullArgument(metaPropertyPath, "Could not resolve property '%s' in '%s'", propertyName, metaClass);
        MetaProperty property = metaPropertyPath.getMetaProperty();
        if (!attrModifyPermitted(metaClass, property.getName()))
            continue;
        if (entity instanceof BaseGenericIdEntity && DynamicAttributesUtils.isDynamicAttribute(propertyName) && ((BaseGenericIdEntity) entity).getDynamicAttributes() == null) {
            ConverterHelper.fetchDynamicAttributes(entity);
        }
        if (json.get(propertyName) == null) {
            setField(entity, propertyName, null);
            continue;
        }
        switch(property.getType()) {
            case DATATYPE:
                String value = null;
                if (!json.isNull(propertyName)) {
                    value = json.get(propertyName).toString();
                }
                setField(entity, propertyName, property.getRange().asDatatype().parse(value));
                break;
            case ENUM:
                if (!json.isNull(propertyName)) {
                    setField(entity, propertyName, property.getRange().asEnumeration().parse(json.getString(propertyName)));
                } else {
                    setField(entity, propertyName, null);
                }
                break;
            case COMPOSITION:
            case ASSOCIATION:
                if ("null".equals(json.get(propertyName).toString())) {
                    setField(entity, propertyName, null);
                    break;
                }
                MetaClass propertyMetaClass = propertyMetaClass(property);
                // checks if the user permitted to read and update a property
                if (!updatePermitted(propertyMetaClass) && !readPermitted(propertyMetaClass))
                    break;
                if (!property.getRange().getCardinality().isMany()) {
                    JSONObject jsonChild = json.getJSONObject(propertyName);
                    Entity child;
                    MetaClass childMetaClass;
                    if (jsonChild.has("id")) {
                        String id = jsonChild.getString("id");
                        // will be registered later
                        if (commitRequest.getCommitIds().contains(id)) {
                            EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
                            if (loadInfo == null)
                                throw new IllegalArgumentException("Unable to parse ID: " + id);
                            Entity ref = metadata.create(loadInfo.getMetaClass());
                            ref.setValue("id", loadInfo.getId());
                            setField(entity, propertyName, ref);
                            break;
                        }
                        InstanceRef ref = commitRequest.parseInstanceRefAndRegister(id);
                        childMetaClass = ref.getMetaClass();
                        child = ref.getInstance();
                    } else {
                        childMetaClass = property.getRange().asClass();
                        child = metadata.create(childMetaClass);
                    }
                    asJavaTree(commitRequest, child, childMetaClass, jsonChild);
                    setField(entity, propertyName, child);
                } else {
                    JSONArray jsonArray = json.getJSONArray(propertyName);
                    Collection<Object> coll = property.getRange().isOrdered() ? new ArrayList<>() : new HashSet<>();
                    setField(entity, propertyName, coll);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Object arrayValue = jsonArray.get(i);
                        if (arrayValue == null)
                            coll.add(null);
                        else {
                            // assuming no simple type here
                            JSONObject jsonChild = (JSONObject) arrayValue;
                            InstanceRef ref = commitRequest.parseInstanceRefAndRegister(jsonChild.getString("id"));
                            Entity child = ref.getInstance();
                            coll.add(child);
                            asJavaTree(commitRequest, child, ref.getMetaClass(), jsonChild);
                        }
                    }
                }
                break;
            default:
                throw new IllegalStateException("Unknown property type");
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) JSONArray(org.json.JSONArray) SecurityState(com.haulmont.cuba.core.entity.SecurityState) MetaClass(com.haulmont.chile.core.model.MetaClass) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with SecurityState

use of com.haulmont.cuba.core.entity.SecurityState in project cuba by cuba-platform.

the class RestControllerUtils method addInaccessibleAttribute.

private void addInaccessibleAttribute(BaseGenericIdEntity entity, String property) {
    SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState(entity);
    String[] attributes = BaseEntityInternalAccess.getInaccessibleAttributes(securityState);
    attributes = attributes == null ? new String[1] : Arrays.copyOf(attributes, attributes.length + 1);
    attributes[attributes.length - 1] = property;
    BaseEntityInternalAccess.setInaccessibleAttributes(securityState, attributes);
}
Also used : SecurityState(com.haulmont.cuba.core.entity.SecurityState)

Example 3 with SecurityState

use of com.haulmont.cuba.core.entity.SecurityState in project cuba by cuba-platform.

the class AttributeAccessUpdater method updateAttributeAccess.

/**
 * Updates security state of a given entity instance.
 *
 * @param entity instance
 */
public void updateAttributeAccess(Entity entity) {
    SecurityState securityState = service.computeSecurityState(entity);
    BaseEntityInternalAccess.setSecurityState(entity, securityState);
}
Also used : SecurityState(com.haulmont.cuba.core.entity.SecurityState)

Example 4 with SecurityState

use of com.haulmont.cuba.core.entity.SecurityState in project cuba by cuba-platform.

the class AttributeAccessServiceBean method computeSecurityState.

@Override
public SecurityState computeSecurityState(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    SecurityState state;
    String storeName = metadataTools.getStoreName(metadata.getClassNN(entity.getClass()));
    Transaction tx = persistence.createTransaction(storeName);
    try {
        EntityManager em = persistence.getEntityManager(storeName);
        Entity managedEntity = em.merge(entity);
        support.setupAttributeAccess(managedEntity);
        state = BaseEntityInternalAccess.getSecurityState(managedEntity);
    // do not commit the transaction
    } finally {
        tx.end();
    }
    return state;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SecurityState(com.haulmont.cuba.core.entity.SecurityState)

Example 5 with SecurityState

use of com.haulmont.cuba.core.entity.SecurityState in project cuba by cuba-platform.

the class AttributeAccessSupport method calculateComponentState.

protected ComponentState calculateComponentState(Entity entity, MetaPropertyPath propertyPath) {
    MetaProperty[] metaProperties = propertyPath.getMetaProperties();
    ComponentState componentState = new ComponentState();
    for (int i = 0; i < metaProperties.length; i++) {
        MetaProperty metaProperty = metaProperties[i];
        String name = metaProperty.getName();
        SecurityState securityState = getSecurityState(entity);
        if (securityState != null) {
            componentState.hidden = test(componentState.hidden, securityState.getHiddenAttributes(), name);
            componentState.readOnly = test(componentState.readOnly, securityState.getReadonlyAttributes(), name);
            if (i == metaProperties.length - 1) {
                componentState.required = test(componentState.required, securityState.getRequiredAttributes(), name);
            }
        }
        if (i != metaProperties.length - 1) {
            entity = entity.getValue(name);
            if (entity == null) {
                break;
            }
        }
    }
    return componentState;
}
Also used : MetaProperty(com.haulmont.chile.core.model.MetaProperty) SecurityState(com.haulmont.cuba.core.entity.SecurityState)

Aggregations

SecurityState (com.haulmont.cuba.core.entity.SecurityState)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)3 Entity (com.haulmont.cuba.core.entity.Entity)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 EntityManager (com.haulmont.cuba.core.EntityManager)1 Transaction (com.haulmont.cuba.core.Transaction)1 ParseException (java.text.ParseException)1 Element (org.dom4j.Element)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1