Reading out all actions in a Grails-Controller

前端 未结 5 572
别跟我提以往
别跟我提以往 2021-01-02 20:53

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 actio

5条回答
  •  温柔的废话
    2021-01-02 21:54

    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:

    def actions = new HashSet()
    def controllerClass = grailsApplication.getArtefactInfo(ControllerArtefactHandler.TYPE)
                             .getGrailsClassByLogicalPropertyName(controllerName)
    for (String uri : controllerClass.uris ) {
        actions.add(controllerClass.getMethodActionName(uri) )
    }
    

    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 method getMethodActionName

提交回复
热议问题