use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method validateSecurity.
private TypeReport validateSecurity(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle, ImportStrategy importMode) {
TypeReport typeReport = new TypeReport(klass);
if (objects == null || objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
PreheatIdentifier identifier = bundle.getPreheatIdentifier();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
if (importMode.isCreate()) {
if (!aclService.canCreate(bundle.getUser(), klass)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3000, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
} else {
IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
if (importMode.isUpdate()) {
if (!aclService.canUpdate(bundle.getUser(), persistedObject)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3001, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
} else if (importMode.isDelete()) {
if (!aclService.canDelete(bundle.getUser(), persistedObject)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3002, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
}
}
if (User.class.isInstance(object)) {
User user = (User) object;
List<ErrorReport> errorReports = userService.validateUser(user, bundle.getUser());
if (!errorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
}
List<ErrorReport> sharingErrorReports = aclService.verifySharing(object, bundle.getUser());
if (!sharingErrorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(sharingErrorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
if (object == null) {
return preheatErrorReports;
}
Schema schema = schemaService.getDynamicSchema(object.getClass());
schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
return;
}
if (!p.isCollection()) {
IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
}
}
} else {
Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
for (IdentifiableObject refObject : refObjects) {
if (Preheat.isDefault(refObject))
continue;
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
} else {
objects.add(refObject);
}
}
ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
}
});
if (schema.havePersistedProperty("attributeValues")) {
object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
}
if (schema.havePersistedProperty("userGroupAccesses")) {
object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
}
if (schema.havePersistedProperty("userAccesses")) {
object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
}
return preheatErrorReports;
}
use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.
the class UniquenessCheck method checkUniqueness.
private List<ErrorReport> checkUniqueness(IdentifiableObject object, PreheatIdentifier identifier, Map<String, Map<Object, String>> uniquenessMap, List<Property> uniqueProperties) {
List<ErrorReport> errorReports = new ArrayList<>();
uniqueProperties.forEach(property -> {
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null) {
String objectIdentifier = uniquenessMap.computeIfAbsent(property.getName(), key -> new HashMap<>()).get(value);
if (objectIdentifier != null) {
if (!identifier.getIdentifier(object).equals(objectIdentifier)) {
String identifiersWithName = identifier.getIdentifiersWithName(object);
ErrorReport errorReport = new ErrorReport(HibernateProxyUtils.getRealClass(object), ErrorCode.E5003, property.getName(), value, identifiersWithName, objectIdentifier);
errorReports.add(errorReport.setMainId(objectIdentifier).setErrorProperty(property.getName()));
}
} else {
uniquenessMap.get(property.getName()).put(value, identifier.getIdentifier(object));
}
}
});
return errorReports;
}
use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.
the class UniquenessCheck method checkUniqueness.
private List<ErrorReport> checkUniqueness(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, ValidationContext ctx) {
if (object == null || preheat.isDefault(object)) {
return emptyList();
}
@SuppressWarnings("unchecked") Class<? extends IdentifiableObject> objType = HibernateProxyUtils.getRealClass(object);
Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().computeIfAbsent(objType, key -> new HashMap<>());
Schema schema = ctx.getSchemaService().getDynamicSchema(objType);
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
if (uniqueProperties.isEmpty()) {
return emptyList();
}
return checkUniqueness(object, identifier, uniquenessMap, uniqueProperties);
}
use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.
the class NotOwnerReferencesCheck method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, PreheatIdentifier identifier, ValidationContext ctx) {
if (object == null) {
return emptyList();
}
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
Schema schema = ctx.getSchemaService().getDynamicSchema(HibernateProxyUtils.getRealClass(object));
schema.getProperties().stream().filter(p -> !p.isOwner() && p.isWritable() && (PropertyType.REFERENCE == p.getPropertyType() && schema.getKlass() != p.getKlass() || PropertyType.REFERENCE == p.getItemPropertyType() && schema.getKlass() != p.getItemKlass())).forEach(p -> {
if (!p.isCollection()) {
checkReference(object, identifier, preheatErrorReports, p);
} else {
checkCollection(object, identifier, preheatErrorReports, p);
}
});
return preheatErrorReports;
}
Aggregations