diff --git a/src/main/scala/app/IndexController.scala b/src/main/scala/app/IndexController.scala index 3ffb57a..bb4544b 100644 --- a/src/main/scala/app/IndexController.scala +++ b/src/main/scala/app/IndexController.scala @@ -1,85 +1,85 @@ -package app - -import util._ -import service._ -import jp.sf.amateras.scalatra.forms._ - -class IndexController extends IndexControllerBase - with RepositoryService with ActivityService with AccountService with UsersAuthenticator - -trait IndexControllerBase extends ControllerBase { - self: RepositoryService with ActivityService with AccountService with UsersAuthenticator => - - case class SignInForm(userName: String, password: String) - - val form = mapping( - "userName" -> trim(label("Username", text(required))), - "password" -> trim(label("Password", text(required))) - )(SignInForm.apply) - - get("/"){ - val loginAccount = context.loginAccount - - html.index(getRecentActivities(), - getVisibleRepositories(loginAccount, baseUrl), - loadSystemSettings(), - loginAccount.map{ account => getUserRepositories(account.userName, baseUrl) }.getOrElse(Nil) - ) - } - - get("/signin"){ - val redirect = params.get("redirect") - if(redirect.isDefined && redirect.get.startsWith("/")){ - flash += Keys.Flash.Redirect -> redirect.get - } - html.signin(loadSystemSettings()) - } - - post("/signin", form){ form => - authenticate(loadSystemSettings(), form.userName, form.password) match { - case Some(account) => signin(account) - case None => redirect("/signin") - } - } - - get("/signout"){ - session.invalidate - redirect("/") - } - - /** - * Set account information into HttpSession and redirect. - */ - private def signin(account: model.Account) = { - session.setAttribute(Keys.Session.LoginAccount, account) - updateLastLoginDate(account.userName) - - if(AccountUtil.hasLdapDummyMailAddress(account)) { - redirect("/" + account.userName + "/_edit") - } - - flash.get(Keys.Flash.Redirect).asInstanceOf[Option[String]].map { redirectUrl => - if(redirectUrl.replaceFirst("/$", "") == request.getContextPath){ - redirect("/") - } else { - redirect(redirectUrl) - } - }.getOrElse { - redirect("/") - } - } - - /** - * JSON API for collaborator completion. - * - * TODO Move to other controller? - */ - get("/_user/proposals")(usersOnly { - contentType = formats("json") - org.json4s.jackson.Serialization.write( - Map("options" -> getAllUsers().filter(!_.isGroupAccount).map(_.userName).toArray) - ) - }) - - -} +package app + +import util._ +import service._ +import jp.sf.amateras.scalatra.forms._ + +class IndexController extends IndexControllerBase + with RepositoryService with ActivityService with AccountService with UsersAuthenticator + +trait IndexControllerBase extends ControllerBase { + self: RepositoryService with ActivityService with AccountService with UsersAuthenticator => + + case class SignInForm(userName: String, password: String) + + val form = mapping( + "userName" -> trim(label("Username", text(required))), + "password" -> trim(label("Password", text(required))) + )(SignInForm.apply) + + get("/"){ + val loginAccount = context.loginAccount + + html.index(getRecentActivities(), + getVisibleRepositories(loginAccount, baseUrl), + loadSystemSettings(), + loginAccount.map{ account => getUserRepositories(account.userName, baseUrl) }.getOrElse(Nil) + ) + } + + get("/signin"){ + val redirect = params.get("redirect") + if(redirect.isDefined && redirect.get.startsWith("/")){ + flash += Keys.Flash.Redirect -> redirect.get + } + html.signin(loadSystemSettings()) + } + + post("/signin", form){ form => + authenticate(loadSystemSettings(), form.userName, form.password) match { + case Some(account) => signin(account) + case None => redirect("/signin") + } + } + + get("/signout"){ + session.invalidate + redirect("/") + } + + /** + * Set account information into HttpSession and redirect. + */ + private def signin(account: model.Account) = { + session.setAttribute(Keys.Session.LoginAccount, account) + updateLastLoginDate(account.userName) + + if(AccountUtil.hasLdapDummyMailAddress(account)) { + redirect("/" + account.userName + "/_edit") + } + + flash.get(Keys.Flash.Redirect).asInstanceOf[Option[String]].map { redirectUrl => + if(redirectUrl.replaceFirst("/$", "") == request.getContextPath){ + redirect("/") + } else { + redirect(redirectUrl) + } + }.getOrElse { + redirect("/") + } + } + + /** + * JSON API for collaborator completion. + * + * TODO Move to other controller? + */ + get("/_user/proposals")(usersOnly { + contentType = formats("json") + org.json4s.jackson.Serialization.write( + Map("options" -> getAllUsers().filter(!_.isGroupAccount).map(_.userName).toArray) + ) + }) + + +} diff --git a/src/main/scala/service/SystemSettingsService.scala b/src/main/scala/service/SystemSettingsService.scala index 9c5d300..7bc171b 100644 --- a/src/main/scala/service/SystemSettingsService.scala +++ b/src/main/scala/service/SystemSettingsService.scala @@ -1,183 +1,183 @@ -package service - -import util.Directory._ -import util.ControlUtil._ -import SystemSettingsService._ -import javax.servlet.http.HttpServletRequest - -trait SystemSettingsService { - - def baseUrl(implicit request: HttpServletRequest): String = loadSystemSettings().baseUrl.getOrElse { - defining(request.getRequestURL.toString){ url => - url.substring(0, url.length - (request.getRequestURI.length - request.getContextPath.length)) - } - }.replaceFirst("/$", "") - - def saveSystemSettings(settings: SystemSettings): Unit = { - defining(new java.util.Properties()){ props => - settings.baseUrl.foreach(props.setProperty(BaseURL, _)) - props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString) - props.setProperty(Gravatar, settings.gravatar.toString) - props.setProperty(Notification, settings.notification.toString) - if(settings.notification) { - settings.smtp.foreach { smtp => - props.setProperty(SmtpHost, smtp.host) - smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString)) - smtp.user.foreach(props.setProperty(SmtpUser, _)) - smtp.password.foreach(props.setProperty(SmtpPassword, _)) - smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString)) - smtp.fromAddress.foreach(props.setProperty(SmtpFromAddress, _)) - smtp.fromName.foreach(props.setProperty(SmtpFromName, _)) - } - } - props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString) - if(settings.ldapAuthentication){ - settings.ldap.map { ldap => - props.setProperty(LdapHost, ldap.host) - ldap.port.foreach(x => props.setProperty(LdapPort, x.toString)) - ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x)) - ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x)) - props.setProperty(LdapBaseDN, ldap.baseDN) - props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute) - ldap.additionalFilterCondition.foreach(x => props.setProperty(LdapAdditionalFilterCondition, x)) - ldap.fullNameAttribute.foreach(x => props.setProperty(LdapFullNameAttribute, x)) - props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute) - ldap.disableMailResolve.foreach(x => props.setProperty(LdapDisableMailResolve, x.toString)) - ldap.tls.foreach(x => props.setProperty(LdapTls, x.toString)) - ldap.keystore.foreach(x => props.setProperty(LdapKeystore, x)) - } - } - props.store(new java.io.FileOutputStream(GitBucketConf), null) - } - } - - - def loadSystemSettings(): SystemSettings = { - defining(new java.util.Properties()){ props => - if(GitBucketConf.exists){ - props.load(new java.io.FileInputStream(GitBucketConf)) - } - SystemSettings( - getOptionValue(props, BaseURL, None), - getValue(props, AllowAccountRegistration, false), - getValue(props, Gravatar, true), - getValue(props, Notification, false), - if(getValue(props, Notification, false)){ - Some(Smtp( - getValue(props, SmtpHost, ""), - getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)), - getOptionValue(props, SmtpUser, None), - getOptionValue(props, SmtpPassword, None), - getOptionValue[Boolean](props, SmtpSsl, None), - getOptionValue(props, SmtpFromAddress, None), - getOptionValue(props, SmtpFromName, None))) - } else { - None - }, - getValue(props, LdapAuthentication, false), - if(getValue(props, LdapAuthentication, false)){ - Some(Ldap( - getValue(props, LdapHost, ""), - getOptionValue(props, LdapPort, Some(DefaultLdapPort)), - getOptionValue(props, LdapBindDN, None), - getOptionValue(props, LdapBindPassword, None), - getValue(props, LdapBaseDN, ""), - getValue(props, LdapUserNameAttribute, ""), - getOptionValue(props, LdapAdditionalFilterCondition, None), - getOptionValue(props, LdapFullNameAttribute, None), - getValue(props, LdapMailAddressAttribute, ""), - getOptionValue[Boolean](props, LdapDisableMailResolve, None), - getOptionValue[Boolean](props, LdapTls, None), - getOptionValue(props, LdapKeystore, None))) - } else { - None - } - ) - } - } - -} - -object SystemSettingsService { - import scala.reflect.ClassTag - - case class SystemSettings( - baseUrl: Option[String], - allowAccountRegistration: Boolean, - gravatar: Boolean, - notification: Boolean, - smtp: Option[Smtp], - ldapAuthentication: Boolean, - ldap: Option[Ldap]) - - case class Ldap( - host: String, - port: Option[Int], - bindDN: Option[String], - bindPassword: Option[String], - baseDN: String, - userNameAttribute: String, - additionalFilterCondition: Option[String], - fullNameAttribute: Option[String], - mailAttribute: String, - disableMailResolve: Option[Boolean], - tls: Option[Boolean], - keystore: Option[String]) - - case class Smtp( - host: String, - port: Option[Int], - user: Option[String], - password: Option[String], - ssl: Option[Boolean], - fromAddress: Option[String], - fromName: Option[String]) - - val DefaultSmtpPort = 25 - val DefaultLdapPort = 389 - - private val BaseURL = "base_url" - 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 val SmtpFromAddress = "smtp.from_address" - private val SmtpFromName = "smtp.from_name" - private val LdapAuthentication = "ldap_authentication" - private val LdapHost = "ldap.host" - private val LdapPort = "ldap.port" - private val LdapBindDN = "ldap.bindDN" - private val LdapBindPassword = "ldap.bind_password" - private val LdapBaseDN = "ldap.baseDN" - private val LdapUserNameAttribute = "ldap.username_attribute" - private val LdapAdditionalFilterCondition = "ldap.additional_filter_condition" - private val LdapFullNameAttribute = "ldap.fullname_attribute" - private val LdapMailAddressAttribute = "ldap.mail_attribute" - private val LdapDisableMailResolve = "ldap.disable_mail_resolve" - private val LdapTls = "ldap.tls" - private val LdapKeystore = "ldap.keystore" - - private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = - defining(props.getProperty(key)){ value => - 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] = - defining(props.getProperty(key)){ value => - if(value == null || value.isEmpty) default - else Some(convertType(value)).asInstanceOf[Option[A]] - } - - private def convertType[A: ClassTag](value: String) = - defining(implicitly[ClassTag[A]].runtimeClass){ c => - if(c == classOf[Boolean]) value.toBoolean - else if(c == classOf[Int]) value.toInt - else value - } - -} +package service + +import util.Directory._ +import util.ControlUtil._ +import SystemSettingsService._ +import javax.servlet.http.HttpServletRequest + +trait SystemSettingsService { + + def baseUrl(implicit request: HttpServletRequest): String = loadSystemSettings().baseUrl.getOrElse { + defining(request.getRequestURL.toString){ url => + url.substring(0, url.length - (request.getRequestURI.length - request.getContextPath.length)) + } + }.replaceFirst("/$", "") + + def saveSystemSettings(settings: SystemSettings): Unit = { + defining(new java.util.Properties()){ props => + settings.baseUrl.foreach(props.setProperty(BaseURL, _)) + props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString) + props.setProperty(Gravatar, settings.gravatar.toString) + props.setProperty(Notification, settings.notification.toString) + if(settings.notification) { + settings.smtp.foreach { smtp => + props.setProperty(SmtpHost, smtp.host) + smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString)) + smtp.user.foreach(props.setProperty(SmtpUser, _)) + smtp.password.foreach(props.setProperty(SmtpPassword, _)) + smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString)) + smtp.fromAddress.foreach(props.setProperty(SmtpFromAddress, _)) + smtp.fromName.foreach(props.setProperty(SmtpFromName, _)) + } + } + props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString) + if(settings.ldapAuthentication){ + settings.ldap.map { ldap => + props.setProperty(LdapHost, ldap.host) + ldap.port.foreach(x => props.setProperty(LdapPort, x.toString)) + ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x)) + ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x)) + props.setProperty(LdapBaseDN, ldap.baseDN) + props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute) + ldap.additionalFilterCondition.foreach(x => props.setProperty(LdapAdditionalFilterCondition, x)) + ldap.fullNameAttribute.foreach(x => props.setProperty(LdapFullNameAttribute, x)) + props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute) + ldap.disableMailResolve.foreach(x => props.setProperty(LdapDisableMailResolve, x.toString)) + ldap.tls.foreach(x => props.setProperty(LdapTls, x.toString)) + ldap.keystore.foreach(x => props.setProperty(LdapKeystore, x)) + } + } + props.store(new java.io.FileOutputStream(GitBucketConf), null) + } + } + + + def loadSystemSettings(): SystemSettings = { + defining(new java.util.Properties()){ props => + if(GitBucketConf.exists){ + props.load(new java.io.FileInputStream(GitBucketConf)) + } + SystemSettings( + getOptionValue(props, BaseURL, None), + getValue(props, AllowAccountRegistration, false), + getValue(props, Gravatar, true), + getValue(props, Notification, false), + if(getValue(props, Notification, false)){ + Some(Smtp( + getValue(props, SmtpHost, ""), + getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)), + getOptionValue(props, SmtpUser, None), + getOptionValue(props, SmtpPassword, None), + getOptionValue[Boolean](props, SmtpSsl, None), + getOptionValue(props, SmtpFromAddress, None), + getOptionValue(props, SmtpFromName, None))) + } else { + None + }, + getValue(props, LdapAuthentication, false), + if(getValue(props, LdapAuthentication, false)){ + Some(Ldap( + getValue(props, LdapHost, ""), + getOptionValue(props, LdapPort, Some(DefaultLdapPort)), + getOptionValue(props, LdapBindDN, None), + getOptionValue(props, LdapBindPassword, None), + getValue(props, LdapBaseDN, ""), + getValue(props, LdapUserNameAttribute, ""), + getOptionValue(props, LdapAdditionalFilterCondition, None), + getOptionValue(props, LdapFullNameAttribute, None), + getValue(props, LdapMailAddressAttribute, ""), + getOptionValue[Boolean](props, LdapDisableMailResolve, None), + getOptionValue[Boolean](props, LdapTls, None), + getOptionValue(props, LdapKeystore, None))) + } else { + None + } + ) + } + } + +} + +object SystemSettingsService { + import scala.reflect.ClassTag + + case class SystemSettings( + baseUrl: Option[String], + allowAccountRegistration: Boolean, + gravatar: Boolean, + notification: Boolean, + smtp: Option[Smtp], + ldapAuthentication: Boolean, + ldap: Option[Ldap]) + + case class Ldap( + host: String, + port: Option[Int], + bindDN: Option[String], + bindPassword: Option[String], + baseDN: String, + userNameAttribute: String, + additionalFilterCondition: Option[String], + fullNameAttribute: Option[String], + mailAttribute: String, + disableMailResolve: Option[Boolean], + tls: Option[Boolean], + keystore: Option[String]) + + case class Smtp( + host: String, + port: Option[Int], + user: Option[String], + password: Option[String], + ssl: Option[Boolean], + fromAddress: Option[String], + fromName: Option[String]) + + val DefaultSmtpPort = 25 + val DefaultLdapPort = 389 + + private val BaseURL = "base_url" + 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 val SmtpFromAddress = "smtp.from_address" + private val SmtpFromName = "smtp.from_name" + private val LdapAuthentication = "ldap_authentication" + private val LdapHost = "ldap.host" + private val LdapPort = "ldap.port" + private val LdapBindDN = "ldap.bindDN" + private val LdapBindPassword = "ldap.bind_password" + private val LdapBaseDN = "ldap.baseDN" + private val LdapUserNameAttribute = "ldap.username_attribute" + private val LdapAdditionalFilterCondition = "ldap.additional_filter_condition" + private val LdapFullNameAttribute = "ldap.fullname_attribute" + private val LdapMailAddressAttribute = "ldap.mail_attribute" + private val LdapDisableMailResolve = "ldap.disable_mail_resolve" + private val LdapTls = "ldap.tls" + private val LdapKeystore = "ldap.keystore" + + private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = + defining(props.getProperty(key)){ value => + 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] = + defining(props.getProperty(key)){ value => + if(value == null || value.isEmpty) default + else Some(convertType(value)).asInstanceOf[Option[A]] + } + + private def convertType[A: ClassTag](value: String) = + defining(implicitly[ClassTag[A]].runtimeClass){ c => + if(c == classOf[Boolean]) value.toBoolean + else if(c == classOf[Int]) value.toInt + else value + } + +}