mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 22:45:51 +01:00
Provides Slick Session to plug-ins via ThreadLocal
This commit is contained in:
11
src/main/scala/plugin/Sessions.scala
Normal file
11
src/main/scala/plugin/Sessions.scala
Normal file
@@ -0,0 +1,11 @@
|
||||
package plugin
|
||||
|
||||
import slick.jdbc.JdbcBackend.Session
|
||||
|
||||
/**
|
||||
* Provides Slick Session to Plug-ins.
|
||||
*/
|
||||
object Sessions {
|
||||
val sessions = new ThreadLocal[Session]
|
||||
implicit def session: Session = sessions.get()
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import service.SystemSettingsService
|
||||
import util.Keys
|
||||
import app.Context
|
||||
import plugin.Results._
|
||||
import plugin.Sessions._
|
||||
|
||||
class PluginActionFilter extends Filter with SystemSettingsService {
|
||||
|
||||
@@ -27,7 +28,8 @@ class PluginActionFilter extends Filter with SystemSettingsService {
|
||||
// Create Context
|
||||
val loginAccount = req.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
|
||||
implicit val context = Context(loadSystemSettings(), Option(loginAccount), req)
|
||||
|
||||
sessions.set(Database.getSession(req))
|
||||
try {
|
||||
// Invoke global action
|
||||
action(req, res, context) match {
|
||||
// TODO to be type classes?
|
||||
@@ -46,6 +48,9 @@ class PluginActionFilter extends Filter with SystemSettingsService {
|
||||
res.getWriter.write(x.body)
|
||||
res.getWriter.flush()
|
||||
}
|
||||
} finally {
|
||||
sessions.remove()
|
||||
}
|
||||
}.getOrElse {
|
||||
chain.doFilter(req, res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user