开发者

Grails: User evaluator for Commentable with Spring Security plugin

开发者 https://www.devze.com 2023-01-12 19:54 出处:网络
I am trying to use the commentable plugin with Spring Security. I can\'t manage to write the right grails.commentable.poster.evaluator

I am trying to use the commentable plugin with Spring Security.

I can't manage to write the right

grails.commentable.poster.evaluator

I tried {User.get(springSecurityService.principal.id)}, but from the CommentController, both User and springSecurity see开发者_C百科ms unaccessible.

What should I do?


For springSecurityService.principal.id since there's no dependency injection field for springSecurityService it can't work, so you need to call what springSecurityService.principal.id calls - org.springframework.security.core.context.SecurityContextHolder.context.authentication.principal.id

To fix the problem with User, you'll need the full class name with package. So combined, this should be

{com.yourcompany.yourapp.User.get(org.springframework.security.core.context.SecurityContextHolder.context.authentication.principal.id)}


I adapted Burt's answer such that it doesn't throw an exception if nobody is logged in

grails.commentable.poster.evaluator = {

    def principal = org.springframework.security.core.context.SecurityContextHolder.context.authentication?.principal

    if (principal?.hasProperty('id')) {

        def currentUserId = principal.id
        if (currentUserId) {
            com.yourcompany.yourapp.User.get(currentUserId)
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消