Search in sources :

Example 11 with CrudEntityContext

use of io.jmix.core.accesscontext.CrudEntityContext in project jmix by jmix-framework.

the class DataStoreCrudListener method beforeEntityLoad.

public void beforeEntityLoad(DataStoreBeforeEntityLoadEvent event) {
    LoadContext<?> context = event.getLoadContext();
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(context.getEntityMetaClass());
    CrudEntityContext entityContext = new CrudEntityContext(metaClass);
    accessManager.applyConstraints(entityContext, context.getAccessConstraints());
    if (!entityContext.isReadPermitted()) {
        log.debug("Reading entity {} is not permitted by access constraints", metaClass);
        event.setLoadPrevented();
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext)

Example 12 with CrudEntityContext

use of io.jmix.core.accesscontext.CrudEntityContext in project jmix by jmix-framework.

the class DataStoreCrudListener method beforeEntityCount.

public void beforeEntityCount(DataStoreBeforeEntityCountEvent event) {
    LoadContext<?> context = event.getLoadContext();
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(context.getEntityMetaClass());
    CrudEntityContext entityContext = new CrudEntityContext(metaClass);
    accessManager.applyConstraints(entityContext, context.getAccessConstraints());
    if (!entityContext.isReadPermitted()) {
        log.debug("Reading entity {} is not permitted by access constraints", metaClass);
        event.setCountPrevented();
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext)

Example 13 with CrudEntityContext

use of io.jmix.core.accesscontext.CrudEntityContext in project jmix by jmix-framework.

the class DataStoreCrudListener method beforeEntitySave.

@Override
public void beforeEntitySave(DataStoreBeforeEntitySaveEvent event) {
    SaveContext context = event.getSaveContext();
    Collection<AccessConstraint<?>> accessConstraints = context.getAccessConstraints();
    if (accessConstraints.isEmpty()) {
        return;
    }
    Map<MetaClass, CrudEntityContext> accessCache = new HashMap<>();
    for (Object entity : context.getEntitiesToSave()) {
        if (entity == null) {
            continue;
        }
        MetaClass metaClass = metadata.getClass(entity);
        CrudEntityContext entityContext = accessCache.computeIfAbsent(metaClass, key -> evaluateCrudAccess(key, accessConstraints));
        if (entityStates.isNew(entity)) {
            if (!entityContext.isCreatePermitted()) {
                throw new AccessDeniedException("entity", metaClass.getName(), "create");
            }
        } else if (!entityContext.isUpdatePermitted()) {
            throw new AccessDeniedException("entity", metaClass.getName(), "update");
        }
    }
    for (Object entity : context.getEntitiesToRemove()) {
        if (entity == null) {
            continue;
        }
        MetaClass metaClass = metadata.getClass(entity);
        CrudEntityContext entityContext = accessCache.computeIfAbsent(metaClass, key -> evaluateCrudAccess(key, accessConstraints));
        if (!entityContext.isDeletePermitted()) {
            throw new AccessDeniedException("entity", metaClass.getName(), "update");
        }
    }
}
Also used : AccessDeniedException(io.jmix.core.security.AccessDeniedException) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) HashMap(java.util.HashMap) AccessConstraint(io.jmix.core.constraint.AccessConstraint)

Example 14 with CrudEntityContext

use of io.jmix.core.accesscontext.CrudEntityContext in project jmix by jmix-framework.

the class BaseDatabaseRoleProvider method deleteRole.

@Override
public boolean deleteRole(T role) {
    CrudEntityContext entityContext = new CrudEntityContext(metadata.getClass(getRoleClass()));
    accessManager.applyRegisteredConstraints(entityContext);
    if (!entityContext.isDeletePermitted()) {
        return false;
    }
    String roleDatabaseId = role.getCustomProperties().get("databaseId");
    Object roleEntity;
    if (Strings.isNullOrEmpty(roleDatabaseId)) {
        throw new IllegalArgumentException(String.format("Database ID of role with code \"%s\" is empty", role.getCode()));
    } else {
        UUID roleEntityId = UUID.fromString(roleDatabaseId);
        roleEntity = dataManager.getReference(getRoleClass(), roleEntityId);
        dataManager.remove(roleEntity);
    }
    return true;
}
Also used : CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) UUID(java.util.UUID)

Example 15 with CrudEntityContext

use of io.jmix.core.accesscontext.CrudEntityContext in project jmix by jmix-framework.

