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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2015 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 v1
  5. import (
  6. "strings"
  7. "github.com/go-macaron/binding"
  8. "gopkg.in/macaron.v1"
  9. api "github.com/gogits/go-gogs-client"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/context"
  13. "github.com/gogits/gogs/routers/api/v1/admin"
  14. "github.com/gogits/gogs/routers/api/v1/misc"
  15. "github.com/gogits/gogs/routers/api/v1/org"
  16. "github.com/gogits/gogs/routers/api/v1/repo"
  17. "github.com/gogits/gogs/routers/api/v1/user"
  18. )
  19. func RepoAssignment() macaron.Handler {
  20. return func(ctx *context.APIContext) {
  21. userName := ctx.Params(":username")
  22. repoName := ctx.Params(":reponame")
  23. var (
  24. owner *models.User
  25. err error
  26. )
  27. // Check if the user is the same as the repository owner.
  28. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  29. owner = ctx.User
  30. } else {
  31. owner, err = models.GetUserByName(userName)
  32. if err != nil {
  33. if models.IsErrUserNotExist(err) {
  34. ctx.Status(404)
  35. } else {
  36. ctx.Error(500, "GetUserByName", err)
  37. }
  38. return
  39. }
  40. }
  41. ctx.Repo.Owner = owner
  42. // Get repository.
  43. repo, err := models.GetRepositoryByName(owner.Id, repoName)
  44. if err != nil {
  45. if models.IsErrRepoNotExist(err) {
  46. ctx.Status(404)
  47. } else {
  48. ctx.Error(500, "GetRepositoryByName", err)
  49. }
  50. return
  51. } else if err = repo.GetOwner(); err != nil {
  52. ctx.Error(500, "GetOwner", err)
  53. return
  54. }
  55. if ctx.IsSigned && ctx.User.IsAdmin {
  56. ctx.Repo.AccessMode = models.ACCESS_MODE_OWNER
  57. } else {
  58. mode, err := models.AccessLevel(ctx.User, repo)
  59. if err != nil {
  60. ctx.Error(500, "AccessLevel", err)
  61. return
  62. }
  63. ctx.Repo.AccessMode = mode
  64. }
  65. if !ctx.Repo.HasAccess() {
  66. ctx.Status(404)
  67. return
  68. }
  69. ctx.Repo.Repository = repo
  70. }
  71. }
  72. // Contexter middleware already checks token for user sign in process.
  73. func ReqToken() macaron.Handler {
  74. return func(ctx *context.Context) {
  75. if !ctx.IsSigned {
  76. ctx.Error(401)
  77. return
  78. }
  79. }
  80. }
  81. func ReqBasicAuth() macaron.Handler {
  82. return func(ctx *context.Context) {
  83. if !ctx.IsBasicAuth {
  84. ctx.Error(401)
  85. return
  86. }
  87. }
  88. }
  89. func ReqAdmin() macaron.Handler {
  90. return func(ctx *context.Context) {
  91. if !ctx.User.IsAdmin {
  92. ctx.Error(403)
  93. return
  94. }
  95. }
  96. }
  97. // RegisterRoutes registers all v1 APIs routes to web application.
  98. // FIXME: custom form error response
  99. func RegisterRoutes(m *macaron.Macaron) {
  100. bind := binding.Bind
  101. m.Group("/v1", func() {
  102. // Miscellaneous
  103. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  104. m.Post("/markdown/raw", misc.MarkdownRaw)
  105. // Users
  106. m.Group("/users", func() {
  107. m.Get("/search", user.Search)
  108. m.Group("/:username", func() {
  109. m.Get("", user.GetInfo)
  110. m.Group("/tokens", func() {
  111. m.Combo("").Get(user.ListAccessTokens).
  112. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  113. }, ReqBasicAuth())
  114. })
  115. })
  116. m.Group("/users", func() {
  117. m.Group("/:username", func() {
  118. m.Get("/keys", user.ListPublicKeys)
  119. m.Get("/followers", user.ListFollowers)
  120. m.Group("/following", func() {
  121. m.Get("", user.ListFollowing)
  122. m.Get("/:target", user.CheckFollowing)
  123. })
  124. })
  125. }, ReqToken())
  126. m.Group("/user", func() {
  127. m.Combo("/emails").Get(user.ListEmails).
  128. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  129. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  130. m.Get("/followers", user.ListMyFollowers)
  131. m.Group("/following", func() {
  132. m.Get("", user.ListMyFollowing)
  133. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  134. })
  135. m.Group("/keys", func() {
  136. m.Combo("").Get(user.ListMyPublicKeys).
  137. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  138. m.Combo("/:id").Get(user.GetPublicKey).
  139. Delete(user.DeletePublicKey)
  140. })
  141. }, ReqToken())
  142. // Repositories
  143. m.Combo("/user/repos", ReqToken()).Get(repo.ListMyRepos).
  144. Post(bind(api.CreateRepoOption{}), repo.Create)
  145. m.Post("/org/:org/repos", ReqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  146. m.Group("/repos", func() {
  147. m.Get("/search", repo.Search)
  148. })
  149. m.Group("/repos", func() {
  150. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  151. m.Combo("/:username/:reponame").Get(repo.Get).
  152. Delete(repo.Delete)
  153. m.Group("/:username/:reponame", func() {
  154. m.Combo("/hooks").Get(repo.ListHooks).
  155. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  156. m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), repo.EditHook)
  157. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  158. m.Get("/archive/*", repo.GetArchive)
  159. m.Group("/branches", func() {
  160. m.Get("", repo.ListBranches)
  161. m.Get("/:branchname", repo.GetBranch)
  162. })
  163. m.Group("/keys", func() {
  164. m.Combo("").Get(repo.ListDeployKeys).
  165. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  166. m.Combo("/:id").Get(repo.GetDeployKey).
  167. Delete(repo.DeleteDeploykey)
  168. })
  169. m.Group("/issues", func() {
  170. m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  171. m.Combo("/:index").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  172. })
  173. }, RepoAssignment())
  174. }, ReqToken())
  175. // Organizations
  176. m.Get("/user/orgs", ReqToken(), org.ListMyOrgs)
  177. m.Get("/users/:username/orgs", org.ListUserOrgs)
  178. m.Combo("/orgs/:orgname").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  179. m.Any("/*", func(ctx *context.Context) {
  180. ctx.Error(404)
  181. })
  182. m.Group("/admin", func() {
  183. m.Group("/users", func() {
  184. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  185. m.Group("/:username", func() {
  186. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  187. Delete(admin.DeleteUser)
  188. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  189. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  190. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  191. })
  192. })
  193. }, ReqAdmin())
  194. }, context.APIContexter())
  195. }