Skip to content

Commit

Permalink
Applied Stackable Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
gakuzzzz committed Feb 18, 2013
1 parent 895afc7 commit 1e17d0d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
38 changes: 16 additions & 22 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import play.api.mvc.Results._
import jp.t2v.lab.play20.auth._
import play.api.Play._
import play.api.cache.Cache
import scala.reflect.classTag
import reflect.{ClassTag, classTag}
import jp.t2v.lab.play2.stackc.{RequestWithAttributes, RequestAttributeKey, StackableController}

object Application extends Controller with LoginLogout with AuthConfigImpl {

Expand All @@ -37,25 +38,25 @@ object Application extends Controller with LoginLogout with AuthConfigImpl {
}

}
object Message extends Base {
object Message extends Controller with Pjax with AuthElement with AuthConfigImpl {

def main = compositeAction(NormalUser) { user => implicit template => implicit request =>
def main = StackAction(AuthorityKey -> NormalUser) { implicit request =>
val title = "message main"
Cache.set("hoge", "testtttttt")
Ok(html.message.main(title))
}

def list = compositeAction(NormalUser) { user => implicit template => implicit request =>
def list = StackAction(AuthorityKey -> NormalUser) { implicit request =>
val title = Cache.getAs[String]("hoge").getOrElse("all messages")
Ok(html.message.list(title))
}

def detail(id: Int) = compositeAction(NormalUser) { user => implicit template => implicit request =>
def detail(id: Int) = StackAction(AuthorityKey -> NormalUser) { implicit request =>
val title = "messages detail "
Ok(html.message.detail(title + id))
}

def write = compositeAction(Administrator) { user => implicit template => implicit request =>
def write = StackAction(AuthorityKey -> Administrator) { implicit request =>
val title = "write message"
Ok(html.message.write(title))
}
Expand Down Expand Up @@ -93,25 +94,18 @@ trait AuthConfigImpl extends AuthConfig {

}

trait Base extends Controller with Auth with Pjax with AuthConfigImpl {
trait Pjax extends StackableController {
self: Controller with AuthElement with AuthConfigImpl =>

def compositeAction(permission: Permission)(f: Account => Template => RequestHeader => PlainResult) =
Action { implicit request =>
(for {
user <- authorized(permission).right
template <- pjax(user).right
} yield f(user)(template)(request)).merge
}

}
type Template = String => Html => Html

trait Pjax {
self: Controller =>
case object TemplateKey extends RequestAttributeKey

type Template = String => Html => Html
def pjax(user: Account)(implicit request: RequestHeader): Either[PlainResult, Template] = Right {
if (request.headers.keys("X-PJAX")) html.pjaxTemplate.apply
else html.fullTemplate.apply(user)
abstract override def proceed[A](req: RequestWithAttributes[A])(f: RequestWithAttributes[A] => Result): Result = {
val template: Template = if (req.headers.keys("X-Pjax")) html.pjaxTemplate.apply else html.fullTemplate.apply(loggedIn(req))
super.proceed(req.set(TemplateKey, template))(f)
}

implicit def template[A](implicit req: RequestWithAttributes[A]): Template = req.getAs[Template](TemplateKey).get

}
8 changes: 5 additions & 3 deletions module/build.sbt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
name := "play21.auth"

version := "0.7"
version := "0.8-SNAPSHOT"

scalaVersion := "2.10.0"

resolvers ++= Seq(
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
)

libraryDependencies ++= Seq(
"play" %% "play" % "2.1.0"
"play" %% "play" % "2.1.0",
"jp.t2v" %% "stackable-controller" % "0.1-SNAPSHOT"
)

organization := "jp.t2v"
Expand Down
4 changes: 2 additions & 2 deletions module/src/main/scala/jp/t2v/lab/play20/auth/Auth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import play.api.mvc._
import play.api.libs.iteratee.{Input, Done}

trait Auth {
self: Controller with AuthConfig =>
self: AuthConfig =>

def authorizedAction(authority: Authority)(f: User => Request[AnyContent] => Result): Action[(AnyContent, User)] =
authorizedAction(BodyParsers.parse.anyContent, authority)(f)
Expand All @@ -30,7 +30,7 @@ trait Auth {
_ <- Either.cond(authorize(user, authority), (), authorizationFailed(request)).right
} yield user

private def restoreUser(implicit request: RequestHeader): Option[User] = for {
private[auth] def restoreUser(implicit request: RequestHeader): Option[User] = for {
cookie <- request.cookies.get(cookieName)
token <- CookieUtil.verifyHmac(cookie)
userId <- idContainer.get(token)
Expand Down
35 changes: 35 additions & 0 deletions module/src/main/scala/jp/t2v/lab/play20/auth/AuthElement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package jp.t2v.lab.play20.auth

import play.api.mvc.{Result, Controller}
import jp.t2v.lab.play2.stackc.{RequestWithAttributes, RequestAttributeKey, StackableController}

trait AuthElement extends StackableController with Auth {
self: Controller with AuthConfig =>

case object AuthKey extends RequestAttributeKey
case object AuthorityKey extends RequestAttributeKey

abstract override def proceed[A](req: RequestWithAttributes[A])(f: RequestWithAttributes[A] => Result): Result = {
(for {
authority <- req.getAs[Authority](AuthorityKey).toRight(authorizationFailed(req)).right
user <- authorized(authority)(req).right
} yield super.proceed(req.set(AuthKey, user))(f)).merge
}

implicit def loggedIn[A](implicit req: RequestWithAttributes[A]): User = req.getAs[User](AuthKey).get

}


trait OptionalAuthElement extends StackableController with Auth {
self: Controller with AuthConfig =>

case object AuthKey extends RequestAttributeKey

abstract override def proceed[A](req: RequestWithAttributes[A])(f: RequestWithAttributes[A] => Result): Result = {
val maybeUser = restoreUser(req)
super.proceed(req.set(AuthKey, maybeUser.getOrElse(null)))(f)
}

implicit def loggedIn[A](implicit req: RequestWithAttributes[A]): Option[User] = req.getAs[User](AuthKey)
}
8 changes: 6 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ object ApplicationBuild extends Build {
"org.mindrot" % "jbcrypt" % "0.3m"
)

lazy val root = play.Project(appName, appVersion, appDependencies).settings(
resolvers += "jbcrypt repo" at "http://mvnrepository.com/"
lazy val root = play.Project(appName, appVersion, appDependencies, path = file(".")).settings(
resolvers ++= Seq(
"jbcrypt repo" at "http://mvnrepository.com/",
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
)
).dependsOn(module)


}
3 changes: 1 addition & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ logLevel := Level.Warn

// The Typesafe repository
resolvers ++= Seq(
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",
"sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
)

// Use the Play sbt plugin for Play projects
Expand Down

0 comments on commit 1e17d0d

Please sign in to comment.