0

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?

Fred K
  • 13,249
  • 14
  • 78
  • 103

1 Answers1

2

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;
}
Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • Hi Steve, thanks for your answer (and for your plugin). Could you take a look also to this [question](http://stackoverflow.com/questions/19978971/deadbolt-show-parts-of-template-only-for-current-logged-user)? thanks – Fred K Nov 18 '13 at 13:59