gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:20:02 26 4
gpt4 key购买 nike

我正在使用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
}

我仍然遇到相同的Long / Integer错误

编辑:

这是与该pkugin相关的Config.groovy的内容
grails {
plugin{
audittrail{
createdBy.field = "createdBy"
editedBy.field = "editedBy"
createdDate.field = "createdDate"
editedDate.field = "editedDate"
}
}
}

最佳答案

您可以使用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()
}

关于grails - 如何在Bootstrap的Grails audit-trail插件中设置默认的createdBy和editedBy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17573131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com