playframeworkplayframework-2.2deadbolt

Handling redirect to a specific page


I'm using Deadbolt for authorization. I need to redirect an user if he is present (subjectPresent). For example, this controller renders the signup page:

public static Result signup() {
     return ok(signup.render())
 }

But if a user is already present (then he's already registered) the above controller has to redirect him to his profile page: return ok(profilePage.render())

How can do it with annotation?


Solution

  • Deadbolt isn't really for this kind of conditional switching, but you could hack it in the following way:

    1. Create another DeadboltHandler, called something like SubjectPresentHandler
    2. Implement the SubjectPresentHandler#onAuthFailure method to redirect to the profile page
    3. Annotate your signup method with

      @SubjectNotPresent(handler=SubjectPresentHandler.class)

    This causes an authorisation failure if a user is present. This will then invoke SubjectPresentHandler#onAuthFailure to obtain the result.

    However, personally I would consider adding a simple test within the signup method along the lines of

    public static Result signup() {
        Result result;
        User user = // however you normally get your user
        if (user == null) {
            result = ok(signup.render())
        } else {
            result = redirect(routes.<your profile view method>);
        }
        return result;
    }