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.

331 lines
9.0 KiB

10 years ago
10 years ago
10 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
10 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 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. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package user
  6. import (
  7. "errors"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. )
  15. // ListAccessTokens list all the access tokens
  16. func ListAccessTokens(ctx *context.APIContext) {
  17. // swagger:operation GET /users/{username}/tokens user userGetTokens
  18. // ---
  19. // summary: List the authenticated user's access tokens
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: username
  24. // in: path
  25. // description: username of user
  26. // type: string
  27. // required: true
  28. // - name: page
  29. // in: query
  30. // description: page number of results to return (1-based)
  31. // type: integer
  32. // - name: limit
  33. // in: query
  34. // description: page size of results, maximum page size is 50
  35. // type: integer
  36. // responses:
  37. // "200":
  38. // "$ref": "#/responses/AccessTokenList"
  39. tokens, err := models.ListAccessTokens(ctx.User.ID, utils.GetListOptions(ctx))
  40. if err != nil {
  41. ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err)
  42. return
  43. }
  44. apiTokens := make([]*api.AccessToken, len(tokens))
  45. for i := range tokens {
  46. apiTokens[i] = &api.AccessToken{
  47. ID: tokens[i].ID,
  48. Name: tokens[i].Name,
  49. TokenLastEight: tokens[i].TokenLastEight,
  50. }
  51. }
  52. ctx.JSON(http.StatusOK, &apiTokens)
  53. }
  54. // CreateAccessToken create access tokens
  55. func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) {
  56. // swagger:operation POST /users/{username}/tokens user userCreateToken
  57. // ---
  58. // summary: Create an access token
  59. // consumes:
  60. // - application/json
  61. // produces:
  62. // - application/json
  63. // parameters:
  64. // - name: username
  65. // in: path
  66. // description: username of user
  67. // type: string
  68. // required: true
  69. // - name: accessToken
  70. // in: body
  71. // schema:
  72. // type: object
  73. // required:
  74. // - name
  75. // properties:
  76. // name:
  77. // type: string
  78. // responses:
  79. // "200":
  80. // "$ref": "#/responses/AccessToken"
  81. t := &models.AccessToken{
  82. UID: ctx.User.ID,
  83. Name: form.Name,
  84. }
  85. exist, err := models.AccessTokenByNameExists(t)
  86. if err != nil {
  87. ctx.InternalServerError(err)
  88. return
  89. }
  90. if exist {
  91. ctx.Error(http.StatusBadRequest, "AccessTokenByNameExists", errors.New("access token name has been used already"))
  92. return
  93. }
  94. if err := models.NewAccessToken(t); err != nil {
  95. ctx.Error(http.StatusInternalServerError, "NewAccessToken", err)
  96. return
  97. }
  98. ctx.JSON(http.StatusCreated, &api.AccessToken{
  99. Name: t.Name,
  100. Token: t.Token,
  101. ID: t.ID,
  102. TokenLastEight: t.TokenLastEight,
  103. })
  104. }
  105. // DeleteAccessToken delete access tokens
  106. func DeleteAccessToken(ctx *context.APIContext) {
  107. // swagger:operation DELETE /users/{username}/tokens/{token} user userDeleteAccessToken
  108. // ---
  109. // summary: delete an access token
  110. // produces:
  111. // - application/json
  112. // parameters:
  113. // - name: username
  114. // in: path
  115. // description: username of user
  116. // type: string
  117. // required: true
  118. // - name: token
  119. // in: path
  120. // description: token to be deleted
  121. // type: integer
  122. // format: int64
  123. // required: true
  124. // responses:
  125. // "204":
  126. // "$ref": "#/responses/empty"
  127. tokenID := ctx.ParamsInt64(":id")
  128. if err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil {
  129. if models.IsErrAccessTokenNotExist(err) {
  130. ctx.NotFound()
  131. } else {
  132. ctx.Error(http.StatusInternalServerError, "DeleteAccessTokenByID", err)
  133. }
  134. return
  135. }
  136. ctx.Status(http.StatusNoContent)
  137. }
  138. // CreateOauth2Application is the handler to create a new OAuth2 Application for the authenticated user
  139. func CreateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2ApplicationOptions) {
  140. // swagger:operation POST /user/applications/oauth2 user userCreateOAuth2Application
  141. // ---
  142. // summary: creates a new OAuth2 application
  143. // produces:
  144. // - application/json
  145. // parameters:
  146. // - name: body
  147. // in: body
  148. // required: true
  149. // schema:
  150. // "$ref": "#/definitions/CreateOAuth2ApplicationOptions"
  151. // responses:
  152. // "201":
  153. // "$ref": "#/responses/OAuth2Application"
  154. app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
  155. Name: data.Name,
  156. UserID: ctx.User.ID,
  157. RedirectURIs: data.RedirectURIs,
  158. })
  159. if err != nil {
  160. ctx.Error(http.StatusBadRequest, "", "error creating oauth2 application")
  161. return
  162. }
  163. secret, err := app.GenerateClientSecret()
  164. if err != nil {
  165. ctx.Error(http.StatusBadRequest, "", "error creating application secret")
  166. return
  167. }
  168. app.ClientSecret = secret
  169. ctx.JSON(http.StatusCreated, convert.ToOAuth2Application(app))
  170. }
  171. // ListOauth2Applications list all the Oauth2 application
  172. func ListOauth2Applications(ctx *context.APIContext) {
  173. // swagger:operation GET /user/applications/oauth2 user userGetOauth2Application
  174. // ---
  175. // summary: List the authenticated user's oauth2 applications
  176. // produces:
  177. // - application/json
  178. // parameters:
  179. // - name: page
  180. // in: query
  181. // description: page number of results to return (1-based)
  182. // type: integer
  183. // - name: limit
  184. // in: query
  185. // description: page size of results, maximum page size is 50
  186. // type: integer
  187. // responses:
  188. // "200":
  189. // "$ref": "#/responses/OAuth2ApplicationList"
  190. apps, err := models.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx))
  191. if err != nil {
  192. ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err)
  193. return
  194. }
  195. apiApps := make([]*api.OAuth2Application, len(apps))
  196. for i := range apps {
  197. apiApps[i] = convert.ToOAuth2Application(apps[i])
  198. apiApps[i].ClientSecret = "" // Hide secret on application list
  199. }
  200. ctx.JSON(http.StatusOK, &apiApps)
  201. }
  202. // DeleteOauth2Application delete OAuth2 Application
  203. func DeleteOauth2Application(ctx *context.APIContext) {
  204. // swagger:operation DELETE /user/applications/oauth2/{id} user userDeleteOAuth2Application
  205. // ---
  206. // summary: delete an OAuth2 Application
  207. // produces:
  208. // - application/json
  209. // parameters:
  210. // - name: id
  211. // in: path
  212. // description: token to be deleted
  213. // type: integer
  214. // format: int64
  215. // required: true
  216. // responses:
  217. // "204":
  218. // "$ref": "#/responses/empty"
  219. appID := ctx.ParamsInt64(":id")
  220. if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
  221. ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
  222. return
  223. }
  224. ctx.Status(http.StatusNoContent)
  225. }
  226. // GetOauth2Application get OAuth2 Application
  227. func GetOauth2Application(ctx *context.APIContext) {
  228. // swagger:operation GET /user/applications/oauth2/{id} user userGetOAuth2Application
  229. // ---
  230. // summary: get an OAuth2 Application
  231. // produces:
  232. // - application/json
  233. // parameters:
  234. // - name: id
  235. // in: path
  236. // description: Application ID to be found
  237. // type: integer
  238. // format: int64
  239. // required: true
  240. // responses:
  241. // "200":
  242. // "$ref": "#/responses/OAuth2Application"
  243. appID := ctx.ParamsInt64(":id")
  244. app, err := models.GetOAuth2ApplicationByID(appID)
  245. if err != nil {
  246. if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) {
  247. ctx.NotFound()
  248. } else {
  249. ctx.Error(http.StatusInternalServerError, "GetOauth2ApplicationByID", err)
  250. }
  251. return
  252. }
  253. app.ClientSecret = ""
  254. ctx.JSON(http.StatusOK, convert.ToOAuth2Application(app))
  255. }
  256. // UpdateOauth2Application update OAuth2 Application
  257. func UpdateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2ApplicationOptions) {
  258. // swagger:operation PATCH /user/applications/oauth2/{id} user userUpdateOAuth2Application
  259. // ---
  260. // summary: update an OAuth2 Application, this includes regenerating the client secret
  261. // produces:
  262. // - application/json
  263. // parameters:
  264. // - name: id
  265. // in: path
  266. // description: application to be updated
  267. // type: integer
  268. // format: int64
  269. // required: true
  270. // - name: body
  271. // in: body
  272. // required: true
  273. // schema:
  274. // "$ref": "#/definitions/CreateOAuth2ApplicationOptions"
  275. // responses:
  276. // "200":
  277. // "$ref": "#/responses/OAuth2Application"
  278. appID := ctx.ParamsInt64(":id")
  279. err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
  280. Name: data.Name,
  281. UserID: ctx.User.ID,
  282. ID: appID,
  283. RedirectURIs: data.RedirectURIs,
  284. })
  285. if err != nil {
  286. ctx.Error(http.StatusBadRequest, "", "error updating oauth2 application")
  287. return
  288. }
  289. app, err := models.GetOAuth2ApplicationByID(appID)
  290. if err != nil {
  291. if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) {
  292. ctx.NotFound()
  293. } else {
  294. ctx.Error(http.StatusInternalServerError, "UpdateOauth2ApplicationByID", err)
  295. }
  296. return
  297. }
  298. secret, err := app.GenerateClientSecret()
  299. if err != nil {
  300. ctx.Error(http.StatusBadRequest, "", "error updating application secret")
  301. return
  302. }
  303. app.ClientSecret = secret
  304. ctx.JSON(http.StatusOK, convert.ToOAuth2Application(app))
  305. }