grails - Grails Spring Security:GSP中基于用户的if/else逻辑

标签 grails spring-security taglib

我确信必须有一种基于用户信息在GSP中实现if / else逻辑块的更优雅的方法。

考虑以下:

<g:if test="(user logged in) && (this users post) && (post replied to)">
    Show options for this post if replied to
</g:if>
<g:elseif test="(user logged in) && (this users post)">
    show options if this users post
</g:elseif>
<g:elseif test="(user logged in)">
    show options if not this users post
</g:elseif>
<g:else>
    show options if no one is logged in
</g:else>

我必须使用以下命令确定用户是否已登录(sec.loggedInUserInfo(field:'id')!=“”)我知道,我讨厌它!
我不能使用漂亮的Spring Security标记,因为它们不能满足我的要求,并且我试图实现自定义标记,但是将无法实现if / else功能(或者可以)吗?
任何帮助将非常感激。

编辑:

我创建的标签:
<g:isOwnerAndStatusEquals post="${post}" status="${Post.REPLIED_TO}">
     My post and is replied to
</g:isOwnerAndStatusEquals>

Tag lib的实现:
 class AuthTagLib {

     def springSecurityService

     def isOwnerAndStatusEquals = { attrs, body ->
         def loggedInUser = springSecurityService.currentUser
         def post = attrs?.post
         def postUser = post?.user
         def postStatus = attrs?.status


         if(loggedInUser?.id == postUser?.id && job?.status?.equals(thisStatus)) {
             out << body()
         }
     }
}

上面的内容很好,但我不知道如何在同一个标​​签中添加if / else?

最佳答案

以下内容似乎等同于您的GSP代码,并且不需要您编写任何自定义标签:

<sec:ifLoggedIn>
  <g:if test="(this users post) && (post replied to)">
    Show options for this post if replied to
  </g:if>
  <g:elseif test="(this users post)">
    show options if this users post
  </g:elseif>
  <g:else>
    show options if not this users post
  </g:else>
</sec:ifLoggedIn>
<sec:ifNotLoggedIn>
  show options if no one is logged in
</sec:ifNotLoggedIn>

关于grails - Grails Spring Security:GSP中基于用户的if/else逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351110/

相关文章:

grails - 如何在grails中使用事件推送插件

grails - 加速 Grails 依赖解析

grails - Jasper Report插件在Grails 2.4.2中生成PDF

grails - Grails/SpringSecurity:登录和注销

ldap - 带有 LDAP 和自定义 UserDetailsContextMapper 的 Spring Security

jsp - 在JSP中将枚举值作为标记属性传递

java - 如何从自定义标签返回值

email - Grails:当 SMTP 服务器暂时关闭时如何缓冲出站电子邮件?

spring - 使用 Spring Security 进行身份验证后如何基于角色进行重定向

unit-testing - 如何在(Spock,Grails)单元测试Taglib中模拟渲染?