GrailsSpring安全性:需要基于用户的GSPs中的if/else逻辑
我相信一定有一种更优雅的方法可以基于用户信息在GSP中实现if/else逻辑块 考虑以下几点:GrailsSpring安全性:需要基于用户的GSPs中的if/else逻辑,grails,spring-security,taglib,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) && (
<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>
如果回复,则显示此帖子的选项
如果此用户发布,则显示选项
如果不是此用户帖子,则显示选项
如果没有人登录,则显示选项
我必须使用以下信息来确定用户是否已登录(第loggedInUserInfo节(字段:“id”)!=“”),我知道,我讨厌它!
我不能使用nicespring安全性标记,因为它们不满足我的要求,我试图实现自定义标记,但无法实现if/else功能(或者我能实现吗)?
任何帮助都将不胜感激
编辑:
我创建的标记:
<g:isOwnerAndStatusEquals post="${post}" status="${Post.REPLIED_TO}">
My post and is replied to
</g:isOwnerAndStatusEquals>
我的帖子被回复了
标记库实现:
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()
}
}
}
类AuthTagLib{
def springSecurityService
def isOwnerAndStatusEquals={attrs,body->
def loggedInUser=springSecurityService.currentUser
def post=属性?post
def postaser=post?用户
def postStatus=attrs?.status
if(loggedInUser?.id==postaser?.id&&job?.status?.equals(thisStatus)){
out您可以在g:if标记的测试标准中使用SpringSecurityUtils。如下所示:
<g:if test="${grails.plugin.springsecurity.SpringSecurityUtils.ifAllGranted('ROLE_MYROLE')}"
以下代码与您的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>
如果回复,则显示此帖子的选项
如果此用户发布,则显示选项
如果不是此用户帖子,则显示选项
如果没有人登录,则显示选项
是的,你可以在你的自定义标记中包含这个。你在这样做时面临什么障碍?我的问题是实现if/else。仅此而已!!显然我不知道如何实现。我可以将我尝试过的内容作为答案发布。你可以将其添加到问题中,而不是作为编辑。如果你无法编辑你的问题,请将其发布,我可以更新问题w同样。你的思路是正确的。你可以在标记中嵌入if/else逻辑,然后使用特定于每个条件的内容模板。根据每个条件,你只需呈现模板。比如out,可能我误解了你的解决方案,但这个if/else的示例我不需要重用。isOwner标记定义itely将被重新使用,但我不认为我想创建自定义标记来实现其中的if/else功能来完成这项特定工作。这里是灯柱的经典案例。我在编辑的问题中结合了上述内容和自定义标记,但仍需为@Donal的方向给出分数。