我需要从我的网络应用程序中的任何控制器读出所有可用的操作。这样做的原因是一个授权系统,我需要给用户一个允许操作的列表。
例如:用户 xyz 具有执行 show、list、search 操作的权限。用户 admin 具有执行编辑、删除等操作的权限。
我需要从控制器读出所有动作。有人有想法吗?
我需要从我的网络应用程序中的任何控制器读出所有可用的操作。这样做的原因是一个授权系统,我需要给用户一个允许操作的列表。
例如:用户 xyz 具有执行 show、list、search 操作的权限。用户 admin 具有执行编辑、删除等操作的权限。
我需要从控制器读出所有动作。有人有想法吗?
这将创建一个包含控制器信息的地图列表(“数据”变量)。List 中的每个元素都是一个 Map,键为 'controller',对应于控制器的 URL 名称(eg 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
}
这是一个适用于 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
}
}
}
}
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)}"
}
}
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
我必须提取所有控制器及其各自 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()}"
}
}