读出Grails-Controller中的所有操作

问题描述 投票:6回答:5

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

例如:用户xyz具有执行动作节目,列表,搜索的授权。用户admin具有执行操作编辑,删除等操作的权限。

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

grails grails-controller
5个回答
9
投票

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

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
}

5
投票

这是一个与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
            }
        }
    }
}

4
投票

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注入。由于控制器本身没有必要的方法,这段代码检索它的controllerClass(参见GrailsControllerClass),它有我们需要的东西:property uris和method getMethodActionName


4
投票

要打印出包含操作名称的所有方法的列表:

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

0
投票

我必须提取所有控制器及其各自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()}"
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.