Deadbolt - Play Framework - How to check a @RestrictedResource with parameters in a controller?

三世轮回 提交于 2019-12-05 21:41:43

It's not possible to have dynamic information in an annotation, but you can use params to define the name of an incoming value in the request. However, this information isn't passed into the handler at the moment because it expects a map. While you can pass in a map of parameters from the restrictedResource tag, you can't do this from an annotation so an empty map is passed into the handler.

Your best approach here is to pull a well-known parameter name from the request object. I need to have a rethink about the best way to do this without breaking backwards compatibility.

Steve (author of Deadbolt)

I've found a way the solved the problem, not the best I think, but it is the Steve Chaloner's solution (Deadbolt's creator), and it works.

For example, if your Controller's method argument is named "id", and you want to check this id inside your checkAccess method :

// Controller's method : 
@RestrictedResource(name = {"Domain"})
public static void showDomain(String id){} 

Just check at the beginning of your checkAccess method the Map "resourceParameters" is empty, and use the request object to get the parameters:

public AccessResult checkAccess(List<String> resourceNames,
                                Map<String, String> resourceParameters)
{    
    Map<String, String> hashm = new HashMap<String,String>();

    if(resourceParameters != null && !resourceParameters.isEmpty()){
        hashm = resourceParameters;
    }else if(Http.Request.current().routeArgs!= null && !Http.Request.current().routeArgs.isEmpty()){
        hashm = Http.Request.current().routeArgs;
    }
}

Then just have to foreach your hashmap inside your checkAccess method to get your Controller's method argument and check the Access as you wish.

for (Map.Entry<String,String> mp : hashm.entrySet())
{
    // Get the id argument
    if(mp.getKey().equals("id"))
    {
        // Do something with the value..
        mp.getValue()
    }        
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!