如何在Bootstrap的Grails audit-trail插件中设置默认的createdBy和editedBy?

时间:2013-07-10 14:21:04

标签: grails

我使用的是Grails 2.2.2,audit-trail插件2.0.3和spring-security-core 1.2.7.3

当我将注释放在一个类上并使用浏览器插入记录时(通过Controller / gsp),一切正常。

@gorm.AuditStamp
class Note {
   String name
}

但是,当我在Bootstrap中插入记录时

new Note(name:'Testing').save()

我在启动时遇到错误

ERROR property.BasicPropertyAccessor  - IllegalArgumentException in class: test.Note, setter method of property: createdBy
ERROR property.BasicPropertyAccessor  - expected type: java.lang.Long, actual value: java.lang.Integer
ERROR context.GrailsContextLoader  - Error initializing the application: IllegalArgumentException occurred while calling setter of test.Note.createdBy; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of test.Note.createdBy

有没有办法修复在Bootstrap期间使审计跟踪工作?我只是在从Bootstrap插入时放置默认值,并在使用浏览器屏幕插入时记录用户。我使用以下更改但仍然没有运气:

static mapping = {
    createdBy (defaultValue: Long.valueOf( 1l ))
    editedBy (defaultValue: Long.valueOf( 1l ))
}
static constraints = {
    createdBy nullable:true
    editedBy nullable:true
}

我仍然得到相同的长/整数错误

编辑:

这是我的Config.groovy与此pkugin相关的内容

grails {
    plugin{
        audittrail{
            createdBy.field = "createdBy"
            editedBy.field = "editedBy"
            createdDate.field = "createdDate"
            editedDate.field = "editedDate"
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您可以使用SpringSecurityUtils.doWithAuth伪造安全上下文

import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils

// set up a default user, if one doesn't already exist
def defaultUser = User.findByUsername('default') ?: new User(username:'default').save()

// run the following code as if that user were logged in
SpringSecurityUtils.doWithAuth('default') {
  new Note(name:'Testing').save()
}