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