Search in sources :

Example 1 with LogMessage

use of com.yahoo.elide.core.audit.LogMessage in project elide by yahoo.

the class PersistentResourceTest method testClassLevelAudit.

@Test
public void testClassLevelAudit() throws Exception {
    Child child = newChild(5);
    Parent parent = newParent(7);
    TestAuditLogger logger = new TestAuditLogger();
    RequestScope requestScope = getUserScope(goodUser, logger);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, requestScope.getUUIDFor(parent), requestScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, parentResource, "children", requestScope.getUUIDFor(child), requestScope);
    childResource.auditClass(Audit.Action.CREATE, new ChangeSpec(childResource, null, null, childResource.getObject()));
    assertEquals(1, logger.getMessages().size(), "One message should be logged");
    LogMessage message = logger.getMessages().get(0);
    assertEquals("CREATE Child 5 Parent 7", message.getMessage(), "Logging template should match");
    assertEquals(0, message.getOperationCode(), "Operation code should match");
    // tidy up this thread's messages
    logger.clear();
}
Also used : ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Parent(example.Parent) LogMessage(com.yahoo.elide.core.audit.LogMessage) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 2 with LogMessage

use of com.yahoo.elide.core.audit.LogMessage in project elide by yahoo.

the class PersistentResourceTest method testFieldLevelAudit.

@Test
public void testFieldLevelAudit() throws Exception {
    Child child = newChild(5);
    Parent parent = newParent(7);
    TestAuditLogger logger = new TestAuditLogger();
    RequestScope requestScope = getUserScope(goodUser, logger);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, requestScope.getUUIDFor(parent), requestScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, parentResource, "children", requestScope.getUUIDFor(child), requestScope);
    childResource.auditField(new ChangeSpec(childResource, "name", parent, null));
    assertEquals(1, logger.getMessages().size(), "One message should be logged");
    LogMessage message = logger.getMessages().get(0);
    assertEquals("UPDATE Child 5 Parent 7", message.getMessage(), "Logging template should match");
    assertEquals(1, message.getOperationCode(), "Operation code should match");
    // tidy up this thread's messages
    logger.clear();
}
Also used : ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Parent(example.Parent) LogMessage(com.yahoo.elide.core.audit.LogMessage) TestAuditLogger(com.yahoo.elide.core.audit.TestAuditLogger) Child(example.Child) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 3 with LogMessage

use of com.yahoo.elide.core.audit.LogMessage in project elide by yahoo.

the class PersistentResource method auditClass.

/**
 * Audit an action on an entity.
 *
 * @param action     the action
 * @param changeSpec the change that occurred
 */
protected void auditClass(Audit.Action action, ChangeSpec changeSpec) {
    Audit[] annotations = getResourceType().getAnnotationsByType(Audit.class);
    if (annotations == null) {
        return;
    }
    for (Audit annotation : annotations) {
        for (Audit.Action auditAction : annotation.action()) {
            if (auditAction == action) {
                // compare object reference
                LogMessage message = new LogMessageImpl(annotation, this, Optional.ofNullable(changeSpec));
                getRequestScope().getAuditLogger().log(message);
            }
        }
    }
}
Also used : Audit(com.yahoo.elide.annotation.Audit) LogMessage(com.yahoo.elide.core.audit.LogMessage) LogMessageImpl(com.yahoo.elide.core.audit.LogMessageImpl)

Example 4 with LogMessage

use of com.yahoo.elide.core.audit.LogMessage in project elide by yahoo.

the class PersistentResource method auditField.

/**
 * Audit an action on field.
 *
 * @param changeSpec Change spec for audit
 */
protected void auditField(final ChangeSpec changeSpec) {
    final String fieldName = changeSpec.getFieldName();
    Audit[] annotations = dictionary.getAttributeOrRelationAnnotations(getResourceType(), Audit.class, fieldName);
    if (annotations == null || annotations.length == 0) {
        // Default to class-level annotation for action
        auditClass(Audit.Action.UPDATE, changeSpec);
        return;
    }
    for (Audit annotation : annotations) {
        if (annotation.action().length == 1 && annotation.action()[0] == Audit.Action.UPDATE) {
            LogMessage message = new LogMessageImpl(annotation, this, Optional.of(changeSpec));
            getRequestScope().getAuditLogger().log(message);
        } else {
            throw new InvalidSyntaxException("Only Audit.Action.UPDATE is allowed on fields.");
        }
    }
}
Also used : Audit(com.yahoo.elide.annotation.Audit) LogMessage(com.yahoo.elide.core.audit.LogMessage) LogMessageImpl(com.yahoo.elide.core.audit.LogMessageImpl) InvalidSyntaxException(com.yahoo.elide.core.audit.InvalidSyntaxException)

Aggregations

LogMessage (com.yahoo.elide.core.audit.LogMessage)4 Audit (com.yahoo.elide.annotation.Audit)2 LogMessageImpl (com.yahoo.elide.core.audit.LogMessageImpl)2 TestAuditLogger (com.yahoo.elide.core.audit.TestAuditLogger)2 ChangeSpec (com.yahoo.elide.core.security.ChangeSpec)2 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)2 Child (example.Child)2 Parent (example.Parent)2 Test (org.junit.jupiter.api.Test)2 InvalidSyntaxException (com.yahoo.elide.core.audit.InvalidSyntaxException)1