Toggle Gravatar support at the system settings.

This commit is contained in:
takezoe
2013-07-25 03:00:46 +09:00
parent 34e2663492
commit b61836adf7
5 changed files with 84 additions and 50 deletions

View File

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

View File

@@ -1,6 +1,7 @@
package service package service
import model._ import model._
import service.SystemSettingsService.SystemSettings
/** /**
* This service is used for a view helper mainly. * This service is used for a view helper mainly.
@@ -10,6 +11,11 @@ import model._
*/ */
trait RequestCache { trait RequestCache {
def getSystemSettings()(implicit context: app.Context): SystemSettings =
context.cache("system_settings"){
new SystemSettingsService {}.loadSystemSettings()
}
def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = { def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = {
context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){ context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){
new IssuesService {}.getIssue(userName, repositoryName, issueId) new IssuesService {}.getIssue(userName, repositoryName, issueId)

View File

@@ -8,6 +8,7 @@ trait SystemSettingsService {
def saveSystemSettings(settings: SystemSettings): Unit = { def saveSystemSettings(settings: SystemSettings): Unit = {
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.store(new java.io.FileOutputStream(GitBucketConf), null) props.store(new java.io.FileOutputStream(GitBucketConf), null)
} }
@@ -17,16 +18,22 @@ trait SystemSettingsService {
if(GitBucketConf.exists){ if(GitBucketConf.exists){
props.load(new java.io.FileInputStream(GitBucketConf)) props.load(new java.io.FileInputStream(GitBucketConf))
} }
SystemSettings(getBoolean(props, "allow_account_registration")) SystemSettings(
getBoolean(props, AllowAccountRegistration),
getBoolean(props, Gravatar, true))
} }
} }
object SystemSettingsService { object SystemSettingsService {
case class SystemSettings(allowAccountRegistration: Boolean) case class SystemSettings(
allowAccountRegistration: Boolean,
gravatar: Boolean
)
private val AllowAccountRegistration = "allow_account_registration" private val AllowAccountRegistration = "allow_account_registration"
private val Gravatar = "gravatar"
private def getBoolean(props: java.util.Properties, key: String, default: Boolean = false): Boolean = { private def getBoolean(props: java.util.Properties, key: String, default: Boolean = false): Boolean = {
val value = props.getProperty(key) val value = props.getProperty(key)

View File

@@ -12,7 +12,9 @@ trait AvatarImageProvider { self: RequestCache =>
*/ */
protected def getAvatarImageHtml(userName: String, size: Int, protected def getAvatarImageHtml(userName: String, size: Int,
mailAddress: String = "", tooltip: Boolean = false)(implicit context: app.Context): Html = { mailAddress: String = "", tooltip: Boolean = false)(implicit context: app.Context): Html = {
val src = getAccountByUserName(userName).collect { case account if(account.image.isEmpty) =>
val src = if(getSystemSettings().gravatar){
getAccountByUserName(userName).collect { case account if(account.image.isEmpty && !account.isGroupAccount) =>
s"""http://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress)}?s=${size}""" s"""http://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress)}?s=${size}"""
} getOrElse { } getOrElse {
if(mailAddress.nonEmpty){ if(mailAddress.nonEmpty){
@@ -21,6 +23,10 @@ trait AvatarImageProvider { self: RequestCache =>
s"""${context.path}/${userName}/_avatar""" s"""${context.path}/${userName}/_avatar"""
} }
} }
} else {
s"""${context.path}/${userName}/_avatar"""
}
if(tooltip){ if(tooltip){
Html(s"""<img src="${src}" class="avatar" style="width: ${size}px; height: ${size}px;" data-toggle="tooltip" title="${userName}"/>""") Html(s"""<img src="${src}" class="avatar" style="width: ${size}px; height: ${size}px;" data-toggle="tooltip" title="${userName}"/>""")
} else { } else {

View File

@@ -19,6 +19,14 @@
<strong>Deny</strong> - Only administrators can create account. <strong>Deny</strong> - Only administrators can create account.
</label> </label>
</fieldset> </fieldset>
<hr>
<label><strong>Services</strong></label>
<fieldset>
<label>
<input type="checkbox" name="gravatar"@if(settings.gravatar){ checked}/>
Gravatar
</label>
</fieldset>
</div> </div>
</div> </div>
<fieldset> <fieldset>