Search in sources :

Example 1 with PermissionConfig

use of io.jmix.graphql.schema.permission.PermissionConfig in project jmix by jmix-framework.

the class PermissionDataFetcher method getPermissions.

protected PermissionConfig getPermissions(boolean entities, boolean entityAttrs, boolean specifics) {
    PermissionConfig PermissionConfig = new PermissionConfig();
    List<ShortPermissionInfo> entityPermissions = new ArrayList<>();
    List<ShortPermissionInfo> entityAttributePermissions = new ArrayList<>();
    List<ShortPermissionInfo> specificPermissions = new ArrayList<>();
    PermissionConfig.setEntities(entityPermissions);
    PermissionConfig.setEntityAttributes(entityAttributePermissions);
    PermissionConfig.setSpecifics(specificPermissions);
    for (MetaClass metaClass : metadata.getSession().getClasses()) {
        CrudEntityContext entityContext = new CrudEntityContext(metaClass);
        accessManager.applyRegisteredConstraints(entityContext);
        if (entities) {
            if (entityContext.isCreatePermitted()) {
                entityPermissions.add(new ShortPermissionInfo(getEntityTarget(metaClass, "create"), ALLOWED_CRUD_PERMISSION));
            }
            if (entityContext.isReadPermitted()) {
                entityPermissions.add(new ShortPermissionInfo(getEntityTarget(metaClass, "read"), ALLOWED_CRUD_PERMISSION));
            }
            if (entityContext.isUpdatePermitted()) {
                entityPermissions.add(new ShortPermissionInfo(getEntityTarget(metaClass, "update"), ALLOWED_CRUD_PERMISSION));
            }
            if (entityContext.isDeletePermitted()) {
                entityPermissions.add(new ShortPermissionInfo(getEntityTarget(metaClass, "delete"), ALLOWED_CRUD_PERMISSION));
            }
        }
        if (entityAttrs) {
            for (MetaProperty metaProperty : metaClass.getProperties()) {
                EntityAttributeContext attributeContext = new EntityAttributeContext(metaClass, metaProperty.getName());
                accessManager.applyRegisteredConstraints(attributeContext);
                if (attributeContext.canModify()) {
                    entityAttributePermissions.add(new ShortPermissionInfo(getEntityAttributeTarget(metaClass, metaProperty), MODIFY_ATTRIBUTE_PERMISSION));
                } else if (attributeContext.canView()) {
                    entityAttributePermissions.add(new ShortPermissionInfo(getEntityAttributeTarget(metaClass, metaProperty), VIEW_ATTRIBUTE_PERMISSION));
                }
            }
        }
    }
    if (specifics) {
        GraphQLAccessContext downloadContext = new GraphQLAccessContext(GRAPHQL_FILE_DOWNLOAD_ENABLED);
        accessManager.applyRegisteredConstraints(downloadContext);
        if (downloadContext.isPermitted()) {
            specificPermissions.add(new ShortPermissionInfo(downloadContext.getName(), 1));
        } else {
            specificPermissions.add(new ShortPermissionInfo(downloadContext.getName(), 0));
        }
        GraphQLAccessContext uploadContext = new GraphQLAccessContext(GRAPHQL_FILE_UPLOAD_ENABLED);
        accessManager.applyRegisteredConstraints(uploadContext);
        if (uploadContext.isPermitted()) {
            specificPermissions.add(new ShortPermissionInfo(uploadContext.getName(), 1));
        } else {
            specificPermissions.add(new ShortPermissionInfo(uploadContext.getName(), 0));
        }
    }
    return PermissionConfig;
}
Also used : PermissionConfig(io.jmix.graphql.schema.permission.PermissionConfig) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) GraphQLAccessContext(io.jmix.graphql.accesscontext.GraphQLAccessContext) EntityAttributeContext(io.jmix.core.accesscontext.EntityAttributeContext) ArrayList(java.util.ArrayList) ShortPermissionInfo(io.jmix.graphql.schema.permission.ShortPermissionInfo) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

CrudEntityContext (io.jmix.core.accesscontext.CrudEntityContext)1 EntityAttributeContext (io.jmix.core.accesscontext.EntityAttributeContext)1 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 GraphQLAccessContext (io.jmix.graphql.accesscontext.GraphQLAccessContext)1 PermissionConfig (io.jmix.graphql.schema.permission.PermissionConfig)1 ShortPermissionInfo (io.jmix.graphql.schema.permission.ShortPermissionInfo)1 ArrayList (java.util.ArrayList)1