Last active
September 28, 2015 06:08
-
-
Save julienrf/1396370 to your computer and use it in GitHub Desktop.
Dependency injection in Scala with Play 2: it’s free
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MyApp extends Controller { | |
// Home page | |
val index = Action { | |
Ok(views.html.index()) | |
} | |
// Login action: here I just bind a (non empty) username from the query and put it in the session | |
val login = Action { implicit request => | |
Form(mapping("username" -> nonEmptyText)(identity)(Some(_))).bindFromRequest.fold( | |
noUser => redirectToIndex, | |
username => redirectToIndex.withSession("username" -> username) | |
) | |
} | |
// Logout action: clear the session and redirect to the home page | |
val logout = Action { | |
redirectToIndex.withNewSession | |
} | |
// Convenient shortcut | |
val redirectToIndex = Redirect(routes.MyApp.index) | |
} | |
object Secured { | |
// Authentication check: executes the wrapped action only if a username is found in the session | |
def Authenticated[A](action: Action[A]): Action[A] = Action(action.parser) { request => | |
request.session.get("username") match { | |
case Some(user) => action(request) | |
case None => Unauthorized(views.html.unauthorized()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val index = Secured.Authenticated { | |
Action { | |
Ok(views.html.index()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MockSecured { | |
def Authenticated[A](action: Action[A]) = action | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Security { | |
def Authenticated[A](action: Action[A]): Action[A] | |
} | |
trait Secured extends Security { | |
override def Authenticated[A](action: Action[A]) = Action(action.parser) { request => | |
// … | |
} | |
} | |
trait MockedSecured extends Security { | |
override def Authenticated[A](action: Action[A]) = action | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait MyApp extends Controller { | |
this: Security => | |
val index = Authenticated { | |
Action { | |
Ok(views.html.index()) | |
} | |
} | |
// … login and logout actions are not changed | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object myApp { | |
import play.api.Play._ | |
val MyApp = if (isDev) { | |
new controllers.MyApp with controllers.MockSecured | |
} else { | |
new controllers.MyApp with controllers.Secured | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment