61119

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

Question:

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?

<strong>Other things I have tried</strong>

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.

Answer1:

Check out <a href="https://stackoverflow.com/questions/36797048/how-to-redirect-request-in-play-framework-with-scala/36797143#36797143" rel="nofollow">my answer to a question on redirecting requests</a>, 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.

Recommend

  • Add custom page/field to NSIS setup created with electron-builder
  • RChannel Sensitivity in DICOM waveforms
  • Overlay Two ggplot in Shiny
  • How do I access a constant in Perl whose name is contained in a variable?
  • Task.ContinueWith confusion
  • IEnumerable from IEnumerator
  • Oracle Java tutorial - possible error at answer to question
  • How to get javascript output in python BeautifulSoup or any other module
  • Disable CollapsingToolbarLayout
  • When reading a binary file with java, how? [closed]
  • PostgreSQL manualy change query execution plan to force using sort and sequential access instead of
  • Two way binding in Angular without Input
  • output from solvePnP doesn't match projectPoints
  • Fill polygon with pattern doesn't work with leaflet
  • How to rename files in bash to increase number in name?
  • Python multiprocessing using a lock or manager list for Pool workers accessing a global list variabl
  • Catch panning event in Highcharts
  • Copy and reformat cells from one worksheet to another
  • spinlock initialization function
  • Get the output of multiple commands from subprocess.Popen
  • How to expire a session if user leave an interface for 2 minutes? ASP.NET
  • TeamCity: Scripting elements jsp:declaration, jsp:expression, jsp:scriptlet are disallowed here
  • Modification of innerHTML stored in variable not working
  • Excel VBA: Forward ParamArray to Application.Run
  • ARKit – Rendering a 3D object under an invisible plane
  • Stop the background service after particular time in android
  • Search image on Google images with the new Custom Search API?
  • How to achieve density/heat map effect in iOS (iPhone/iPad)?
  • MySQL - Filter records which date is biggest
  • Django self join , How to convert this query to ORM query
  • Find corners of a rotated rectangle
  • cSPADE data mining in R using arulesSequences - Error while converting to “transactions” format
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • How do I use libcurl to printf a remote FTP directory listing?
  • How to split wav file into two or more parts using c#
  • Print Longitude and Latitude in textbox Android
  • iphone image to video issue in video speed
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app