use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class AuditLogWriter method writeForm.
public void writeForm(FormStorageProvider catalog, ResourceId formId) throws IOException {
FormStorage formStorage = catalog.getForm(formId).get();
FormClass formClass = formStorage.getFormClass();
Key<FormEntity> parentKey = FormEntity.key(formId);
Query<FormRecordSnapshotEntity> query = Hrd.ofy().load().type(FormRecordSnapshotEntity.class).ancestor(parentKey);
for (FormRecordSnapshotEntity snapshot : query) {
User user;
try {
user = userCache.get((int) snapshot.getUserId());
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
csv.writeLine(formatTime(snapshot.getTime()), snapshot.getType().name(), user.getEmail(), user.getName(), databaseId(), db.getName(), formId.asString(), formClass.getLabel(), // Field ID
"", // Field Name
"", snapshot.getRecordId().asString(), partner());
}
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class GcsBlobFieldStorageServiceTest method addAttachmentField.
private FormClass addAttachmentField(int activityId, ResourceId attachmentFieldId) {
FormClass formClass = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(activityId)));
formClass.addElement(new FormField(attachmentFieldId).setLabel("Attachment").setType(AttachmentType.TYPE_CLASS.createType()).setVisible(true));
assertResolves(locator.persist(formClass));
// re-fetch
return assertResolves(locator.getFormClass(formClass.getId()));
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class GcsBlobFieldStorageServiceTest method blobPermissionAttack.
/**
* 1. user1 : persist blob with FormInstance1 (FormClass1) user1
* 2. user2 : persist the same blob with FormInstance2 (FormClass2) -> try to steal blob access
*/
@Test
@OnDataSet("/dbunit/sites-simple-blob-security.db.xml")
public void blobPermissionAttack() throws IOException {
blobService.setTestBucketName();
int activityId = 1;
int databaseId = 1;
int locationType = 10;
ResourceId attachmentFieldId = ResourceId.generateFieldId(AttachmentType.TYPE_CLASS);
FormClass formClass = addAttachmentField(activityId, attachmentFieldId);
blobId = BlobId.generate();
blobService.put(user, "attachment;filename=" + FILE_NAME, MimeTypeUtil.mimeTypeFromFileName(FILE_NAME), blobId, formClass.getId(), GcsBlobFieldStorageServiceTest.class.getResourceAsStream("goabout.png"));
FormInstance instance = new FormInstance(CuidAdapter.cuid(SITE_DOMAIN, new KeyGenerator().generateInt()), formClass.getId());
Attachment attachment = new Attachment();
attachment.setMimeType(MimeTypeUtil.mimeTypeFromFileName(FILE_NAME));
attachment.setBlobId(blobId.asString());
attachment.setFilename(FILE_NAME);
AttachmentValue attachmentValue = new AttachmentValue();
attachmentValue.getValues().add(attachment);
instance.set(indicatorField(1), 1);
instance.set(indicatorField(2), 2);
instance.set(attachmentFieldId, attachmentValue);
instance.set(locationField(activityId), locationRef(CuidAdapter.locationFormClass(locationType), 1));
instance.set(partnerField(activityId), partnerRef(databaseId, 1));
instance.set(projectField(activityId), projectRef(databaseId, 1));
instance.set(field(formClass.getId(), START_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(formClass.getId(), END_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(formClass.getId(), COMMENT_FIELD), "My comment");
assertResolves(locator.persist(instance));
assertInstanceExists(formClass.getId(), instance.getId());
AuthenticationModuleStub.setUserId(USER_WITHOUT_ACCESS_TO_DB_1);
int anotherActivityId = 32;
ResourceId newAttachmentFieldId = ResourceId.generateFieldId(AttachmentType.TYPE_CLASS);
addAttachmentField(anotherActivityId, newAttachmentFieldId);
instance.setId(CuidAdapter.cuid(SITE_DOMAIN, new KeyGenerator().generateInt()));
instance.setClassId(CuidAdapter.activityFormClass(anotherActivityId));
instance.set(newAttachmentFieldId, attachmentValue);
instance.set(field(instance.getFormId(), START_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(instance.getFormId(), END_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(partnerField(anotherActivityId), partnerRef(databaseId, 1));
boolean persisted = true;
try {
// this must fail because of blob permission check
assertResolves(locator.persist(instance));
} catch (RuntimeException e) {
e.printStackTrace();
persisted = false;
}
assertFalse("Access to blob is stolen! Permissions check for blobs is broken.", persisted);
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class ActivityFormClassBuilderTest method nullLocationTypeIsNotVisible.
@Test
@OnDataSet("/dbunit/chad-form.db.xml")
public void nullLocationTypeIsNotVisible() {
setUser(9944);
int databaseId = 1470;
FormClass formClass = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(11218)));
ResourceId locationFieldId = CuidAdapter.field(formClass.getId(), CuidAdapter.LOCATION_FIELD);
assertThat(formClass.getFields(), not(hasItem(withId(locationFieldId))));
// Make sure we can update if location is not specified
FormInstance instance = new FormInstance(CuidAdapter.newLegacyFormInstanceId(formClass.getId()), formClass.getId());
instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2014, 1, 2));
instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD), CuidAdapter.partnerRef(databaseId, 1734));
instance.set(ResourceId.valueOf("Q0000031845"), new EnumValue(CuidAdapter.attributeField(166617)));
assertResolves(locator.persist(instance));
// Make sure the null location object is visible to legacy code
SiteDTO site = execute(GetSites.byId(CuidAdapter.getLegacyIdFromCuid(instance.getId()))).getData().get(0);
assertThat(site.getLocationName(), equalTo("Chad"));
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class FormPersisterTest method noDuplicates.
@Test
public void noDuplicates() {
FormClass formClass = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(11218)));
assertResolves(locator.persist(formClass));
FormClass formClass2 = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(11218)));
System.out.println(formClass.getFields());
System.out.println(formClass2.getFields());
assertThat(formClass2.getFields().size(), equalTo(formClass.getFields().size()));
}
Aggregations