-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
65 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
35 changes: 35 additions & 0 deletions
35
module/src/main/scala/jp/t2v/lab/play20/auth/AuthElement.scala
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
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) | ||
} |
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
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