the class UserSettingServiceBean method copySettings.

@Override
public void copySettings(UserDetails fromUser, UserDetails toUser) {
    Preconditions.checkNotNullArgument(fromUser);
    Preconditions.checkNotNullArgument(toUser);
    MetaClass metaClass = metadata.getClass(UiSetting.class);
    CrudEntityContext entityContext = new CrudEntityContext(metaClass);
    accessManager.applyRegisteredConstraints(entityContext);
    if (!entityContext.isCreatePermitted()) {
        throw new AccessDeniedException("entity", metaClass.getName());
    }
    transaction.executeWithoutResult(status -> {
        Query deleteSettingsQuery = entityManager.createQuery("delete from ui_Setting s where s.username = ?1");
        deleteSettingsQuery.setParameter(1, toUser.getUsername());
        deleteSettingsQuery.executeUpdate();
    });
    Map<UUID, UiTablePresentation> presentationsMap = copyPresentations(fromUser, toUser);
    copyUserFolders(fromUser, toUser, presentationsMap);
    Map<UUID, FilterEntity> filtersMap = copyFilters(fromUser, toUser);
    transaction.executeWithoutResult(status -> {
        TypedQuery<UiSetting> q = entityManager.createQuery("select s from ui_Setting s where s.username = ?1", UiSetting.class);
        q.setParameter(1, fromUser.getUsername());
        List<UiSetting> fromUserSettings = q.getResultList();
        for (UiSetting currSetting : fromUserSettings) {
            UiSetting newSetting = metadata.create(UiSetting.class);
            newSetting.setUsername(toUser.getUsername());
            newSetting.setName(currSetting.getName());
            try {
                Document doc = dom4JTools.readDocument(currSetting.getValue());
                List<Element> components = doc.getRootElement().element("components").elements("component");
                for (Element component : components) {
                    Attribute presentationAttr = component.attribute("presentation");
                    if (presentationAttr != null) {
                        UUID presentationId = UuidProvider.fromString(presentationAttr.getValue());
                        UiTablePresentation newPresentation = presentationsMap.get(presentationId);
                        if (newPresentation != null) {
                            presentationAttr.setValue(newPresentation.getId().toString());
                        }
                    }
                    Element defaultFilterEl = component.element("defaultFilter");
                    if (defaultFilterEl != null) {
                        Attribute idAttr = defaultFilterEl.attribute("id");
                        if (idAttr != null) {
                            UUID filterId = UuidProvider.fromString(idAttr.getValue());
                            FilterEntity newFilter = filtersMap.get(filterId);
                            if (newFilter != null) {
                                idAttr.setValue(newFilter.getId().toString());
                            }
                        }
                    }
                }
                newSetting.setValue(dom4JTools.writeDocument(doc, true));
            } catch (Exception e) {
                newSetting.setValue(currSetting.getValue());
            }
            entityManager.persist(newSetting);
        }
    });
}
Also used : AccessDeniedException(io.jmix.core.security.AccessDeniedException) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Document(org.dom4j.Document) AccessDeniedException(io.jmix.core.security.AccessDeniedException) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) UiTablePresentation(io.jmix.uidata.entity.UiTablePresentation) UiSetting(io.jmix.uidata.entity.UiSetting) UUID(java.util.UUID)

Aggregations

CrudEntityContext (io.jmix.core.accesscontext.CrudEntityContext)26 MetaClass (io.jmix.core.metamodel.model.MetaClass)9 EntityAttributeContext (io.jmix.core.accesscontext.EntityAttributeContext)4 AttributeLocalizationFragment (io.jmix.dynattrui.screen.localization.AttributeLocalizationFragment)4 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)3 AccessDeniedException (io.jmix.core.security.AccessDeniedException)3 ArrayList (java.util.ArrayList)3 CategoryAttrsFragment (io.jmix.dynattrui.screen.categoryattr.CategoryAttrsFragment)2 AttributeLocationFragment (io.jmix.dynattrui.screen.location.AttributeLocationFragment)2 UiSetting (io.jmix.uidata.entity.UiSetting)2 UiTablePresentation (io.jmix.uidata.entity.UiTablePresentation)2 UUID (java.util.UUID)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2 Attribute (org.dom4j.Attribute)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 FilterEntity (com.haulmont.cuba.security.entity.FilterEntity)1 AccessManager (io.jmix.core.AccessManager)1 Metadata (io.jmix.core.Metadata)1