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.

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