Add account creation page for users.

This commit is contained in:
takezoe
2013-06-28 14:13:58 +09:00
parent 00ad0c1745
commit c308e1993a
4 changed files with 62 additions and 18 deletions

View File

@@ -5,14 +5,23 @@ import util.OwnerOnlyAuthenticator
import jp.sf.amateras.scalatra.forms._ import jp.sf.amateras.scalatra.forms._
class AccountController extends AccountControllerBase class AccountController extends AccountControllerBase
with AccountService with RepositoryService with OwnerOnlyAuthenticator with SystemSettingsService with AccountService with RepositoryService with OwnerOnlyAuthenticator
trait AccountControllerBase extends ControllerBase { trait AccountControllerBase extends ControllerBase {
self: AccountService with RepositoryService with OwnerOnlyAuthenticator => self: SystemSettingsService with AccountService with RepositoryService with OwnerOnlyAuthenticator =>
case class AccountNewForm(userName: String, password: String,mailAddress: String, url: Option[String])
case class AccountEditForm(mailAddress: String, url: Option[String]) case class AccountEditForm(mailAddress: String, url: Option[String])
val form = mapping( val newForm = mapping(
"userName" -> trim(label("User name" , text(required, maxlength(100)))),
"password" -> trim(label("Password" , text(required, maxlength(20)))),
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
"url" -> trim(label("URL" , optional(text(maxlength(200)))))
)(AccountNewForm.apply)
val editForm = mapping(
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))), "mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
"url" -> trim(label("URL" , optional(text(maxlength(200))))) "url" -> trim(label("URL" , optional(text(maxlength(200)))))
)(AccountEditForm.apply) )(AccountEditForm.apply)
@@ -29,13 +38,29 @@ trait AccountControllerBase extends ControllerBase {
get("/:userName/_edit")(ownerOnly { get("/:userName/_edit")(ownerOnly {
val userName = params("userName") val userName = params("userName")
getAccountByUserName(userName).map(account.html.useredit(_)) getOrElse NotFound getAccountByUserName(userName).map(x => account.html.useredit(Some(x))) getOrElse NotFound
}) })
post("/:userName/_edit", form)(ownerOnly { form => post("/:userName/_edit", editForm)(ownerOnly { form =>
val userName = params("userName") val userName = params("userName")
updateAccount(getAccountByUserName(userName).get.copy(mailAddress = form.mailAddress, url = form.url)) getAccountByUserName(userName).map { account =>
updateAccount(account.copy(
mailAddress = form.mailAddress,
url = form.url))
redirect("/%s".format(userName)) redirect("/%s".format(userName))
} getOrElse NotFound
}) })
get("/register"){
if(loadSystemSettings().allowAccountRegistration){
account.html.useredit(None)
} else NotFound
}
post("/register", newForm){ newForm =>
if(loadSystemSettings().allowAccountRegistration){
createAccount(newForm.userName, newForm.password, newForm.mailAddress, false, newForm.url)
redirect("/signin")
} else NotFound
}
} }

View File

@@ -3,9 +3,9 @@ package app
import service._ import service._
import jp.sf.amateras.scalatra.forms._ import jp.sf.amateras.scalatra.forms._
class SignInController extends SignInControllerBase with AccountService class SignInController extends SignInControllerBase with SystemSettingsService with AccountService
trait SignInControllerBase extends ControllerBase { self: AccountService => trait SignInControllerBase extends ControllerBase { self: SystemSettingsService with AccountService =>
case class SignInForm(userName: String, password: String) case class SignInForm(userName: String, password: String)
@@ -15,7 +15,7 @@ trait SignInControllerBase extends ControllerBase { self: AccountService =>
)(SignInForm.apply) )(SignInForm.apply)
get("/signin"){ get("/signin"){
html.signin() html.signin(loadSystemSettings())
} }
post("/signin", form){ form => post("/signin", form){ form =>

View File

@@ -1,21 +1,37 @@
@(account: model.Account)(implicit context: app.Context) @(account: Option[model.Account])(implicit context: app.Context)
@import context._ @import context._
@import view.helpers._ @import view.helpers._
@html.main(account.userName){ @html.main(account.map(_.userName).getOrElse("Create account")){
<form action="@url(account.userName)/_edit" method="POST" validate="true"> <form action="@if(account.isDefined){url(account.get.userName)/_edit}else{/register}" method="POST" validate="true">
@if(account.isEmpty){
<fieldset>
<label><strong>User name</strong></label>
<input type="text" name="userName" id="userName" value=""/>
<span id="error-userName" class="error"></span>
</fieldset>
<fieldset>
<label><strong>Password</strong></label>
<input type="password" name="password" id="password" value=""/>
<span id="error-password" class="error"></span>
</fieldset>
}
<fieldset> <fieldset>
<label><strong>Mail Address</strong></label> <label><strong>Mail Address</strong></label>
<input type="text" name="mailAddress" id="mailAddress" value="@account.mailAddress"/> <input type="text" name="mailAddress" id="mailAddress" value="@account.map(_.mailAddress)"/>
<span id="error-mailAddress" class="error"></span> <span id="error-mailAddress" class="error"></span>
</fieldset> </fieldset>
<fieldset> <fieldset>
<label><strong>URL (Optional)</strong></label> <label><strong>URL (Optional)</strong></label>
<input type="text" name="url" id="url" style="width: 400px;" value="@account.url"/> <input type="text" name="url" id="url" style="width: 400px;" value="@account.map(_.url)"/>
<span id="error-url" class="error"></span> <span id="error-url" class="error"></span>
</fieldset> </fieldset>
<fieldset> <fieldset>
@if(account.isDefined){
<input type="submit" class="btn btn-success" value="Save"/> <input type="submit" class="btn btn-success" value="Save"/>
<a href="@url(account.userName)" class="btn">Cancel</a> <a href="@url(account.get.userName)" class="btn">Cancel</a>
} else {
<input type="submit" class="btn btn-success" value="Create account"/>
}
</fieldset> </fieldset>
</form> </form>
} }

View File

@@ -1,4 +1,4 @@
@()(implicit context: app.Context) @(systemSettings: service.SystemSettingsService.SystemSettings)(implicit context: app.Context)
@import context._ @import context._
@main("Sign in"){ @main("Sign in"){
<form action="@path/signin" method="POST" validate="true"> <form action="@path/signin" method="POST" validate="true">
@@ -10,6 +10,9 @@
<span id="error-password" class="error"></span> <span id="error-password" class="error"></span>
<div> <div>
<input type="submit" class="btn btn-success" value="Sign in"/> <input type="submit" class="btn btn-success" value="Sign in"/>
@if(systemSettings.allowAccountRegistration){
<a href="@path/register" class="btn">Create new account</a>
}
</div> </div>
</form> </form>
} }