Nathan Edwards
Nathan Edwards

Reputation: 311

Current Request/User details in Models in Scala Play! 2.5

I would like to have access to the current user somewhere deep in my models of my Play app, for things like setting the author, checking that the user can actually save this type, etc.

Ideally, what I would like to use is Guice's @RequestScoped to inject the same UserIdentity across my request, wherever I need it. However, as far as I can tell, the Play! Framework only supports @Singleton and no-scope. So either we'd get the same UserIdentity injected across requests or a different one for every model/util we requested. Both are no-gos for obvious reasons.

Is there some way to utilise this behaviour in Play 2.5?

Other things I have tried

I've tried using a combination of Play's session and cache. But the problem I have is that session is immutable, so I can't add anything to it to reuse in that same request.

I've looked at a bunch of auth frameworks but they all seem to focus on securing actions, not providing me with a current User object.

Upvotes: 1

Views: 828

Answers (1)

andyczerwonka
andyczerwonka

Reputation: 4260

Check out my answer to a question on redirecting requests, where I given an example of getting the current user in each request.

In this case the authorization key is handed out on login and the client passes it in on every request thereafter.

Upvotes: 0

Related Questions