读出 Grails-Controller 中的所有操作

发布于 2024-09-04 01:43:50 字数 153 浏览 6 评论 0原文

我需要从我的网络应用程序中的任何控制器读出所有可用的操作。这样做的原因是授权系统,我需要为用户提供允许的操作列表。

例如: 用户 xyz 具有执行显示、列表、搜索操作的权限。 用户管理员有权执行编辑、删除等操作。

我需要从控制器中读出所有操作。有人有想法吗?

I need to read out all available actions from any controller in my web-app. The reason for this is an authorization system where I need to give users a list of allowed actions.

E.g.:
User xyz has the authorization for executing the actions show, list, search.
User admin has the authorization for executing the actions edit, delete etc.

I need to read out all actions from a controller. Does anyone has an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷默言语 2024-09-11 01:43:50

这将创建一个包含控制器信息的地图列表(“数据”变量)。 List中的每个元素都是一个Map,其中键为'controller',对应控制器的URL名称(例如BookController -> 'book'),controllerName对应类名('BookController'),'actions'对应到该控制器的操作名称列表:

import org.springframework.beans.BeanWrapper
import org.springframework.beans.PropertyAccessorFactory

def data = []
for (controller in grailsApplication.controllerClasses) {
    def controllerInfo = [:]
    controllerInfo.controller = controller.logicalPropertyName
    controllerInfo.controllerName = controller.fullName
    List actions = []
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(controller.newInstance())
    for (pd in beanWrapper.propertyDescriptors) {
        String closureClassName = controller.getPropertyOrStaticPropertyOrFieldValue(pd.name, Closure)?.class?.name
        if (closureClassName) actions << pd.name
    }
    controllerInfo.actions = actions.sort()
    data << controllerInfo
}

This will create a List of Maps (the 'data' variable) with controller information. Each element in the List is a Map with keys 'controller', corresponding to the URL name of the controller (e.g. BookController -> 'book'), controllerName corresponding to the class name ('BookController'), and 'actions' corresponding to a List of action names for that controller:

import org.springframework.beans.BeanWrapper
import org.springframework.beans.PropertyAccessorFactory

def data = []
for (controller in grailsApplication.controllerClasses) {
    def controllerInfo = [:]
    controllerInfo.controller = controller.logicalPropertyName
    controllerInfo.controllerName = controller.fullName
    List actions = []
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(controller.newInstance())
    for (pd in beanWrapper.propertyDescriptors) {
        String closureClassName = controller.getPropertyOrStaticPropertyOrFieldValue(pd.name, Closure)?.class?.name
        if (closureClassName) actions << pd.name
    }
    controllerInfo.actions = actions.sort()
    data << controllerInfo
}
围归者 2024-09-11 01:43:50

这是一个适用于 Grails 2 的示例,即它将捕获定义为方法或闭包的操作

import org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass
import java.lang.reflect.Method
import grails.web.Action

// keys are logical controller names, values are list of action names  
// that belong to that controller
def controllerActionNames = [:]

grailsApplication.controllerClasses.each { DefaultGrailsControllerClass controller ->

    Class controllerClass = controller.clazz

    // skip controllers in plugins
    if (controllerClass.name.startsWith('com.mycompany')) {
        String logicalControllerName = controller.logicalPropertyName

        // get the actions defined as methods (Grails 2)
        controllerClass.methods.each { Method method ->

            if (method.getAnnotation(Action)) {
                def actions = controllerActionNames[logicalControllerName] ?: []
                actions << method.name

                controllerActionNames[logicalControllerName] = actions
            }
        }
    }
}

Here's an example that works with Grails 2, i.e it will capture actions defined as either methods or closures

import org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass
import java.lang.reflect.Method
import grails.web.Action

// keys are logical controller names, values are list of action names  
// that belong to that controller
def controllerActionNames = [:]

grailsApplication.controllerClasses.each { DefaultGrailsControllerClass controller ->

    Class controllerClass = controller.clazz

    // skip controllers in plugins
    if (controllerClass.name.startsWith('com.mycompany')) {
        String logicalControllerName = controller.logicalPropertyName

        // get the actions defined as methods (Grails 2)
        controllerClass.methods.each { Method method ->

            if (method.getAnnotation(Action)) {
                def actions = controllerActionNames[logicalControllerName] ?: []
                actions << method.name

                controllerActionNames[logicalControllerName] = actions
            }
        }
    }
}
执手闯天涯 2024-09-11 01:43:50

Grails 不支持直接的方法来执行此操作。然而,我能够从可用的 grails 方法中整理出一个难题,并得出这个解决方案:

def actions = new HashSet<String>()
def controllerClass = grailsApplication.getArtefactInfo(ControllerArtefactHandler.TYPE)
                         .getGrailsClassByLogicalPropertyName(controllerName)
for (String uri : controllerClass.uris ) {
    actions.add(controllerClass.getMethodActionName(uri) )
}

变量 grailsApplication 和 controllerName 由 grails 注入。
由于控制器本身没有必要的方法,因此此代码检索其控制器类(请参阅 GrailsControllerClass),其中包含我们需要的内容:属性 uris 和方法 getMethodActionName

Grails does not support a straightforward way to do this. However, I was able to put together a puzzle from available grails methods and have come to this solution:

def actions = new HashSet<String>()
def controllerClass = grailsApplication.getArtefactInfo(ControllerArtefactHandler.TYPE)
                         .getGrailsClassByLogicalPropertyName(controllerName)
for (String uri : controllerClass.uris ) {
    actions.add(controllerClass.getMethodActionName(uri) )
}

Variables grailsApplication and controllerName are injected by grails.
As controller itself does not have necessary methods, this code retrieves its controllerClass (see GrailsControllerClass), which has what we need: property uris and method getMethodActionName

吃兔兔 2024-09-11 01:43:50

要打印带有操作名称的所有方法的列表:

   grailsApplication.controllerClasses.each {
               it.getURIs().each {uri ->
                 println  "${it.logicalPropertyName}.${it.getMethodActionName(uri)}"
               }
   }

To print out a list of all the methods with action names:

   grailsApplication.controllerClasses.each {
               it.getURIs().each {uri ->
                 println  "${it.logicalPropertyName}.${it.getMethodActionName(uri)}"
               }
   }
流年里的时光 2024-09-11 01:43:50

我必须获取所有控制器及其各自 URI 的列表。这就是我在 grails 3.1.6 应用程序上所做的。

grailsApplication.controllerClasses.each { controllerArtefact ->
            def controllerClass = controllerArtefact.getClazz()
            def actions = controllerArtefact.getActions()
            actions?.each{action->
                def controllerArtefactString = controllerArtefact.toString()
                def controllerOnly = controllerArtefactString.split('Artefact > ')[1]
                println "$controllerOnly >>>> $controllerOnly/${action.toString()}"
            }
        }

I had to pull a list of all controllers and their respective URI. This is what I did on a grails 3.1.6 application.

grailsApplication.controllerClasses.each { controllerArtefact ->
            def controllerClass = controllerArtefact.getClazz()
            def actions = controllerArtefact.getActions()
            actions?.each{action->
                def controllerArtefactString = controllerArtefact.toString()
                def controllerOnly = controllerArtefactString.split('Artefact > ')[1]
                println "$controllerOnly >>>> $controllerOnly/${action.toString()}"
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文