api.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. // RegisterRoutes registers all v1 APIs routes to web application.
  136. // FIXME: custom form error response
  137. func RegisterRoutes(m *macaron.Macaron) {
  138. bind := binding.Bind
  139. m.Group("/v1", func() {
  140. // Miscellaneous
  141. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  142. m.Post("/markdown/raw", misc.MarkdownRaw)
  143. // Users
  144. m.Group("/users", func() {
  145. m.Get("/search", user.Search)
  146. m.Group("/:username", func() {
  147. m.Get("", user.GetInfo)
  148. m.Group("/tokens", func() {
  149. m.Combo("").Get(user.ListAccessTokens).
  150. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  151. }, ReqBasicAuth())
  152. })
  153. })
  154. m.Group("/users", func() {
  155. m.Group("/:username", func() {
  156. m.Get("/keys", user.ListPublicKeys)
  157. m.Get("/followers", user.ListFollowers)
  158. m.Group("/following", func() {
  159. m.Get("", user.ListFollowing)
  160. m.Get("/:target", user.CheckFollowing)
  161. })
  162. })
  163. }, ReqToken())
  164. m.Group("/user", func() {
  165. m.Combo("/emails").Get(user.ListEmails).
  166. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  167. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  168. m.Get("/followers", user.ListMyFollowers)
  169. m.Group("/following", func() {
  170. m.Get("", user.ListMyFollowing)
  171. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  172. })
  173. m.Group("/keys", func() {
  174. m.Combo("").Get(user.ListMyPublicKeys).
  175. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  176. m.Combo("/:id").Get(user.GetPublicKey).
  177. Delete(user.DeletePublicKey)
  178. })
  179. }, ReqToken())
  180. // Repositories
  181. m.Combo("/user/repos", ReqToken()).Get(repo.ListMyRepos).
  182. Post(bind(api.CreateRepoOption{}), repo.Create)
  183. m.Post("/org/:org/repos", ReqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  184. m.Group("/repos", func() {
  185. m.Get("/search", repo.Search)
  186. })
  187. m.Group("/repos", func() {
  188. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  189. m.Combo("/:username/:reponame").Get(repo.Get).
  190. Delete(repo.Delete)
  191. m.Group("/:username/:reponame", func() {
  192. m.Group("/hooks", func() {
  193. m.Combo("").Get(repo.ListHooks).
  194. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  195. m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
  196. Delete(repo.DeleteHook)
  197. })
  198. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  199. m.Get("/archive/*", repo.GetArchive)
  200. m.Group("/branches", func() {
  201. m.Get("", repo.ListBranches)
  202. m.Get("/:branchname", repo.GetBranch)
  203. })
  204. m.Group("/keys", func() {
  205. m.Combo("").Get(repo.ListDeployKeys).
  206. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  207. m.Combo("/:id").Get(repo.GetDeployKey).
  208. Delete(repo.DeleteDeploykey)
  209. })
  210. m.Group("/issues", func() {
  211. m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  212. m.Combo("/:index").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  213. })
  214. }, RepoAssignment())
  215. }, ReqToken())
  216. // Organizations
  217. m.Get("/user/orgs", ReqToken(), org.ListMyOrgs)
  218. m.Get("/users/:username/orgs", org.ListUserOrgs)
  219. m.Group("/orgs/:orgname", func() {
  220. m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  221. m.Combo("/teams").Get(org.ListTeams)
  222. }, OrgAssignment(true))
  223. m.Any("/*", func(ctx *context.Context) {
  224. ctx.Error(404)
  225. })
  226. m.Group("/admin", func() {
  227. m.Group("/users", func() {
  228. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  229. m.Group("/:username", func() {
  230. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  231. Delete(admin.DeleteUser)
  232. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  233. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  234. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  235. })
  236. })
  237. m.Group("/orgs/:orgname", func() {
  238. m.Group("/teams", func() {
  239. m.Post("", OrgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  240. })
  241. })
  242. m.Group("/teams", func() {
  243. m.Group("/:teamid", func() {
  244. m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
  245. m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
  246. }, OrgAssignment(false, true))
  247. })
  248. }, ReqAdmin())
  249. }, context.APIContexter())
  250. }