diff --git a/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala b/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala index 47654e8..61089f0 100644 --- a/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala +++ b/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala @@ -3,6 +3,13 @@ import gitbucket.core.model.Session import gitbucket.core.service.SystemSettingsService.SystemSettings +/** + * Define the Git repository routing. + * + * @param urlPattern the regular expression which matches the repository path (e.g. "gist/(.+?)/(.+?)\\.git") + * @param localPath the string to assemble local file path of repository (e.g. "gist/$1/$2") + * @param filter the filter for request to the Git repository which is defined by this routing + */ case class GitRepositoryRouting(urlPattern: String, localPath: String, filter: GitRepositoryFilter){ def this(urlPattern: String, localPath: String) = { @@ -14,7 +21,22 @@ } +/** + * Filters request to plug-in served repository. This is used to provide authentication mainly. + */ trait GitRepositoryFilter { + + /** + * Filters request to Git repository. If this method returns true then request is accepted. + * + * @param path the repository path which starts with '/' + * @param userName the authenticated user name or None + * @param settings the system settings + * @param isUpdating true if update request, otherwise false + * @param session the database session + * @return true if allow accessing to repository, otherwise false. + */ def filter(path: String, userName: Option[String], settings: SystemSettings, isUpdating: Boolean) (implicit session: Session): Boolean + } \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/ssh/GitCommand.scala b/src/main/scala/gitbucket/core/ssh/GitCommand.scala index bca00ac..43502dd 100644 --- a/src/main/scala/gitbucket/core/ssh/GitCommand.scala +++ b/src/main/scala/gitbucket/core/ssh/GitCommand.scala @@ -104,7 +104,7 @@ } class DefaultGitReceivePack(owner: String, repoName: String, baseUrl: String) extends DefaultGitCommand(owner, repoName) - with SystemSettingsService with RepositoryService with AccountService { + with RepositoryService with AccountService { override protected def runTask(user: String)(implicit session: Session): Unit = { getRepository(owner, repoName.replaceFirst("\\.wiki\\Z", ""), baseUrl).foreach { repositoryInfo => @@ -124,28 +124,32 @@ } } -class PluginGitUploadPack(repoName: String, baseUrl: String, routing: GitRepositoryRouting) extends GitCommand { +class PluginGitUploadPack(repoName: String, baseUrl: String, routing: GitRepositoryRouting) extends GitCommand + with SystemSettingsService { override protected def runTask(user: String)(implicit session: Session): Unit = { - // TODO filter?? - val path = routing.urlPattern.r.replaceFirstIn(repoName, routing.localPath) - using(Git.open(new File(Directory.GitBucketHome, path))){ git => - val repository = git.getRepository - val upload = new UploadPack(repository) - upload.upload(in, out, err) + if(routing.filter.filter("/" + repoName, Some(user), loadSystemSettings(), false)){ + val path = routing.urlPattern.r.replaceFirstIn(repoName, routing.localPath) + using(Git.open(new File(Directory.GitBucketHome, path))){ git => + val repository = git.getRepository + val upload = new UploadPack(repository) + upload.upload(in, out, err) + } } } } -class PluginGitReceivePack(repoName: String, baseUrl: String, routing: GitRepositoryRouting) extends GitCommand { +class PluginGitReceivePack(repoName: String, baseUrl: String, routing: GitRepositoryRouting) extends GitCommand + with SystemSettingsService { override protected def runTask(user: String)(implicit session: Session): Unit = { - // TODO filter?? - val path = routing.urlPattern.r.replaceFirstIn(repoName, routing.localPath) - using(Git.open(new File(Directory.GitBucketHome, path))){ git => - val repository = git.getRepository - val receive = new ReceivePack(repository) - receive.receive(in, out, err) + if(routing.filter.filter("/" + repoName, Some(user), loadSystemSettings(), true)){ + val path = routing.urlPattern.r.replaceFirstIn(repoName, routing.localPath) + using(Git.open(new File(Directory.GitBucketHome, path))){ git => + val repository = git.getRepository + val receive = new ReceivePack(repository) + receive.receive(in, out, err) + } } } }