1
votes

In my application authentication scheme is based on user and role. users with particular role have access to particular action method.

To implement this I have done following

1.Write custom action

public class Authorization extends play.mvc.Action.Simple {
    public Promise<Result> call(Context ctx) throws Throwable {
        //check access
        return delegate.call(ctx);
    }
}

2.Annotate controller with action

@With(actions.Authorization.class)
public class Upload extends Controller {
    ....
}

In my action I have access to user which is in session. I want to get current controller and action so that I can authenticate user. Is there any way to this?

I have read about creating custom annotation with params and in each controller pass parameters to identify the action. But it seems to much work and error prone if by mistake i write wrong action name while copy paste.

Thanks

1
Are you looking for a reference to a controller object or just an identifier (i.e. String) of which controller underlies the action? - paulk23
Both will work. I was looking for controller and method, String and object both are ok. - hridayesh
Then are you going to have a big if...else structure in your Authorization class ? You really should define at controller or method level the role(s) required to execute the method and check it in your Authorization class. It will be really less error prone. - c4k
Thanks it was indeed error prone. So I have created custom annotation for each type of access an put it in each controller method. - hridayesh

1 Answers

0
votes

You can use context.request().path() to get a string with the path used. This will not include any query parameters, so if you want them you can use getQueryParameter or uri (full url) methods of the same object.

If you need more information than this you can use context.args. This will give you a map with the following keys (in Play 2.2 atleast):

ROUTE_VERB          -> The HTTP verb used (ex: GET)
ROUTE_ACTION_METHOD -> The method called (ex: getUserFavorites)
ROUTE_CONTROLLER    -> Controller class (ex: controllers.api.UsersController)
ROUTE_COMMENTS      -> ???
ROUTE_PATTERN       -> THE URL used (ex: users/favorites)