user.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "golang.org/x/crypto/bcrypt"
  7. "gorm.io/gorm"
  8. )
  9. // UserRepository will return errors on queries if canQuery is false
  10. // and only stores a small set of users in-memory that are indexed by their
  11. // array index + 1
  12. type UserRepository struct {
  13. canQuery bool
  14. users []*models.User
  15. }
  16. // NewUserRepository will return errors if canQuery is false
  17. func NewUserRepository(canQuery bool) repository.UserRepository {
  18. return &UserRepository{canQuery, []*models.User{}}
  19. }
  20. // CreateUser adds a new User row to the Users table in array memory
  21. func (repo *UserRepository) CreateUser(user *models.User) (*models.User, error) {
  22. if !repo.canQuery {
  23. return nil, errors.New("Cannot write database")
  24. }
  25. // make sure email doesn't exist
  26. for _, u := range repo.users {
  27. if u.Email == user.Email {
  28. return nil, errors.New("Cannot write database")
  29. }
  30. }
  31. users := repo.users
  32. users = append(users, user)
  33. repo.users = users
  34. user.ID = uint(len(repo.users))
  35. return user, nil
  36. }
  37. // ReadUser finds a single user based on their unique id
  38. func (repo *UserRepository) ReadUser(id uint) (*models.User, error) {
  39. if !repo.canQuery {
  40. return nil, errors.New("Cannot read from database")
  41. }
  42. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  43. return nil, gorm.ErrRecordNotFound
  44. }
  45. index := int(id - 1)
  46. return repo.users[index], nil
  47. }
  48. // ReadUserByEmail finds a single user based on their unique email
  49. func (repo *UserRepository) ReadUserByEmail(email string) (*models.User, error) {
  50. if !repo.canQuery {
  51. return nil, errors.New("Cannot read from database")
  52. }
  53. for _, u := range repo.users {
  54. if u.Email == email {
  55. return u, nil
  56. }
  57. }
  58. return nil, gorm.ErrRecordNotFound
  59. }
  60. // ReadUserByGithubUserID finds a single user based on their github id field
  61. func (repo *UserRepository) ReadUserByGithubUserID(id int64) (*models.User, error) {
  62. if !repo.canQuery {
  63. return nil, errors.New("Cannot read from database")
  64. }
  65. for _, u := range repo.users {
  66. if u.GithubUserID == id && id != 0 {
  67. return u, nil
  68. }
  69. }
  70. return nil, gorm.ErrRecordNotFound
  71. }
  72. // ReadUserByGoogleUserID finds a single user based on their github id field
  73. func (repo *UserRepository) ReadUserByGoogleUserID(id string) (*models.User, error) {
  74. if !repo.canQuery {
  75. return nil, errors.New("Cannot read from database")
  76. }
  77. for _, u := range repo.users {
  78. if u.GoogleUserID == id && id != "" {
  79. return u, nil
  80. }
  81. }
  82. return nil, gorm.ErrRecordNotFound
  83. }
  84. // UpdateUser modifies an existing User in the database
  85. func (repo *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
  86. if !repo.canQuery {
  87. return nil, errors.New("Cannot write database")
  88. }
  89. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  90. return nil, gorm.ErrRecordNotFound
  91. }
  92. index := int(user.ID - 1)
  93. oldUser := *repo.users[index]
  94. repo.users[index] = user
  95. user.Email = oldUser.Email
  96. user.Password = oldUser.Password
  97. return user, nil
  98. }
  99. // DeleteUser deletes a single user using their unique id
  100. func (repo *UserRepository) DeleteUser(user *models.User) (*models.User, error) {
  101. if !repo.canQuery {
  102. return nil, errors.New("Cannot write database")
  103. }
  104. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  105. return nil, gorm.ErrRecordNotFound
  106. }
  107. index := int(user.ID - 1)
  108. repo.users[index] = nil
  109. return user, nil
  110. }
  111. // CheckPassword checks the input password is correct for the provided user id.
  112. func (repo *UserRepository) CheckPassword(id int, pwd string) (bool, error) {
  113. if !repo.canQuery {
  114. return false, errors.New("Cannot write database")
  115. }
  116. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  117. return false, gorm.ErrRecordNotFound
  118. }
  119. index := int(id - 1)
  120. user := *repo.users[index]
  121. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pwd)); err != nil {
  122. return false, err
  123. }
  124. return true, nil
  125. }