Add System Settings page.

This commit is contained in:
takezoe
2013-06-28 13:45:31 +09:00
parent a5cb3bc9cd
commit 00ad0c1745
9 changed files with 195 additions and 70 deletions

View File

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

View File

@@ -0,0 +1,30 @@
package app
import service.{AccountService, SystemSettingsService}
import SystemSettingsService._
import util.AdminOnlyAuthenticator
import jp.sf.amateras.scalatra.forms._
class SystemSettingsController extends SystemSettingsControllerBase
with SystemSettingsService with AccountService with AdminOnlyAuthenticator
trait SystemSettingsControllerBase extends ControllerBase {
self: SystemSettingsService with AccountService with AdminOnlyAuthenticator =>
private case class SystemSettingsForm(allowAccountRegistration: Boolean)
private val form = mapping(
"allowAccountRegistration" -> trim(label("Account registration", boolean()))
)(SystemSettingsForm.apply)
get("/admin/system")(adminOnly {
admin.html.system(loadSystemSettings())
})
post("/admin/system", form)(adminOnly { form =>
saveSystemSettings(SystemSettings(form.allowAccountRegistration))
redirect("/admin/system")
})
}

View File

@@ -5,9 +5,9 @@ import service._
import util.AdminOnlyAuthenticator
import jp.sf.amateras.scalatra.forms._
class UsersController extends UsersControllerBase with AccountService with AdminOnlyAuthenticator
class UserManagementController extends UserManagementControllerBase with AccountService with AdminOnlyAuthenticator
trait UsersControllerBase extends ControllerBase { self: AccountService with AdminOnlyAuthenticator =>
trait UserManagementControllerBase extends ControllerBase { self: AccountService with AdminOnlyAuthenticator =>
case class UserForm(userName: String, password: String, mailAddress: String, isAdmin: Boolean, url: Option[String])

View File

@@ -0,0 +1,40 @@
package service
import util.Directory._
import SystemSettingsService._
trait SystemSettingsService {
def saveSystemSettings(settings: SystemSettings): Unit = {
val props = new java.util.Properties()
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
props.store(new java.io.FileOutputStream(GitBucketConf), null)
}
def loadSystemSettings(): SystemSettings = {
val props = new java.util.Properties()
if(GitBucketConf.exists){
props.load(new java.io.FileInputStream(GitBucketConf))
}
SystemSettings(getBoolean(props, "allow_account_registration"))
}
}
object SystemSettingsService {
case class SystemSettings(allowAccountRegistration: Boolean)
private val AllowAccountRegistration = "allow_account_registration"
private def getBoolean(props: java.util.Properties, key: String, default: Boolean = false): Boolean = {
val value = props.getProperty(key)
if(value == null || value.isEmpty){
default
} else {
value.toBoolean
}
}
}

View File

@@ -11,6 +11,8 @@ object Directory {
val GitBucketHome = new File(System.getProperty("user.home"), "gitbucket").getAbsolutePath
val GitBucketConf = new File(GitBucketHome, "gitbucket.conf")
val RepositoryHome = "%s/repositories".format(GitBucketHome)
/**

View File

@@ -0,0 +1,17 @@
@(active: String)(body: Html)(implicit context: app.Context)
@import context._
<div class="row-fluid">
<div class="span3">
<ul class="nav nav-tabs nav-stacked">
<li@if(active=="users"){ class="active"}>
<a href="@path/admin/users">User Management</a>
</li>
<li@if(active=="system"){ class="active"}>
<a href="@path/admin/system">System Settings</a>
</li>
</ul>
</div>
<div class="span9">
@body
</div>
</div>

View File

@@ -0,0 +1,28 @@
@(settings: service.SystemSettingsService.SystemSettings)(implicit context: app.Context)
@import context._
@import view.helpers._
@html.main("System Settings"){
@menu("system"){
<form action="@path/admin/system" method="POST" validate="true">
<div class="box">
<div class="box-header">System Settings</div>
<div class="box-content">
<label><strong>Account registration</strong></label>
<fieldset>
<label>
<input type="radio" name="allowAccountRegistration" value="true"@if(settings.allowAccountRegistration){ checked}>
<strong>Allow</strong> - Users can create account by themselves.
</label>
<label>
<input type="radio" name="allowAccountRegistration" value="false"@if(!settings.allowAccountRegistration){ checked}>
<strong>Deny</strong> - Only administrators can create account.
</label>
</fieldset>
</div>
</div>
<fieldset>
<input type="submit" class="btn btn-success" value="Apply changes"/>
</fieldset>
</form>
}
}

View File

@@ -1,6 +1,7 @@
@(account: Option[model.Account])(implicit context: app.Context)
@import context._
@html.main(if(account.isEmpty) "New User" else "Update User"){
@menu("users"){
<form method="POST" action="@if(account.isEmpty){@path/admin/users/_new} else {@path/admin/users/@account.get.userName/_edit}" validate="true">
<fieldset>
<label for="userName"><strong>Username</strong></label>
@@ -36,4 +37,5 @@
<a href="@path/admin/users" class="btn">Cancel</a>
</fieldset>
</form>
}
}

View File

@@ -1,36 +1,41 @@
@(users: List[model.Account])(implicit context: app.Context)
@import context._
@import view.helpers
@import view.helpers._
@html.main("Manage Users"){
@menu("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 table-hover">
<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>
<table class="table table-bordered table-hover">
@users.map { account =>
<tr>
<td><a href="@path/admin/users/@account.userName/_edit">@account.userName</a></td>
<td>@account.mailAddress</td>
<td>
<div class="pull-right">
<a href="@path/admin/users/@account.userName/_edit">Edit</a>
</div>
<div class="strong">
<a href="@url(account.userName)">@account.userName</a>
@if(account.isAdmin){
Administrator
(Administrator)
} else {
Normal
(Normal)
}
</div>
<div>
<hr>
<i class="icon-envelope"></i> @account.mailAddress
@account.url.map { url =>
<i class="icon-home"></i> @url
}
</div>
<div>
<span class="muted">Registered:</span> @datetime(account.registeredDate)
<span class="muted">Updated:</span> @datetime(account.updatedDate)
<span class="muted">Last Login:</span> @account.lastLoginDate.map(datetime)
</div>
</td>
<td>@account.url</td>
<td>@helpers.datetime(account.registeredDate)</td>
<td>@helpers.datetime(account.updatedDate)</td>
<td>@account.lastLoginDate.map(helpers.datetime)</td>
</tr>
}
</table>
}
}