Files
GitBucket/src/main/scala/app/UsersController.scala
2013-06-05 02:46:16 +09:00

77 lines
2.8 KiB
Scala
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 =>
// TODO ユーザ名の先頭に_は使えないようにする利用可能文字チェック
case class UserForm(userName: String, password: String, mailAddress: String, userType: Int, url: Option[String])
val newForm = mapping(
"userName" -> trim(label("Username" , text(required, maxlength(100), unique))),
"password" -> trim(label("Password" , text(required, maxlength(100)))),
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
"userType" -> trim(label("User Type" , number())),
"url" -> trim(label("URL" , optional(text(maxlength(200)))))
)(UserForm.apply)
val editForm = mapping(
"userName" -> trim(label("Username" , text())),
"password" -> trim(label("Password" , text(required, maxlength(100)))),
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
"userType" -> trim(label("User Type" , number())),
"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 =>
val currentDate = new java.sql.Date(System.currentTimeMillis)
createAccount(Account(
userName = form.userName,
password = form.password,
mailAddress = form.mailAddress,
userType = form.userType,
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")
val currentDate = new java.sql.Date(System.currentTimeMillis)
updateAccount(getAccountByUserName(userName).get.copy(
password = form.password,
mailAddress = form.mailAddress,
userType = form.userType,
url = form.url,
updatedDate = currentDate))
redirect("/admin/users")
})
def unique: Constraint = new Constraint(){
def validate(name: String, value: String): Option[String] =
getAccountByUserName(value).map { _ => "User already exists." }
}
}