diff --git a/src/main/scala/app/SystemSettingsController.scala b/src/main/scala/app/SystemSettingsController.scala index cb1c10a..a8f87e9 100644 --- a/src/main/scala/app/SystemSettingsController.scala +++ b/src/main/scala/app/SystemSettingsController.scala @@ -12,15 +12,21 @@ trait SystemSettingsControllerBase extends ControllerBase with FlashMapSupport { self: SystemSettingsService with AccountService with AdminAuthenticator => - private case class SystemSettingsForm( - allowAccountRegistration: Boolean, - gravatar: Boolean - ) - private val form = mapping( "allowAccountRegistration" -> trim(label("Account registration", boolean())), - "gravatar" -> trim(label("Gravatar", boolean())) - )(SystemSettingsForm.apply) + "gravatar" -> trim(label("Gravatar", boolean())), + "notification" -> trim(label("Notification", boolean())), + "smtp" -> mapping( + "host" -> trim(label("SMTP Host", text(new Constraint(){ + def validate(name: String, value: String): Option[String] = + if(params.get("notification").exists(_ == "on")) required.validate(name, value) else None + }))), + "port" -> trim(label("SMTP Port", optional(number()))), + "user" -> trim(label("SMTP User", optional(text()))), + "password" -> trim(label("SMTP Password", optional(text()))), + "ssl" -> trim(label("Enable SSL", optional(boolean()))) + )(Smtp.apply) + )(SystemSettings.apply) get("/admin/system")(adminOnly { @@ -28,10 +34,7 @@ }) post("/admin/system", form)(adminOnly { form => - saveSystemSettings(SystemSettings( - form.allowAccountRegistration, - form.gravatar - )) + saveSystemSettings(form) flash += "info" -> "System settings has been updated." redirect("/admin/system") }) diff --git a/src/main/scala/service/SystemSettingsService.scala b/src/main/scala/service/SystemSettingsService.scala index 36368b8..0d63f6e 100644 --- a/src/main/scala/service/SystemSettingsService.scala +++ b/src/main/scala/service/SystemSettingsService.scala @@ -9,6 +9,14 @@ val props = new java.util.Properties() props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString) props.setProperty(Gravatar, settings.gravatar.toString) + props.setProperty(Notification, settings.notification.toString) + if(settings.notification) { + props.setProperty(SmtpHost, settings.smtp.host) + settings.smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString)) + settings.smtp.user.foreach(props.setProperty(SmtpUser, _)) + settings.smtp.password.foreach(props.setProperty(SmtpPassword, _)) + settings.smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString)) + } props.store(new java.io.FileOutputStream(GitBucketConf), null) } @@ -19,29 +27,62 @@ props.load(new java.io.FileInputStream(GitBucketConf)) } SystemSettings( - getBoolean(props, AllowAccountRegistration), - getBoolean(props, Gravatar, true)) + getValue(props, AllowAccountRegistration, false), + getValue(props, Gravatar, true), + getValue(props, Notification, false), + Smtp( + getValue(props, SmtpHost, ""), + getOptionValue(props, SmtpPort, Some(25)), + getOptionValue(props, SmtpUser, None), + getOptionValue(props, SmtpPassword, None), + getOptionValue[Boolean](props, SmtpSsl, None) + )) } } object SystemSettingsService { + import scala.reflect.ClassTag case class SystemSettings( allowAccountRegistration: Boolean, - gravatar: Boolean + gravatar: Boolean, + notification: Boolean, + smtp: Smtp ) + case class Smtp( + host: String, + port: Option[Int], + user: Option[String], + password: Option[String], + ssl: Option[Boolean]) private val AllowAccountRegistration = "allow_account_registration" private val Gravatar = "gravatar" + private val Notification = "notification" + private val SmtpHost = "smtp.host" + private val SmtpPort = "smtp.port" + private val SmtpUser = "smtp.user" + private val SmtpPassword = "smtp.password" + private val SmtpSsl = "smtp.ssl" - private def getBoolean(props: java.util.Properties, key: String, default: Boolean = false): Boolean = { + private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = { val value = props.getProperty(key) - if(value == null || value.isEmpty){ - default - } else { - value.toBoolean - } + if(value == null || value.isEmpty) default + else convertType(value).asInstanceOf[A] + } + + private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] = { + val value = props.getProperty(key) + if(value == null || value.isEmpty) default + else Some(convertType(value)).asInstanceOf[Option[A]] + } + + private def convertType[A: ClassTag](value: String) = { + val c = implicitly[ClassTag[A]].runtimeClass + if(c == classOf[Boolean]) value.toBoolean + else if(c == classOf[Int]) value.toInt + else value } } diff --git a/src/main/twirl/admin/system.scala.html b/src/main/twirl/admin/system.scala.html index 72c4d16..b2a91ca 100644 --- a/src/main/twirl/admin/system.scala.html +++ b/src/main/twirl/admin/system.scala.html @@ -27,6 +27,49 @@ Gravatar +
+ +
+ +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
@@ -34,4 +77,11 @@
} -} \ No newline at end of file +} + \ No newline at end of file