以何种方式知道用户在renderEditor.template中登录的用户

时间:2012-12-03 21:54:47

标签: templates grails groovy spring-security

我的问题是:有没有什么方法可以在renderEditor.template中知道用户登录了什么以及他有什么角色?
我想做什么:在我的应用程序中,我希望在记录用户为admin(“ROLE_ADMIN”)时进行一些字段更改 - 普通用户将只有html选择标记1个选项,默认情况下已选中,当admin用户将登录时,显示所有用户的列表。

我正在使用Spring Security Plugin + Grails 2.1.1。

我已经尝试过:

  1. 添加Spring Security Service(def springSecurityService),但它始终为null
  2. 尝试传递布尔参数 - 无效果
  3. 任何帮助将不胜感激!

    编辑非常感谢您的回答。但是,我的问题还不够精确,所以我加入了一些假代码,可能更好地解释了我想要实现的内容(来自renderTemplate.template的方法)

    private renderManyToOne(domainClass,property) {
            if (property.association) {
                def sb = new StringBuilder()
                sb << '<g:select'
                ...
                if (/*loged user is admin*/) {
                    sb << ' from="${' << property.type.name << '.list()}"'
                }else{
                    sb << ' from="${user}"'/*only loged user can be selected*/
                }
                ...
                sb << '/>'
                sb as String
            }
        }
    

2 个答案:

答案 0 :(得分:0)

有一个带弹簧安全性的taglib http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/6%20Helper%20Classes.html#6.1%20SecurityTagLib

在我的应用程序中,我使用它:

<ul>
<sec:access expression="hasRole('ROLE_ADMIN')">
    <li><a href="#newFeedModal" role="button">Add New Feed</a></li>
</sec:access>
</ul>

答案 1 :(得分:0)

使用通常的GSP模板引擎不会呈现renderEditor.template。它使用标准SimpleTemplateEngine进行渲染,只需几个简单的绑定。但是,此模板的输出随后将使用GSP模板引擎进行渲染。它相当令人困惑,但您可以输出renderEditor.template GSP代码。例如:

<%  if (property.type == Boolean || property.type == boolean)
        out << renderBooleanEditor(domainClass, property)
    else ...
    else if (property.type == String && domainInstance == 'specialField') {
        out << '''<g:if test="${org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils.ifAllGranted('ROLE_ADMIN')}">'''
        out << renderStringEditor(domainClass, property)
        out << '''</g:if>'''
        out << '''<g:else>'''
        out << renderStringSelectEditor(domainClass, property)
        out << '''</g:else>'''
    }
    ...