读出Grails-Controller中的所有操作 [英] Reading out all actions in a Grails-Controller

查看:84
本文介绍了读出Grails-Controller中的所有操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的网络应用中的任何控制器读出所有可用的操作。其原因是授权系统,我需要给用户一个允许的操作列表。

例如:
用户xyz拥有执行操作show,list,search的权限。
用户管理员具有执行操作编辑,删除等的权限。



我需要读出控制器的所有操作。有没有人有一个想法?

解决方案

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

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

def data = []
for(控制器在grailsApplication.controllerClasses中){
def controllerInfo = [:]
controllerInfo.controller = controller.logicalPropertyName
controllerInfo.controllerName = controller.fullName
列表actions = []
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(controller.newInstance())
for(pd in beanWrapper.propertyDescriptors){
字符串closureClassName = controller.getPropertyOrStaticPropertyOrFieldValue(pd.name,Closure)?.class?.name
如果(closureClassName)动作<< pd.name
}
controllerInfo.actions = actions.sort()
data<< controllerInfo
}


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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