mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-03 12:05:59 +01:00
Define request attribute keys.
This commit is contained in:
@@ -67,37 +67,37 @@ abstract class ControllerBase extends ScalatraFilter
|
|||||||
|
|
||||||
def ajaxGet(path : String)(action : => Any) : Route =
|
def ajaxGet(path : String)(action : => Any) : Route =
|
||||||
super.get(path){
|
super.get(path){
|
||||||
request.setAttribute("AJAX", "true")
|
request.setAttribute(Keys.Request.Ajax, "true")
|
||||||
action
|
action
|
||||||
}
|
}
|
||||||
|
|
||||||
override def ajaxGet[T](path : String, form : MappingValueType[T])(action : T => Any) : Route =
|
override def ajaxGet[T](path : String, form : MappingValueType[T])(action : T => Any) : Route =
|
||||||
super.ajaxGet(path, form){ form =>
|
super.ajaxGet(path, form){ form =>
|
||||||
request.setAttribute("AJAX", "true")
|
request.setAttribute(Keys.Request.Ajax, "true")
|
||||||
action(form)
|
action(form)
|
||||||
}
|
}
|
||||||
|
|
||||||
def ajaxPost(path : String)(action : => Any) : Route =
|
def ajaxPost(path : String)(action : => Any) : Route =
|
||||||
super.post(path){
|
super.post(path){
|
||||||
request.setAttribute("AJAX", "true")
|
request.setAttribute(Keys.Request.Ajax, "true")
|
||||||
action
|
action
|
||||||
}
|
}
|
||||||
|
|
||||||
override def ajaxPost[T](path : String, form : MappingValueType[T])(action : T => Any) : Route =
|
override def ajaxPost[T](path : String, form : MappingValueType[T])(action : T => Any) : Route =
|
||||||
super.ajaxPost(path, form){ form =>
|
super.ajaxPost(path, form){ form =>
|
||||||
request.setAttribute("AJAX", "true")
|
request.setAttribute(Keys.Request.Ajax, "true")
|
||||||
action(form)
|
action(form)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected def NotFound() =
|
protected def NotFound() =
|
||||||
if(request.hasAttribute("AJAX")){
|
if(request.hasAttribute(Keys.Request.Ajax)){
|
||||||
org.scalatra.NotFound()
|
org.scalatra.NotFound()
|
||||||
} else {
|
} else {
|
||||||
org.scalatra.NotFound(html.error("Not Found"))
|
org.scalatra.NotFound(html.error("Not Found"))
|
||||||
}
|
}
|
||||||
|
|
||||||
protected def Unauthorized()(implicit context: app.Context) =
|
protected def Unauthorized()(implicit context: app.Context) =
|
||||||
if(request.hasAttribute("AJAX")){
|
if(request.hasAttribute(Keys.Request.Ajax)){
|
||||||
org.scalatra.Unauthorized()
|
org.scalatra.Unauthorized()
|
||||||
} else {
|
} else {
|
||||||
if(context.loginAccount.isDefined){
|
if(context.loginAccount.isDefined){
|
||||||
@@ -135,11 +135,13 @@ case class Context(path: String, loginAccount: Option[Account], currentUrl: Stri
|
|||||||
* Cached object are available during a request.
|
* Cached object are available during a request.
|
||||||
*/
|
*/
|
||||||
def cache[A](key: String)(action: => A): A =
|
def cache[A](key: String)(action: => A): A =
|
||||||
Option(request.getAttribute("cache." + key).asInstanceOf[A]).getOrElse {
|
defining(Keys.Request.Cache(key)){ cacheKey =>
|
||||||
|
Option(request.getAttribute(cacheKey).asInstanceOf[A]).getOrElse {
|
||||||
val newObject = action
|
val newObject = action
|
||||||
request.setAttribute("cache." + key, newObject)
|
request.setAttribute(cacheKey, newObject)
|
||||||
newObject
|
newObject
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import service.{SystemSettingsService, AccountService, RepositoryService}
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import util.Implicits._
|
import util.Implicits._
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
|
import util.Keys
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides BASIC Authentication for [[servlet.GitRepositoryServlet]].
|
* Provides BASIC Authentication for [[servlet.GitRepositoryServlet]].
|
||||||
@@ -38,7 +39,7 @@ class BasicAuthenticationFilter extends Filter with RepositoryService with Accou
|
|||||||
case null => requireAuth(response)
|
case null => requireAuth(response)
|
||||||
case auth => decodeAuthHeader(auth).split(":") match {
|
case auth => decodeAuthHeader(auth).split(":") match {
|
||||||
case Array(username, password) if(isWritableUser(username, password, repository)) => {
|
case Array(username, password) if(isWritableUser(username, password, repository)) => {
|
||||||
request.setAttribute("USER_NAME", username)
|
request.setAttribute(Keys.Request.UserName, username)
|
||||||
chain.doFilter(req, wrappedResponse)
|
chain.doFilter(req, wrappedResponse)
|
||||||
}
|
}
|
||||||
case _ => requireAuth(response)
|
case _ => requireAuth(response)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory
|
|||||||
import javax.servlet.ServletConfig
|
import javax.servlet.ServletConfig
|
||||||
import javax.servlet.ServletContext
|
import javax.servlet.ServletContext
|
||||||
import javax.servlet.http.HttpServletRequest
|
import javax.servlet.http.HttpServletRequest
|
||||||
import util.{JGitUtil, Directory}
|
import util.{Keys, JGitUtil, Directory}
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
import util.Implicits._
|
import util.Implicits._
|
||||||
import service._
|
import service._
|
||||||
@@ -55,7 +55,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
|
|||||||
|
|
||||||
override def create(request: HttpServletRequest, db: Repository): ReceivePack = {
|
override def create(request: HttpServletRequest, db: Repository): ReceivePack = {
|
||||||
val receivePack = new ReceivePack(db)
|
val receivePack = new ReceivePack(db)
|
||||||
val userName = request.getAttribute("USER_NAME").asInstanceOf[String]
|
val userName = request.getAttribute(Keys.Request.UserName).asInstanceOf[String]
|
||||||
|
|
||||||
logger.debug("requestURI: " + request.getRequestURI)
|
logger.debug("requestURI: " + request.getRequestURI)
|
||||||
logger.debug("userName:" + userName)
|
logger.debug("userName:" + userName)
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define key strings for request attributes, session attributes or flash attributes..
|
* Define key strings for request attributes, session attributes or flash attributes.
|
||||||
*/
|
*/
|
||||||
object Keys {
|
object Keys {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define session keys.
|
||||||
|
*/
|
||||||
object Session {
|
object Session {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,4 +47,26 @@ object Keys {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define request keys.
|
||||||
|
*/
|
||||||
|
object Request {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request key for the Ajax request flag.
|
||||||
|
*/
|
||||||
|
val Ajax = "AJAX"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request key for the username which is used during Git repository access.
|
||||||
|
*/
|
||||||
|
val UserName = "USER_NAME"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate request key for the request cache.
|
||||||
|
*/
|
||||||
|
def Cache(key: String) = s"cache.${key}"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user