Add new user creation stuff.

This commit is contained in:
takezoe
2013-06-04 12:25:43 +09:00
parent 99e6abf833
commit dd7fef93fc
5 changed files with 126 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) { override def init(context: ServletContext) {
context.mount(new IndexController, "/") context.mount(new IndexController, "/")
context.mount(new SignInController, "/*") context.mount(new SignInController, "/*")
context.mount(new UsersController, "/*")
context.mount(new WikiController, "/*") context.mount(new WikiController, "/*")
context.mount(new CreateRepositoryController, "/*") context.mount(new CreateRepositoryController, "/*")
context.mount(new RepositoryViewerController, "/*") context.mount(new RepositoryViewerController, "/*")

View File

@@ -1,9 +1,59 @@
package app package app
class UsersController extends ControllerBase { import model._
import service._
import jp.sf.amateras.scalatra.forms._
get("/"){ class UsersController extends UsersControllerBase with AccountService
trait UsersControllerBase extends ControllerBase { self: AccountService =>
case class NewUserForm(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)))))
)(NewUserForm.apply)
get("/admin/users"){
admin.html.userlist(getAllUsers())
} }
get("/admin/users/_new"){
admin.html.useredit(None)
}
post("/admin/users/_new", newForm){ 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/:name/_edit"){
//
// }
//
// post("/admin/users/:name/_edit"){
//
// }
def unique: Constraint = new Constraint(){
def validate(name: String, value: String): Option[String] =
getAccountByUserName(value).map { _ => "User already exists." }
}
} }

View File

@@ -8,6 +8,11 @@ trait AccountService {
def getAccountByUserName(userName: String): Option[Account] = def getAccountByUserName(userName: String): Option[Account] =
Query(Accounts) filter(_.userName is userName.bind) firstOption Query(Accounts) filter(_.userName is userName.bind) firstOption
def getAllUsers(): List[Account] =
Query(Accounts) sortBy(_.userName) list
def createAccount(account: Account): Unit = Accounts.* insert account
} }

View File

@@ -0,0 +1,38 @@
@(account: Option[model.Account])(implicit context: app.Context)
@import context._
@html.main("New User"){
<form method="POST" action="@path/admin/users/_new" validate="true">
<fieldset>
<label for="userName"><strong>Usrename</strong></label>
<input type="text" name="userName" id="userName" />
<span id="error-userName" class="error"></span>
</fieldset>
<fieldset>
<label for="password"><strong>Password</strong></label>
<input type="password" name="password" id="password" />
<span id="error-password" class="error"></span>
</fieldset>
<fieldset>
<label for="mailAddress"><strong>Mail Address</strong></label>
<input type="text" name="mailAddress" id="mailAddress" />
<span id="error-mailAddress" class="error"></span>
</fieldset>
<fieldset>
<label><strong>User Type</strong></label>
<label for="userType_Normal">
<input type="radio" name="userType" id="userType_Normal" value="0" selected/> Normal
</label>
<label for="userType_Admin">
<input type="radio" name="userType" id="userType_Admin" value="1"/> Administrator
</label>
</fieldset>
<fieldset>
<label><strong>URL</strong></label>
<input type="text" name="url" id="url" style="width: 400px;"/>
<span id="error-url" class="error"></span>
</fieldset>
<fieldset>
<input type="submit" value="Create User"/>
</fieldset>
</form>
}

View File

@@ -0,0 +1,29 @@
@(users: List[model.Account])(implicit context: app.Context)
@import context._
@html.main("Manage Users"){
<div style="text-align: right; margin-bottom: 4px;">
<a href="@path/admin/users/_new" class="btn">New User</a>
</div>
<table class="table table-borderd">
<tr>
<th>Username</th>
<th>Mail Address</th>
<th>Type</th>
<th>URL</th>
<th>Registered</th>
<th>Updated</th>
<th>Last Login</th>
</tr>
@users.map { account =>
<tr>
<td><a href="@path/admin/users/@account.userName/_edit">@account.userName</a></td>
<td>@account.mailAddress</td>
<td>@account.userType</td>
<td>@account.url</td>
<td>@account.registeredDate</td>
<td>@account.updatedDate</td>
<td>@account.lastLoginDate</td>
</tr>
}
</table>
}