Skip to content

Commit

Permalink
started working on embeddedPropertis support
Browse files Browse the repository at this point in the history
see #111
  • Loading branch information
robertoschwald committed Jun 8, 2017
1 parent 5fa5191 commit b32182a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,20 @@ class AuditLogListener extends AbstractPersistenceEventListener {
// ignore (GPAUDITLOGGING-61)
return
}

if (maskList(domain)?.contains(key)) {
log.trace("Masking property ${key} with ${propertyMask}")
propertyMask
} else if (truncateLength) {
truncate(value, truncateLength)
if (getDomainClass(domain).getPersistentProperty(key).isEmbedded()){
squashEmbeddedProperties(domain, key, value)
} else {
truncate(value, Integer.MAX_VALUE)
if (maskList(domain)?.contains(key)) {
log.trace("Masking property ${key} with ${propertyMask}")
propertyMask
} else if (truncateLength) {
truncate(value, truncateLength)
} else {
truncate(value, Integer.MAX_VALUE)
}
}

// TODO fix path
}

String truncate(value, int max) {
Expand Down Expand Up @@ -866,4 +871,11 @@ class AuditLogListener extends AbstractPersistenceEventListener {
true
}

String squashEmbeddedProperties(domain, String key, value){
def clazz = getDomainClass(domain)
if (!clazz.getPersistentProperty(key).isEmbedded()) return null
def embeddedClazz = getDomainClass value
// value is an Embedded Object -> return all properties of it.
Set<String> dirtyProperties = getDirtyPropertyNames(value, embeddedClazz)
}
}
16 changes: 12 additions & 4 deletions audit-test/grails-app/domain/test/Author.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ class Author {
Long age
Boolean famous = false
Publisher publisher

// This should get masked globally
String ssn = "123-456-7890"

Date dateCreated
Date lastUpdated
String lastUpdatedBy
Address address

String handlerCalled = ""
static transients = ['handlerCalled']

static transients = ['handlerCalled']
static hasMany = [books: Book]

static auditable = true


static constraints = {
lastUpdatedBy nullable: true
publisher nullable: true
address(nullable:true)
}

static embedded = ['address']

// Event handlers
def onSave = { newMap ->
assert newMap
Expand All @@ -42,3 +44,9 @@ class Author {
handlerCalled += "onDelete"
}
}

class Address implements Serializable {
String street
String city
String zip
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class AuditDeleteSpec extends Specification {

def events = AuditTrail.findAllByClassName('test.Author')

events.size() == 7
events.size() == 8
['name', 'publisher', 'books', 'ssn', 'age', 'famous', 'dateCreated'].each { name ->
assert events.find {it.propertyName == name}, "${name} was not logged"
}
Expand All @@ -184,7 +184,7 @@ class AuditDeleteSpec extends Specification {
then: "ignored properties not logged"
def events = AuditTrail.findAllByClassName('test.Author')

events.size() == 6
events.size() == 7
['name', 'publisher', 'books', 'ssn', 'lastUpdated', 'lastUpdatedBy'].each { name ->
assert events.find {it.propertyName == name}, "${name} was not logged"
}
Expand Down
28 changes: 24 additions & 4 deletions audit-test/src/integration-test/groovy/test/AuditInsertSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ class AuditInsertSpec extends Specification {

then: "ignored properties not logged"
def events = AuditTrail.findAllByClassName('test.Author')

events.size() == 7
println("Events: ${events}")
events.size() == 8
['name', 'publisher', 'books', 'ssn', 'age', 'famous', 'dateCreated'].each { name ->
assert events.find {it.propertyName == name}, "${name} was not logged"
}
Expand All @@ -290,8 +290,8 @@ class AuditInsertSpec extends Specification {

then: "ignored properties not logged"
def events = AuditTrail.findAllByClassName('test.Author')

events.size() == 6
println("Events: ${events}")
events.size() == 7
['name', 'publisher', 'books', 'ssn', 'lastUpdated', 'lastUpdatedBy'].each { name ->
assert events.find {it.propertyName == name}, "${name} was not logged"
}
Expand Down Expand Up @@ -337,5 +337,25 @@ class AuditInsertSpec extends Specification {
}
}

void "Test insert logging with embedded object"() {
given:
def author = new Author(name: "Aaron", age: 37, famous: true)
author.address = new Address(city:'test', street:'teststr', zip:'testZip')
author.addToBooks(new Book(title: 'Foo', description: 'Bar', pages: 200))

when:
author.save(flush: true, failOnError: true)

then: "author is saved"
author.id
author.address.zip == 'testZip'

and: "verbose audit logging is created"
def events = AuditTrail.findAllByClassName('test.Author')
events.size() == (Author.gormPersistentEntity.persistentPropertyNames - defaultIgnoreList).size()

def bookEvents = AuditTrail.findAllByClassName('test.Book')
bookEvents.size() == (Book.gormPersistentEntity.persistentPropertyNames - defaultIgnoreList).size()
}

}

0 comments on commit b32182a

Please sign in to comment.