读出 Grails-Controller 中的所有操作
我需要从我的网络应用程序中的任何控制器读出所有可用的操作。这样做的原因是授权系统,我需要为用户提供允许的操作列表。
例如: 用户 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将创建一个包含控制器信息的地图列表(“数据”变量)。 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:
这是一个适用于 Grails 2 的示例,即它将捕获定义为方法或闭包的操作
Here's an example that works with Grails 2, i.e it will capture actions defined as either methods or closures
Grails 不支持直接的方法来执行此操作。然而,我能够从可用的 grails 方法中整理出一个难题,并得出这个解决方案:
变量 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:
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 methodgetMethodActionName
要打印带有操作名称的所有方法的列表:
To print out a list of all the methods with action names:
我必须获取所有控制器及其各自 URI 的列表。这就是我在 grails 3.1.6 应用程序上所做的。
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.