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.

310 lines
8.2 KiB

8 years ago
8 years ago
8 years ago
  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.IsSigned || !ctx.User.IsAdmin {
  92. ctx.Error(403)
  93. return
  94. }
  95. }
  96. }
  97. func orgAssignment(args ...bool) macaron.Handler {
  98. var (
  99. assignOrg bool
  100. assignTeam bool
  101. )
  102. if len(args) > 0 {
  103. assignOrg = args[0]
  104. }
  105. if len(args) > 1 {
  106. assignTeam = args[1]
  107. }
  108. return func(ctx *context.APIContext) {
  109. ctx.Org = new(context.APIOrganization)
  110. var err error
  111. if assignOrg {
  112. ctx.Org.Organization, err = models.GetUserByName(ctx.Params(":orgname"))
  113. if err != nil {
  114. if models.IsErrUserNotExist(err) {
  115. ctx.Status(404)
  116. } else {
  117. ctx.Error(500, "GetUserByName", err)
  118. }
  119. return
  120. }
  121. }
  122. if assignTeam {
  123. ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
  124. if err != nil {
  125. if models.IsErrUserNotExist(err) {
  126. ctx.Status(404)
  127. } else {
  128. ctx.Error(500, "GetTeamById", err)
  129. }
  130. return
  131. }
  132. }
  133. }
  134. }
  135. func mustEnableIssues(ctx *context.APIContext) {
  136. if !ctx.Repo.Repository.EnableIssues || ctx.Repo.Repository.EnableExternalTracker {
  137. ctx.Status(404)
  138. return
  139. }
  140. }
  141. // RegisterRoutes registers all v1 APIs routes to web application.
  142. // FIXME: custom form error response
  143. func RegisterRoutes(m *macaron.Macaron) {
  144. bind := binding.Bind
  145. m.Group("/v1", func() {
  146. // Miscellaneous
  147. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  148. m.Post("/markdown/raw", misc.MarkdownRaw)
  149. // Users
  150. m.Group("/users", func() {
  151. m.Get("/search", user.Search)
  152. m.Group("/:username", func() {
  153. m.Get("", user.GetInfo)
  154. m.Group("/tokens", func() {
  155. m.Combo("").Get(user.ListAccessTokens).
  156. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  157. }, reqBasicAuth())
  158. })
  159. })
  160. m.Group("/users", func() {
  161. m.Group("/:username", func() {
  162. m.Get("/keys", user.ListPublicKeys)
  163. m.Get("/followers", user.ListFollowers)
  164. m.Group("/following", func() {
  165. m.Get("", user.ListFollowing)
  166. m.Get("/:target", user.CheckFollowing)
  167. })
  168. })
  169. }, reqToken())
  170. m.Group("/user", func() {
  171. m.Combo("/emails").Get(user.ListEmails).
  172. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  173. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  174. m.Get("/followers", user.ListMyFollowers)
  175. m.Group("/following", func() {
  176. m.Get("", user.ListMyFollowing)
  177. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  178. })
  179. m.Group("/keys", func() {
  180. m.Combo("").Get(user.ListMyPublicKeys).
  181. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  182. m.Combo("/:id").Get(user.GetPublicKey).
  183. Delete(user.DeletePublicKey)
  184. })
  185. }, reqToken())
  186. // Repositories
  187. m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
  188. Post(bind(api.CreateRepoOption{}), repo.Create)
  189. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  190. m.Group("/repos", func() {
  191. m.Get("/search", repo.Search)
  192. })
  193. m.Group("/repos", func() {
  194. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  195. m.Combo("/:username/:reponame").Get(repo.Get).
  196. Delete(repo.Delete)
  197. m.Group("/:username/:reponame", func() {
  198. m.Group("/hooks", func() {
  199. m.Combo("").Get(repo.ListHooks).
  200. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  201. m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
  202. Delete(repo.DeleteHook)
  203. })
  204. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  205. m.Get("/archive/*", repo.GetArchive)
  206. m.Group("/branches", func() {
  207. m.Get("", repo.ListBranches)
  208. m.Get("/:branchname", repo.GetBranch)
  209. })
  210. m.Group("/keys", func() {
  211. m.Combo("").Get(repo.ListDeployKeys).
  212. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  213. m.Combo("/:id").Get(repo.GetDeployKey).
  214. Delete(repo.DeleteDeploykey)
  215. })
  216. m.Group("/issues", func() {
  217. m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  218. m.Group("/:index", func() {
  219. m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  220. m.Group("/labels", func() {
  221. m.Combo("").Get(repo.ListIssueLabels).
  222. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  223. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  224. Delete(repo.ClearIssueLabels)
  225. m.Delete("/:id", repo.DeleteIssueLabel)
  226. })
  227. })
  228. }, mustEnableIssues)
  229. m.Group("/labels", func() {
  230. m.Combo("").Get(repo.ListLabels).
  231. Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
  232. m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  233. Delete(repo.DeleteLabel)
  234. })
  235. }, repoAssignment())
  236. }, reqToken())
  237. // Organizations
  238. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  239. m.Get("/users/:username/orgs", org.ListUserOrgs)
  240. m.Group("/orgs/:orgname", func() {
  241. m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  242. m.Combo("/teams").Get(org.ListTeams)
  243. }, orgAssignment(true))
  244. m.Any("/*", func(ctx *context.Context) {
  245. ctx.Error(404)
  246. })
  247. m.Group("/admin", func() {
  248. m.Group("/users", func() {
  249. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  250. m.Group("/:username", func() {
  251. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  252. Delete(admin.DeleteUser)
  253. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  254. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  255. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  256. })
  257. })
  258. m.Group("/orgs/:orgname", func() {
  259. m.Group("/teams", func() {
  260. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  261. })
  262. })
  263. m.Group("/teams", func() {
  264. m.Group("/:teamid", func() {
  265. m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
  266. m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
  267. }, orgAssignment(false, true))
  268. })
  269. }, reqAdmin())
  270. }, context.APIContexter())
  271. }