(refs #10) Add notification email form.

This commit is contained in:
shimamoto
2013-08-08 20:58:57 +09:00
parent a0a284ad26
commit 6d453ea80b
3 changed files with 115 additions and 21 deletions

View File

@@ -9,6 +9,14 @@ trait SystemSettingsService {
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 @@ trait SystemSettingsService {
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
}
}