mirror of
				https://github.com/gitbucket/gitbucket.git
				synced 2025-10-31 10:36:05 +01:00 
			
		
		
		
	Add option to keep session in the local dev mode (#3205)
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -4,6 +4,7 @@ | |||||||
| .ensime_cache | .ensime_cache | ||||||
| .DS_Store | .DS_Store | ||||||
| .java-version | .java-version | ||||||
|  | .tmp | ||||||
|  |  | ||||||
| # sbt specific | # sbt specific | ||||||
| dist/* | dist/* | ||||||
|   | |||||||
| @@ -90,7 +90,6 @@ scalacOptions := Seq( | |||||||
|   "-Wconf:cat=unused&src=twirl/.*:s,cat=unused&src=scala/gitbucket/core/model/[^/]+\\.scala:s" |   "-Wconf:cat=unused&src=twirl/.*:s,cat=unused&src=scala/gitbucket/core/model/[^/]+\\.scala:s" | ||||||
| ) | ) | ||||||
| compile / javacOptions ++= Seq("-target", "8", "-source", "8") | compile / javacOptions ++= Seq("-target", "8", "-source", "8") | ||||||
| Jetty / javaOptions += "-Dlogback.configurationFile=/logback-dev.xml" |  | ||||||
|  |  | ||||||
| // Test settings | // Test settings | ||||||
| //testOptions in Test += Tests.Argument("-l", "ExternalDBTest") | //testOptions in Test += Tests.Argument("-l", "ExternalDBTest") | ||||||
| @@ -286,7 +285,9 @@ Test / testOptions ++= { | |||||||
| } | } | ||||||
|  |  | ||||||
| Jetty / javaOptions ++= Seq( | Jetty / javaOptions ++= Seq( | ||||||
|  |   "-Dlogback.configurationFile=/logback-dev.xml", | ||||||
|   "-Xdebug", |   "-Xdebug", | ||||||
|   "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000", |   "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000", | ||||||
|   "-Dorg.eclipse.jetty.annotations.AnnotationParser.LEVEL=OFF" |   "-Dorg.eclipse.jetty.annotations.AnnotationParser.LEVEL=OFF", | ||||||
|  |   //"-Ddev-features=keep-session" | ||||||
| ) | ) | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| package gitbucket.core.controller | package gitbucket.core.controller | ||||||
|  |  | ||||||
| import java.io.{File, FileInputStream} | import java.io.{File, FileInputStream, FileOutputStream} | ||||||
|  |  | ||||||
| import gitbucket.core.api.{ApiError, JsonFormat} | import gitbucket.core.api.{ApiError, JsonFormat} | ||||||
| import gitbucket.core.model.Account | import gitbucket.core.model.Account | ||||||
| import gitbucket.core.service.{AccountService, RepositoryService, SystemSettingsService} | import gitbucket.core.service.{AccountService, RepositoryService, SystemSettingsService} | ||||||
| @@ -14,9 +13,9 @@ import org.scalatra._ | |||||||
| import org.scalatra.i18n._ | import org.scalatra.i18n._ | ||||||
| import org.scalatra.json._ | import org.scalatra.json._ | ||||||
| import org.scalatra.forms._ | import org.scalatra.forms._ | ||||||
|  |  | ||||||
| import javax.servlet.http.{HttpServletRequest, HttpServletResponse} | import javax.servlet.http.{HttpServletRequest, HttpServletResponse} | ||||||
| import javax.servlet.{FilterChain, ServletRequest, ServletResponse} | import javax.servlet.{FilterChain, ServletRequest, ServletResponse} | ||||||
|  |  | ||||||
| import is.tagomor.woothee.Classifier | import is.tagomor.woothee.Classifier | ||||||
|  |  | ||||||
| import scala.util.Try | import scala.util.Try | ||||||
| @@ -29,6 +28,9 @@ import org.eclipse.jgit.treewalk._ | |||||||
| import org.apache.commons.io.IOUtils | import org.apache.commons.io.IOUtils | ||||||
| import org.slf4j.LoggerFactory | import org.slf4j.LoggerFactory | ||||||
| import org.json4s.Formats | import org.json4s.Formats | ||||||
|  | import org.json4s.jackson.Serialization | ||||||
|  |  | ||||||
|  | import java.nio.charset.StandardCharsets | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Provides generic features for controller implementations. |  * Provides generic features for controller implementations. | ||||||
| @@ -93,8 +95,16 @@ abstract class ControllerBase | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private def LoginAccount: Option[Account] = |   private def LoginAccount: Option[Account] = { | ||||||
|     request.getAs[Account](Keys.Session.LoginAccount).orElse(session.getAs[Account](Keys.Session.LoginAccount)) |     request | ||||||
|  |       .getAs[Account](Keys.Session.LoginAccount) | ||||||
|  |       .orElse(session.getAs[Account](Keys.Session.LoginAccount)) | ||||||
|  |       .orElse { | ||||||
|  |         if (isDevFeatureEnabled(DevFeatures.KeepSession)) { | ||||||
|  |           getLoginAccountFromLocalFile() | ||||||
|  |         } else None | ||||||
|  |       } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   def ajaxGet(path: String)(action: => Any): Route = |   def ajaxGet(path: String)(action: => Any): Route = | ||||||
|     super.get(path) { |     super.get(path) { | ||||||
| @@ -277,6 +287,47 @@ abstract class ControllerBase | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   protected object DevFeatures { | ||||||
|  |     val KeepSession = "keep-session" | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private val loginAccountFile = new File(".tmp/login_account.json") | ||||||
|  |  | ||||||
|  |   protected def isDevFeatureEnabled(feature: String): Boolean = { | ||||||
|  |     Option(System.getProperty("dev-features")).getOrElse("").split(",").map(_.trim).contains(feature) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   protected def getLoginAccountFromLocalFile(): Option[Account] = { | ||||||
|  |     if (isDevFeatureEnabled(DevFeatures.KeepSession)) { | ||||||
|  |       if (loginAccountFile.exists()) { | ||||||
|  |         Using.resource(new FileInputStream(loginAccountFile)) { in => | ||||||
|  |           val json = IOUtils.toString(in, StandardCharsets.UTF_8) | ||||||
|  |           val account = parse(json).extract[Account] | ||||||
|  |           session.setAttribute(Keys.Session.LoginAccount, account) | ||||||
|  |           Some(parse(json).extract[Account]) | ||||||
|  |         } | ||||||
|  |       } else None | ||||||
|  |  | ||||||
|  |     } else None | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   protected def saveLoginAccountToLocalFile(account: Account): Unit = { | ||||||
|  |     if (isDevFeatureEnabled(DevFeatures.KeepSession)) { | ||||||
|  |       if (!loginAccountFile.getParentFile.exists()) { | ||||||
|  |         loginAccountFile.getParentFile.mkdirs() | ||||||
|  |       } | ||||||
|  |       Using.resource(new FileOutputStream(loginAccountFile)) { in => | ||||||
|  |         in.write(Serialization.write(account).getBytes(StandardCharsets.UTF_8)) | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   protected def deleteLoginAccountFromLocalFile(): Unit = { | ||||||
|  |     if (isDevFeatureEnabled(DevFeatures.KeepSession)) { | ||||||
|  |       loginAccountFile.delete() | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|   | |||||||
| @@ -156,6 +156,9 @@ trait IndexControllerBase extends ControllerBase { | |||||||
|  |  | ||||||
|   get("/signout") { |   get("/signout") { | ||||||
|     session.invalidate |     session.invalidate | ||||||
|  |     if (isDevFeatureEnabled(DevFeatures.KeepSession)) { | ||||||
|  |       deleteLoginAccountFromLocalFile() | ||||||
|  |     } | ||||||
|     redirect("/") |     redirect("/") | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -178,6 +181,9 @@ trait IndexControllerBase extends ControllerBase { | |||||||
|    */ |    */ | ||||||
|   private def signin(account: Account, redirectUrl: String = "/") = { |   private def signin(account: Account, redirectUrl: String = "/") = { | ||||||
|     session.setAttribute(Keys.Session.LoginAccount, account) |     session.setAttribute(Keys.Session.LoginAccount, account) | ||||||
|  |     if (isDevFeatureEnabled(DevFeatures.KeepSession)) { | ||||||
|  |       saveLoginAccountToLocalFile(account) | ||||||
|  |     } | ||||||
|     updateLastLoginDate(account.userName) |     updateLastLoginDate(account.userName) | ||||||
|  |  | ||||||
|     if (LDAPUtil.isDummyMailAddress(account)) { |     if (LDAPUtil.isDummyMailAddress(account)) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user