(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

@@ -12,15 +12,21 @@ class SystemSettingsController extends SystemSettingsControllerBase
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 @@ trait SystemSettingsControllerBase extends ControllerBase with FlashMapSupport {
})
post("/admin/system", form)(adminOnly { form =>
saveSystemSettings(SystemSettings(
form.allowAccountRegistration,
form.gravatar
))
saveSystemSettings(form)
flash += "info" -> "System settings has been updated."
redirect("/admin/system")
})

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
}
}

View File

@@ -27,6 +27,49 @@
Gravatar
</label>
</fieldset>
<hr>
<label><strong>Notification email</strong></label>
<fieldset>
<label>
<input type="checkbox" id="notification" name="notification"@if(settings.notification){ checked}/>
Send notifications
</label>
</fieldset>
<div class="form-horizontal">
<div class="control-group">
<label class="control-label" for="smtpHost">SMTP Host</label>
<div class="controls">
<input type="text" id="smtpHost" name="host" value="@settings.smtp.host"/>
<span id="error-host" class="error"></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="smtpPort">SMTP Port</label>
<div class="controls">
<input type="text" id="smtpPort" name="port" class="input-mini" value="@settings.smtp.port"/>
<span id="error-port" class="error"></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="smtpUser">SMTP User</label>
<div class="controls">
<input type="text" id="smtpUser" name="user" value="@settings.smtp.user"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="smtpPassword">SMTP Password</label>
<div class="controls">
<input type="password" id="smtpPassword" name="password" value="@settings.smtp.password"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="ssl"@if(settings.smtp.ssl == Some(true)){ checked}/> Enable SSL
</label>
</div>
</div>
</div>
</div>
</div>
<fieldset>
@@ -35,3 +78,10 @@
</form>
}
}
<script>
$(function(){
$('#notification').change(function(){
$('.form-horizontal input').prop('disabled', !$(this).prop('checked'));
}).change();
});
</script>