You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
6.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/git"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. )
  16. func RepoAssignment(redirect bool, args ...bool) martini.Handler {
  17. return func(ctx *Context, params martini.Params) {
  18. log.Trace(fmt.Sprint(args))
  19. // valid brachname
  20. var validBranch bool
  21. // display bare quick start if it is a bare repo
  22. var displayBare bool
  23. if len(args) >= 1 {
  24. // Note: argument has wrong value in Go1.3 martini.
  25. // validBranch = args[0]
  26. validBranch = true
  27. }
  28. if len(args) >= 2 {
  29. // displayBare = args[1]
  30. displayBare = true
  31. }
  32. var (
  33. user *models.User
  34. err error
  35. isTrueOwner bool
  36. )
  37. userName := params["username"]
  38. repoName := params["reponame"]
  39. refName := params["branchname"]
  40. // Collaborators who have write access can be seen as owners.
  41. if ctx.IsSigned {
  42. ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.AU_WRITABLE)
  43. if err != nil {
  44. ctx.Handle(500, "RepoAssignment(HasAccess)", err)
  45. return
  46. }
  47. isTrueOwner = ctx.User.LowerName == strings.ToLower(userName)
  48. }
  49. if !isTrueOwner {
  50. user, err = models.GetUserByName(userName)
  51. if err != nil {
  52. if err == models.ErrUserNotExist {
  53. ctx.Handle(404, "RepoAssignment(GetUserByName)", err)
  54. return
  55. } else if redirect {
  56. ctx.Redirect("/")
  57. return
  58. }
  59. ctx.Handle(500, "RepoAssignment(GetUserByName)", err)
  60. return
  61. }
  62. } else {
  63. user = ctx.User
  64. }
  65. if user == nil {
  66. if redirect {
  67. ctx.Redirect("/")
  68. return
  69. }
  70. ctx.Handle(403, "RepoAssignment", errors.New("invliad user account for single repository"))
  71. return
  72. }
  73. ctx.Repo.Owner = user
  74. // get repository
  75. repo, err := models.GetRepositoryByName(user.Id, repoName)
  76. if err != nil {
  77. if err == models.ErrRepoNotExist {
  78. ctx.Handle(404, "RepoAssignment", err)
  79. return
  80. } else if redirect {
  81. ctx.Redirect("/")
  82. return
  83. }
  84. ctx.Handle(500, "RepoAssignment", err)
  85. return
  86. }
  87. // Check access.
  88. if repo.IsPrivate && !ctx.Repo.IsOwner {
  89. if ctx.User == nil {
  90. ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
  91. return
  92. }
  93. hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.AU_READABLE)
  94. if err != nil {
  95. ctx.Handle(500, "RepoAssignment(HasAccess)", err)
  96. return
  97. } else if !hasAccess {
  98. ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
  99. return
  100. }
  101. }
  102. ctx.Repo.HasAccess = true
  103. ctx.Data["HasAccess"] = true
  104. if repo.IsMirror {
  105. ctx.Repo.Mirror, err = models.GetMirror(repo.Id)
  106. if err != nil {
  107. ctx.Handle(500, "RepoAssignment(GetMirror)", err)
  108. return
  109. }
  110. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  111. }
  112. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  113. ctx.Repo.Repository = repo
  114. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  115. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  116. if err != nil {
  117. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  118. return
  119. }
  120. ctx.Repo.GitRepo = gitRepo
  121. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  122. tags, err := ctx.Repo.GitRepo.GetTags()
  123. if err != nil {
  124. ctx.Handle(500, "RepoAssignment(GetTags))", err)
  125. return
  126. }
  127. ctx.Repo.Repository.NumTags = len(tags)
  128. ctx.Data["Title"] = user.Name + "/" + repo.Name
  129. ctx.Data["Repository"] = repo
  130. ctx.Data["Owner"] = user
  131. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  132. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  133. ctx.Data["BranchName"] = ""
  134. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
  135. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
  136. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  137. if ctx.Repo.Repository.IsGoget {
  138. ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", base.AppUrl, user.LowerName, repo.LowerName)
  139. ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", base.Domain, user.LowerName, repo.LowerName)
  140. }
  141. // when repo is bare, not valid branch
  142. if !ctx.Repo.Repository.IsBare && validBranch {
  143. detect:
  144. if len(refName) > 0 {
  145. if gitRepo.IsBranchExist(refName) {
  146. ctx.Repo.IsBranch = true
  147. ctx.Repo.BranchName = refName
  148. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
  149. if err != nil {
  150. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  151. return
  152. }
  153. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  154. } else if gitRepo.IsTagExist(refName) {
  155. ctx.Repo.IsBranch = true
  156. ctx.Repo.BranchName = refName
  157. ctx.Repo.Commit, err = gitRepo.GetCommitOfTag(refName)
  158. if err != nil {
  159. ctx.Handle(404, "RepoAssignment invalid tag", nil)
  160. return
  161. }
  162. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  163. } else if len(refName) == 40 {
  164. ctx.Repo.IsCommit = true
  165. ctx.Repo.CommitId = refName
  166. ctx.Repo.BranchName = refName
  167. ctx.Repo.Commit, err = gitRepo.GetCommit(refName)
  168. if err != nil {
  169. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  170. return
  171. }
  172. } else {
  173. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  174. return
  175. }
  176. } else {
  177. if len(refName) == 0 {
  178. if gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  179. refName = ctx.Repo.Repository.DefaultBranch
  180. } else {
  181. brs, err := gitRepo.GetBranches()
  182. if err != nil {
  183. ctx.Handle(500, "RepoAssignment(GetBranches))", err)
  184. return
  185. }
  186. refName = brs[0]
  187. }
  188. }
  189. goto detect
  190. }
  191. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  192. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  193. log.Debug("Repo.Commit: %v", ctx.Repo.Commit)
  194. }
  195. log.Debug("displayBare: %v; IsBare: %v", displayBare, ctx.Repo.Repository.IsBare)
  196. // repo is bare and display enable
  197. if displayBare && ctx.Repo.Repository.IsBare {
  198. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  199. ctx.HTML(200, "repo/single_bare")
  200. return
  201. }
  202. if ctx.IsSigned {
  203. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  204. }
  205. ctx.Data["BranchName"] = ctx.Repo.BranchName
  206. brs, err := ctx.Repo.GitRepo.GetBranches()
  207. if err != nil {
  208. log.Error("RepoAssignment(GetBranches): %v", err)
  209. }
  210. ctx.Data["Branches"] = brs
  211. ctx.Data["CommitId"] = ctx.Repo.CommitId
  212. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  213. }
  214. }
  215. func RequireOwner() martini.Handler {
  216. return func(ctx *Context) {
  217. if !ctx.Repo.IsOwner {
  218. if !ctx.IsSigned {
  219. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  220. ctx.Redirect("/user/login")
  221. return
  222. }
  223. ctx.Handle(404, ctx.Req.RequestURI, nil)
  224. return
  225. }
  226. }
  227. }