mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
Add RichRequest which extends HttpServletRequest.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import util.{FileUtil}
|
||||
import util.FileUtil
|
||||
import util.ControlUtil._
|
||||
import org.scalatra._
|
||||
import org.scalatra.servlet.{MultipartConfig, FileUploadSupport}
|
||||
import org.apache.commons.io.FileUtils
|
||||
@@ -18,8 +19,7 @@ class FileUploadController extends ScalatraServlet
|
||||
|
||||
post("/image"){
|
||||
fileParams.get("file") match {
|
||||
case Some(file) if(FileUtil.isImage(file.name)) => {
|
||||
val fileId = generateFileId
|
||||
case Some(file) if(FileUtil.isImage(file.name)) => defining(generateFileId){ fileId =>
|
||||
FileUtils.writeByteArrayToFile(getTemporaryFile(fileId), file.get)
|
||||
session += "upload_" + fileId -> file.name
|
||||
Ok(fileId)
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.scalatra.FlashMapSupport
|
||||
import service.WebHookService.WebHookPayload
|
||||
import util.JGitUtil.CommitInfo
|
||||
import util.ControlUtil._
|
||||
import util.Implicits._
|
||||
import org.eclipse.jgit.api.Git
|
||||
|
||||
class RepositorySettingsController extends RepositorySettingsControllerBase
|
||||
@@ -181,7 +182,7 @@ trait RepositorySettingsControllerBase extends ControllerBase with FlashMapSuppo
|
||||
*/
|
||||
private def webHook: Constraint = new Constraint(){
|
||||
override def validate(name: String, value: String): Option[String] =
|
||||
defining(request.getRequestURI.split("/")){ paths =>
|
||||
defining(request.paths){ paths =>
|
||||
getWebHookURLs(paths(1), paths(2)).map(_.url).find(_ == value).map(_ => "URL had been registered already.")
|
||||
}
|
||||
}
|
||||
@@ -191,7 +192,7 @@ trait RepositorySettingsControllerBase extends ControllerBase with FlashMapSuppo
|
||||
*/
|
||||
private def collaborator: Constraint = new Constraint(){
|
||||
override def validate(name: String, value: String): Option[String] =
|
||||
defining(request.getRequestURI.split("/")){ paths =>
|
||||
defining(request.paths){ paths =>
|
||||
getAccountByUserName(value) match {
|
||||
case None => Some("User does not exist.")
|
||||
case Some(x) if(x.userName == paths(1) || getCollaborators(paths(1), paths(2)).contains(x.userName))
|
||||
|
||||
@@ -4,6 +4,8 @@ import javax.servlet._
|
||||
import javax.servlet.http._
|
||||
import service.{SystemSettingsService, AccountService, RepositoryService}
|
||||
import org.slf4j.LoggerFactory
|
||||
import util.Implicits._
|
||||
import util.ControlUtil._
|
||||
|
||||
/**
|
||||
* Provides BASIC Authentication for [[servlet.GitRepositoryServlet]].
|
||||
@@ -25,10 +27,7 @@ class BasicAuthenticationFilter extends Filter with RepositoryService with Accou
|
||||
}
|
||||
|
||||
try {
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
val repositoryOwner = paths(2)
|
||||
val repositoryName = paths(3).replaceFirst("\\.git$", "")
|
||||
|
||||
defining(request.paths.toSeq){ case (repositoryOwner :: repositoryName :: _) =>
|
||||
getRepository(repositoryOwner, repositoryName.replaceFirst("\\.wiki", ""), "") match {
|
||||
case Some(repository) => {
|
||||
if(!request.getRequestURI.endsWith("/git-receive-pack") &&
|
||||
@@ -49,6 +48,7 @@ class BasicAuthenticationFilter extends Filter with RepositoryService with Accou
|
||||
}
|
||||
case None => response.sendError(HttpServletResponse.SC_NOT_FOUND)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
case ex: Exception => {
|
||||
logger.error("error", ex)
|
||||
|
||||
@@ -11,6 +11,7 @@ import javax.servlet.ServletContext
|
||||
import javax.servlet.http.HttpServletRequest
|
||||
import util.{JGitUtil, Directory}
|
||||
import util.ControlUtil._
|
||||
import util.Implicits._
|
||||
import service._
|
||||
import WebHookService._
|
||||
import org.eclipse.jgit.api.Git
|
||||
@@ -59,7 +60,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
|
||||
logger.debug("requestURI: " + request.getRequestURI)
|
||||
logger.debug("userName:" + userName)
|
||||
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
defining(request.paths){ paths =>
|
||||
val owner = paths(2)
|
||||
val repository = paths(3).replaceFirst("\\.git$", "")
|
||||
val baseURL = request.getRequestURL.toString.replaceFirst("/git/.*", "")
|
||||
@@ -71,6 +72,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
|
||||
receivePack
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package util
|
||||
import app.ControllerBase
|
||||
import service._
|
||||
import RepositoryService.RepositoryInfo
|
||||
import util.Implicits._
|
||||
import util.ControlUtil._
|
||||
|
||||
/**
|
||||
* Allows only oneself and administrators.
|
||||
@@ -13,7 +15,7 @@ trait OneselfAuthenticator { self: ControllerBase =>
|
||||
|
||||
private def authenticate(action: => Any) = {
|
||||
{
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
defining(request.paths){ paths =>
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.isAdmin) => action
|
||||
case Some(x) if(paths(1) == x.userName) => action
|
||||
@@ -22,6 +24,7 @@ trait OneselfAuthenticator { self: ControllerBase =>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only the repository owner and administrators.
|
||||
@@ -32,7 +35,7 @@ trait OwnerAuthenticator { self: ControllerBase with RepositoryService =>
|
||||
|
||||
private def authenticate(action: (RepositoryInfo) => Any) = {
|
||||
{
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
defining(request.paths){ paths =>
|
||||
getRepository(paths(1), paths(2), baseUrl).map { repository =>
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.isAdmin) => action(repository)
|
||||
@@ -43,6 +46,7 @@ trait OwnerAuthenticator { self: ControllerBase with RepositoryService =>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only signed in users.
|
||||
@@ -87,7 +91,7 @@ trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService =
|
||||
|
||||
private def authenticate(action: (RepositoryInfo) => Any) = {
|
||||
{
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
defining(request.paths){ paths =>
|
||||
getRepository(paths(1), paths(2), baseUrl).map { repository =>
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.isAdmin) => action(repository)
|
||||
@@ -99,6 +103,7 @@ trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService =
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only the repository owner and administrators.
|
||||
@@ -109,7 +114,7 @@ trait ReferrerAuthenticator { self: ControllerBase with RepositoryService =>
|
||||
|
||||
private def authenticate(action: (RepositoryInfo) => Any) = {
|
||||
{
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
defining(request.paths){ paths =>
|
||||
getRepository(paths(1), paths(2), baseUrl).map { repository =>
|
||||
if(!repository.repository.isPrivate){
|
||||
action(repository)
|
||||
@@ -125,6 +130,7 @@ trait ReferrerAuthenticator { self: ControllerBase with RepositoryService =>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows only signed in users which can access the repository.
|
||||
@@ -135,7 +141,7 @@ trait ReadableUsersAuthenticator { self: ControllerBase with RepositoryService =
|
||||
|
||||
private def authenticate(action: (RepositoryInfo) => Any) = {
|
||||
{
|
||||
val paths = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
defining(request.paths){ paths =>
|
||||
getRepository(paths(1), paths(2), baseUrl).map { repository =>
|
||||
context.loginAccount match {
|
||||
case Some(x) if(x.isAdmin) => action(repository)
|
||||
@@ -148,3 +154,4 @@ trait ReadableUsersAuthenticator { self: ControllerBase with RepositoryService =
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package util
|
||||
|
||||
import scala.util.matching.Regex
|
||||
import javax.servlet.http.HttpServletRequest
|
||||
|
||||
/**
|
||||
* Provides some usable implicit conversions.
|
||||
@@ -42,4 +43,8 @@ object Implicits {
|
||||
}
|
||||
}
|
||||
|
||||
implicit class RichRequest(request: HttpServletRequest){
|
||||
def paths: Array[String] = request.getRequestURI.substring(request.getContextPath.length).split("/")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user