mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
(refs #10) Add notification email form.
This commit is contained in:
@@ -12,15 +12,21 @@ class SystemSettingsController extends SystemSettingsControllerBase
|
|||||||
trait SystemSettingsControllerBase extends ControllerBase with FlashMapSupport {
|
trait SystemSettingsControllerBase extends ControllerBase with FlashMapSupport {
|
||||||
self: SystemSettingsService with AccountService with AdminAuthenticator =>
|
self: SystemSettingsService with AccountService with AdminAuthenticator =>
|
||||||
|
|
||||||
private case class SystemSettingsForm(
|
|
||||||
allowAccountRegistration: Boolean,
|
|
||||||
gravatar: Boolean
|
|
||||||
)
|
|
||||||
|
|
||||||
private val form = mapping(
|
private val form = mapping(
|
||||||
"allowAccountRegistration" -> trim(label("Account registration", boolean())),
|
"allowAccountRegistration" -> trim(label("Account registration", boolean())),
|
||||||
"gravatar" -> trim(label("Gravatar", boolean()))
|
"gravatar" -> trim(label("Gravatar", boolean())),
|
||||||
)(SystemSettingsForm.apply)
|
"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 {
|
get("/admin/system")(adminOnly {
|
||||||
@@ -28,10 +34,7 @@ trait SystemSettingsControllerBase extends ControllerBase with FlashMapSupport {
|
|||||||
})
|
})
|
||||||
|
|
||||||
post("/admin/system", form)(adminOnly { form =>
|
post("/admin/system", form)(adminOnly { form =>
|
||||||
saveSystemSettings(SystemSettings(
|
saveSystemSettings(form)
|
||||||
form.allowAccountRegistration,
|
|
||||||
form.gravatar
|
|
||||||
))
|
|
||||||
flash += "info" -> "System settings has been updated."
|
flash += "info" -> "System settings has been updated."
|
||||||
redirect("/admin/system")
|
redirect("/admin/system")
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,6 +9,14 @@ trait SystemSettingsService {
|
|||||||
val props = new java.util.Properties()
|
val props = new java.util.Properties()
|
||||||
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
|
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
|
||||||
props.setProperty(Gravatar, settings.gravatar.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)
|
props.store(new java.io.FileOutputStream(GitBucketConf), null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,29 +27,62 @@ trait SystemSettingsService {
|
|||||||
props.load(new java.io.FileInputStream(GitBucketConf))
|
props.load(new java.io.FileInputStream(GitBucketConf))
|
||||||
}
|
}
|
||||||
SystemSettings(
|
SystemSettings(
|
||||||
getBoolean(props, AllowAccountRegistration),
|
getValue(props, AllowAccountRegistration, false),
|
||||||
getBoolean(props, Gravatar, true))
|
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 {
|
object SystemSettingsService {
|
||||||
|
import scala.reflect.ClassTag
|
||||||
|
|
||||||
case class SystemSettings(
|
case class SystemSettings(
|
||||||
allowAccountRegistration: Boolean,
|
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 AllowAccountRegistration = "allow_account_registration"
|
||||||
private val Gravatar = "gravatar"
|
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)
|
val value = props.getProperty(key)
|
||||||
if(value == null || value.isEmpty){
|
if(value == null || value.isEmpty) default
|
||||||
default
|
else convertType(value).asInstanceOf[A]
|
||||||
} else {
|
|
||||||
value.toBoolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,49 @@
|
|||||||
Gravatar
|
Gravatar
|
||||||
</label>
|
</label>
|
||||||
</fieldset>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
@@ -35,3 +78,10 @@
|
|||||||
</form>
|
</form>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
<script>
|
||||||
|
$(function(){
|
||||||
|
$('#notification').change(function(){
|
||||||
|
$('.form-horizontal input').prop('disabled', !$(this).prop('checked'));
|
||||||
|
}).change();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
Reference in New Issue
Block a user