Created
July 18, 2012 06:42
-
-
Save rssh/3134641 to your computer and use it in GitHub Desktop.
jobAction simplicified (yet not ideal)
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 JobAction { | |
def apply(f: JobRequest => Result) = process(mustBeAuthenticated=false)(f) | |
def authenticated = process(mustBeAuthenticated=true) _ | |
def owner(id: ObjectId) = process(mustBeAuthenticated=true, optUserId = Option(id)) _ | |
def admin = process(mustBeAuthenticated=true, mustBeAdmin=true) _ | |
private def process(mustBeAuthenticated: Boolean, mustBeAdmin: Boolean = false, optUserId: Option[ObjectId] = None)(f: JobRequest => Result) = Action { | |
request => | |
(findCurrentSessionAccount(request) match { | |
case Some(account) => if (optUserId.exists( _ != account.id)) { | |
Left("error.no.rights") | |
} else if (mustBeAdmin && !account.isAdmin) { | |
Left("error.must.be.admin") | |
} else { | |
Right(Some(account)) | |
} | |
case None => if (mustBeAuthenticated) { | |
Left("error.must.login") | |
} else { | |
Right(None) | |
} | |
}) fold ( | |
error => Redirect(routes.Application.login).flashing("error" -> Messages(error)), | |
account => f(JobRequest(account,request)) | |
) | |
} | |
private def findCurrentSessionAccount(request: Request[AnyContent]): Option[Account] = | |
request.session.get("accountId").flatMap(id => Account.find(new ObjectId(id))) | |
} | |
case class JobRequest(account: Option[Account], request: Request[AnyContent]) extends WrappedRequest(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment