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

@@ -10,6 +10,8 @@ import org.eclipse.jgit.lib.Ref
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)