Implementing authentication.

This commit is contained in:
takezoe
2013-06-03 00:36:51 +09:00
parent d3a985d65a
commit 1ab58d0363
8 changed files with 83 additions and 45 deletions

View File

@@ -1,9 +1,9 @@
package app
import model.Account
import org.scalatra._
import org.scalatra.json._
import org.json4s._
import org.json4s.jackson._
import jp.sf.amateras.scalatra.forms._
/**
@@ -13,11 +13,35 @@ abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFo
implicit val jsonFormats = DefaultFormats
implicit def context: Context = Context(servletContext.getContextPath, LoginUser)
// TODO get from session
private val LoginUser = "admin" //System.getProperty("user.name")
implicit def context: Context = Context(servletContext.getContextPath, LoginAccount)
private def LoginAccount: Option[Account] = {
session.get("LOGIN_ACCOUNT") match {
case Some(x: Account) => Some(x)
case _ => None
}
}
protected def usersOnly(action: => Any) = {
{
context.loginAccount match {
case Some(x) => action
case None => redirect("/signin")
}
}
}
protected def usersOnly[T](action: T => Any) = {
(form: T) => {
context.loginAccount match {
case Some(x) => action(form)
case None => redirect("/signin")
}
}
}
// protected def adminOnly()
}
case class Context(path: String, loginUser: String)
case class Context(path: String, loginAccount: Option[Account])