如何在整个应用程序中查找授权给grails中特定用户角色的所有gsp页面?

时间:2015-10-14 21:59:46

标签: grails

我必须找到授权给特定角色的所有视图名称(.gsp名称),例如Global_Admin。这不包括任何数据库操作,只能从控制器方法中识别。

用户角色在方法声明上方的控制器类中以下面给出的格式提到。一个控制器的示例:

class JobController {

    @Secured(["hasAnyRole('Global_Admin')"])
    def jobsList(){
        // Business logic here

        render(view:"jobsList")
    }
}

因此,我需要搜索应用程序内的所有控制器类,并找到授权给Global_Admin的每个方法的视图名称。我应该在新的GSP页面中显示此列表。

1 个答案:

答案 0 :(得分:0)

是的,绝对有可能。假设注释存在于动作或方法上,我提供了一个基本代码。

import grails.plugin.springsecurity.annotation.Secured

class MyController {

   def grailsApplication

   def test() {
        List<String> gspsWithAnnotation = []

        // Iterate each controller class
        grailsApplication.controllerClasses.each { controller ->
            // Iterate each action of the controller
            controller.getClazz().methods.each { method ->
                // See if action has Secured annotation
                if (method.isAnnotationPresent(Secured)) {
                    List<String> value = method.getAnnotation(Secured).value()
                    // See if annotation value is "Global_Admin"
                    if (value.any { it.contains("Global_Admin")}) {
                        // Then put that action name to the list
                        gspsWithAnnotation << method.name
                    }
                }
            }
        }

        println "All GSPs with annotation: $gspsWithAnnotation"
        [gspsWithAnnotation: gspsWithAnnotation]
    }
}

只需验证Secured注释的包。当然,这段代码会检查操作上的Secured注释,您可以调整它以查看控制器类上的注释。

相关问题