读出 Grails-Controller 中的所有动作 [英] Reading out all actions in a Grails-Controller
问题描述
我需要从我的网络应用程序中的任何控制器读出所有可用的操作.这样做的原因是我需要为用户提供一个允许操作列表的授权系统.
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.
例如:用户 xyz 具有执行显示、列表、搜索操作的权限.用户 admin 具有执行编辑、删除等操作的权限.
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?
推荐答案
这将创建一个带有控制器信息的地图列表(数据"变量).List中的每个元素都是一个Map,键为'controller',对应控制器的URL名(例如BookController -> 'book'),controllerName对应类名('BookController'),'actions'对应于该控制器的动作名称列表:
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
}
这篇关于读出 Grails-Controller 中的所有动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!