/api/users.go

https://github.com/capitalone/checks-out · Go · 88 lines · 61 code · 6 blank · 21 comment · 17 complexity · a291a003e1b85e6aa1463201a32503ab MD5 · raw file

  1. /*
  2. SPDX-Copyright: Copyright (c) Brad Rydzewski, project contributors, Capital One Services, LLC
  3. SPDX-License-Identifier: Apache-2.0
  4. Copyright 2017 Brad Rydzewski, project contributors, Capital One Services, LLC
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and limitations under the License.
  13. */
  14. package api
  15. import (
  16. "database/sql"
  17. "net/http"
  18. "github.com/capitalone/checks-out/exterror"
  19. "github.com/capitalone/checks-out/remote"
  20. "github.com/capitalone/checks-out/router/middleware/session"
  21. "github.com/capitalone/checks-out/store"
  22. "github.com/gin-gonic/gin"
  23. )
  24. // GetUser gets the currently authenticated user.
  25. func GetUser(c *gin.Context) {
  26. IndentedJSON(c, 200, session.User(c))
  27. }
  28. // DeleteUser removes the currently authenticated user
  29. // and all associated repositories from the database.
  30. func DeleteUser(c *gin.Context) {
  31. user := session.User(c)
  32. repos, err := store.GetRepoUserId(c, user.ID)
  33. if err != nil {
  34. c.Error(exterror.Append(err, "Deleting user"))
  35. return
  36. }
  37. for _, repo := range repos {
  38. err = store.DeleteRepo(c, repo)
  39. if err != nil {
  40. c.Error(exterror.Append(err, "Deleting user"))
  41. return
  42. }
  43. }
  44. err = store.DeleteUser(c, user)
  45. if err != nil {
  46. c.Error(exterror.Append(err, "Deleting user"))
  47. return
  48. }
  49. err = remote.RevokeAuthorization(c, user)
  50. if err != nil {
  51. c.Error(exterror.Append(err, "Deleting user"))
  52. return
  53. }
  54. c.String(204, "")
  55. }
  56. func GetReposForUserLogin(c *gin.Context) {
  57. var (
  58. login = c.Param("user")
  59. )
  60. user, err := store.GetUserLogin(c, login)
  61. if err != nil {
  62. if err == sql.ErrNoRows {
  63. err = exterror.Create(http.StatusNotFound, err)
  64. }
  65. c.Error(err)
  66. return
  67. }
  68. repos, err := store.GetRepoUserId(c, user.ID)
  69. if err != nil {
  70. if err == sql.ErrNoRows {
  71. err = exterror.Create(http.StatusNotFound, err)
  72. }
  73. c.Error(err)
  74. return
  75. }
  76. IndentedJSON(c, 200, repos)
  77. }