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.

268 lines
7.1 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
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/log"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. func RepoAssignment(redirect bool, args ...bool) martini.Handler {
  17. return func(ctx *Context, params martini.Params) {
  18. // valid brachname
  19. var validBranch bool
  20. // display bare quick start if it is a bare repo
  21. var displayBare bool
  22. if len(args) >= 1 {
  23. validBranch = args[0]
  24. }
  25. if len(args) >= 2 {
  26. displayBare = args[1]
  27. }
  28. var (
  29. user *models.User
  30. err error
  31. isTrueOwner bool
  32. )
  33. userName := params["username"]
  34. repoName := params["reponame"]
  35. refName := params["branchname"]
  36. // Collaborators who have write access can be seen as owners.
  37. if ctx.IsSigned {
  38. ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.AU_WRITABLE)
  39. if err != nil {
  40. ctx.Handle(500, "RepoAssignment(HasAccess)", err)
  41. return
  42. }
  43. isTrueOwner = ctx.User.LowerName == strings.ToLower(userName)
  44. }
  45. if !isTrueOwner {
  46. user, err = models.GetUserByName(userName)
  47. if err != nil {
  48. if err == models.ErrUserNotExist {
  49. ctx.Handle(404, "RepoAssignment(GetUserByName)", err)
  50. return
  51. } else if redirect {
  52. ctx.Redirect("/")
  53. return
  54. }
  55. ctx.Handle(500, "RepoAssignment(GetUserByName)", err)
  56. return
  57. }
  58. } else {
  59. user = ctx.User
  60. }
  61. if user == nil {
  62. if redirect {
  63. ctx.Redirect("/")
  64. return
  65. }
  66. ctx.Handle(403, "RepoAssignment", errors.New("invliad user account for single repository"))
  67. return
  68. }
  69. ctx.Repo.Owner = user
  70. // get repository
  71. repo, err := models.GetRepositoryByName(user.Id, repoName)
  72. if err != nil {
  73. if err == models.ErrRepoNotExist {
  74. ctx.Handle(404, "RepoAssignment", err)
  75. return
  76. } else if redirect {
  77. ctx.Redirect("/")
  78. return
  79. }
  80. ctx.Handle(500, "RepoAssignment", err)
  81. return
  82. }
  83. // Check if the mirror repository owner(mirror repository doesn't have access).
  84. if ctx.IsSigned && !ctx.Repo.IsOwner && repo.OwnerId == ctx.User.Id {
  85. ctx.Repo.IsOwner = true
  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. repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
  114. ctx.Repo.Repository = repo
  115. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  116. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  117. if err != nil {
  118. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  119. return
  120. }
  121. ctx.Repo.GitRepo = gitRepo
  122. ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
  123. tags, err := ctx.Repo.GitRepo.GetTags()
  124. if err != nil {
  125. ctx.Handle(500, "RepoAssignment(GetTags))", err)
  126. return
  127. }
  128. ctx.Repo.Repository.NumTags = len(tags)
  129. ctx.Data["Title"] = user.Name + "/" + repo.Name
  130. ctx.Data["Repository"] = repo
  131. ctx.Data["Owner"] = user
  132. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  133. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  134. ctx.Data["BranchName"] = ""
  135. if setting.SshPort != 22 {
  136. ctx.Repo.CloneLink.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", setting.RunUser, setting.Domain, user.LowerName, repo.LowerName)
  137. } else {
  138. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.Domain, user.LowerName, repo.LowerName)
  139. }
  140. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, user.LowerName, repo.LowerName)
  141. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  142. if ctx.Repo.Repository.IsGoget {
  143. ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", setting.AppUrl, user.LowerName, repo.LowerName)
  144. ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", setting.Domain, user.LowerName, repo.LowerName)
  145. }
  146. // when repo is bare, not valid branch
  147. if !ctx.Repo.Repository.IsBare && validBranch {
  148. detect:
  149. if len(refName) > 0 {
  150. if gitRepo.IsBranchExist(refName) {
  151. ctx.Repo.IsBranch = true
  152. ctx.Repo.BranchName = refName
  153. ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
  154. if err != nil {
  155. ctx.Handle(404, "RepoAssignment invalid branch", nil)
  156. return
  157. }
  158. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  159. } else if gitRepo.IsTagExist(refName) {
  160. ctx.Repo.IsBranch = true
  161. ctx.Repo.BranchName = refName
  162. ctx.Repo.Commit, err = gitRepo.GetCommitOfTag(refName)
  163. if err != nil {
  164. ctx.Handle(404, "RepoAssignment invalid tag", nil)
  165. return
  166. }
  167. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  168. } else if len(refName) == 40 {
  169. ctx.Repo.IsCommit = true
  170. ctx.Repo.CommitId = refName
  171. ctx.Repo.BranchName = refName
  172. ctx.Repo.Commit, err = gitRepo.GetCommit(refName)
  173. if err != nil {
  174. ctx.Handle(404, "RepoAssignment invalid commit", nil)
  175. return
  176. }
  177. } else {
  178. ctx.Handle(404, "RepoAssignment invalid repo", nil)
  179. return
  180. }
  181. } else {
  182. if len(refName) == 0 {
  183. if gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  184. refName = ctx.Repo.Repository.DefaultBranch
  185. } else {
  186. brs, err := gitRepo.GetBranches()
  187. if err != nil {
  188. ctx.Handle(500, "RepoAssignment(GetBranches))", err)
  189. return
  190. }
  191. refName = brs[0]
  192. }
  193. }
  194. goto detect
  195. }
  196. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  197. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  198. }
  199. log.Debug("displayBare: %v; IsBare: %v", displayBare, ctx.Repo.Repository.IsBare)
  200. // repo is bare and display enable
  201. if displayBare && ctx.Repo.Repository.IsBare {
  202. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  203. ctx.HTML(200, "repo/single_bare")
  204. return
  205. }
  206. if ctx.IsSigned {
  207. ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
  208. }
  209. ctx.Data["BranchName"] = ctx.Repo.BranchName
  210. brs, err := ctx.Repo.GitRepo.GetBranches()
  211. if err != nil {
  212. log.Error("RepoAssignment(GetBranches): %v", err)
  213. }
  214. ctx.Data["Branches"] = brs
  215. ctx.Data["CommitId"] = ctx.Repo.CommitId
  216. ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
  217. }
  218. }
  219. func RequireOwner() martini.Handler {
  220. return func(ctx *Context) {
  221. if !ctx.Repo.IsOwner {
  222. if !ctx.IsSigned {
  223. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  224. ctx.Redirect("/user/login")
  225. return
  226. }
  227. ctx.Handle(404, ctx.Req.RequestURI, nil)
  228. return
  229. }
  230. }
  231. }