【问题标题】:Grails w/ Spring Security Core only allowing owner to update in GSP带有 Spring Security Core 的 Grails 仅允许所有者在 GSP 中更新
【发布时间】:2011-08-26 16:46:39
【问题描述】:

This question 有一些代码要添加到控制器闭包中,这很好,但如果我想使用 grails 默认的脚手架视图,但如果用户是经理,则只显示编辑/更新按钮,或者域对象归用户所有? Reading the documentation,我试过了:

<sec:access expression="hasRole('ROLE_MANAGER') || (projectInstance.owner == springSecurityService.currentUser)">
   <span class="button"><g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /></span>
</sec:access>

但访问类似乎不允许 OR:

Error processing GroovyPageView: Error executing tag <g:form>: Error executing tag <sec:access>: Cannot handle (124) '|' 

有人做过类似的事情吗?

【问题讨论】:

    标签: grails spring-security gsp


    【解决方案1】:

    我认为那将是愚蠢的。更好的方法是在控制器中这样做:

     def edit = {
        def projectInstance = Project.get(params.id)
        def managerOrAdmin = SpringSecurityUtils.ifAnyGranted('ROLE_ADMIN,ROLE_MANAGER')
        def editable = (projectInstance.owner == springSecurityService.currentUser
                       || managerOrAdmin)
        if (!projectInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [projectInstance: projectInstance, editable:editable]
        }
    }
    

    然后在gsp中做

    <g:if test="${editable}">
      <span class="button"><g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /></span>
    </g:if>
    

    这是有道理的,如果我们遵循“视图应该在 MVC 中执行尽可能少的处理逻辑”的良好编程口号

    【讨论】:

    • 我同意这种方法,因为您的表达方式比简单的角色检查稍微复杂一些。
    • 如果我在几个动作中都需要这个,不知道在对象中使用 beforeUpdate 方法是否更好?
    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 2012-08-31
    • 2013-09-30
    • 2016-03-09
    • 2020-06-10
    • 2011-08-01
    • 1970-01-01
    • 2019-05-09
    相关资源
    最近更新 更多