Newer
Older
gitbucket_jkp / src / main / scala / app / UsersController.scala
package app

import model._
import service._
import util.AdminOnlyAuthenticator
import jp.sf.amateras.scalatra.forms._

class UsersController extends UsersControllerBase with AccountService with AdminOnlyAuthenticator

trait UsersControllerBase extends ControllerBase { self: AccountService with AdminOnlyAuthenticator =>
  
  case class UserForm(userName: String, password: String, mailAddress: String, isAdmin: Boolean, url: Option[String])
  
  val newForm = mapping(
    "userName"    -> trim(label("Username"     , text(required, maxlength(100), identifier, unique))),
    "password"    -> trim(label("Password"     , text(required, maxlength(100)))),
    "mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
    "isAdmin"     -> trim(label("User Type"    , boolean())),
    "url"         -> trim(label("URL"          , optional(text(maxlength(200)))))
  )(UserForm.apply)

  val editForm = mapping(
    "userName"    -> trim(label("Username"     , text(required, maxlength(100), identifier))),
    "password"    -> trim(label("Password"     , text(required, maxlength(100)))),
    "mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
    "isAdmin"     -> trim(label("User Type"    , boolean())),
    "url"         -> trim(label("URL"          , optional(text(maxlength(200)))))
  )(UserForm.apply)
  
  get("/admin/users")(adminOnly {
    admin.html.userlist(getAllUsers())
  })
  
  get("/admin/users/_new")(adminOnly {
    admin.html.useredit(None)
  })
  
  post("/admin/users/_new", newForm)(adminOnly { form =>
    // TODO make a common function to get the current timestamp.
    val currentDate = new java.sql.Timestamp(System.currentTimeMillis)
    createAccount(Account(
        userName       = form.userName, 
        password       = form.password, 
        mailAddress    = form.mailAddress,
        isAdmin        = form.isAdmin,
        url            = form.url, 
        registeredDate = currentDate, 
        updatedDate    = currentDate, 
        lastLoginDate  = None))
        
    redirect("/admin/users")
  })
  
  get("/admin/users/:userName/_edit")(adminOnly {
    val userName = params("userName")
    admin.html.useredit(getAccountByUserName(userName))
  })
  
  post("/admin/users/:name/_edit", editForm)(adminOnly { form =>
    val userName = params("userName")
    // TODO make a common function to get the current timestamp.
    val currentDate = new java.sql.Timestamp(System.currentTimeMillis)
    updateAccount(getAccountByUserName(userName).get.copy(
        password     = form.password,
        mailAddress  = form.mailAddress,
        isAdmin      = form.isAdmin,
        url          = form.url,
        updatedDate  = currentDate))
    
    redirect("/admin/users")
  })

  private def unique: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] =
      getAccountByUserName(value).map { _ => "User already exists." }
  }  
  
}