user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. func (repo *UserRepository) ListUsersByIDs(ids []uint) ([]*models.User, error) {
  49. if !repo.canQuery {
  50. return nil, errors.New("Cannot read from database")
  51. }
  52. resp := make([]*models.User, 0)
  53. // find all roles matching
  54. for _, user := range repo.users {
  55. for _, userID := range ids {
  56. if userID == user.ID {
  57. resp = append(resp, user)
  58. }
  59. }
  60. }
  61. return resp, nil
  62. }
  63. // ReadUserByEmail finds a single user based on their unique email
  64. func (repo *UserRepository) ReadUserByEmail(email string) (*models.User, error) {
  65. if !repo.canQuery {
  66. return nil, errors.New("Cannot read from database")
  67. }
  68. for _, u := range repo.users {
  69. if u.Email == email {
  70. return u, nil
  71. }
  72. }
  73. return nil, gorm.ErrRecordNotFound
  74. }
  75. // ReadUserByGithubUserID finds a single user based on their github id field
  76. func (repo *UserRepository) ReadUserByGithubUserID(id int64) (*models.User, error) {
  77. if !repo.canQuery {
  78. return nil, errors.New("Cannot read from database")
  79. }
  80. for _, u := range repo.users {
  81. if u.GithubUserID == id && id != 0 {
  82. return u, nil
  83. }
  84. }
  85. return nil, gorm.ErrRecordNotFound
  86. }
  87. // ReadUserByGoogleUserID finds a single user based on their github id field
  88. func (repo *UserRepository) ReadUserByGoogleUserID(id string) (*models.User, error) {
  89. if !repo.canQuery {
  90. return nil, errors.New("Cannot read from database")
  91. }
  92. for _, u := range repo.users {
  93. if u.GoogleUserID == id && id != "" {
  94. return u, nil
  95. }
  96. }
  97. return nil, gorm.ErrRecordNotFound
  98. }
  99. // UpdateUser modifies an existing User in the database
  100. func (repo *UserRepository) UpdateUser(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. oldUser := *repo.users[index]
  109. repo.users[index] = user
  110. user.Email = oldUser.Email
  111. user.Password = oldUser.Password
  112. return user, nil
  113. }
  114. // DeleteUser deletes a single user using their unique id
  115. func (repo *UserRepository) DeleteUser(user *models.User) (*models.User, error) {
  116. if !repo.canQuery {
  117. return nil, errors.New("Cannot write database")
  118. }
  119. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  120. return nil, gorm.ErrRecordNotFound
  121. }
  122. index := int(user.ID - 1)
  123. repo.users[index] = nil
  124. return user, nil
  125. }
  126. // CheckPassword checks the input password is correct for the provided user id.
  127. func (repo *UserRepository) CheckPassword(id int, pwd string) (bool, error) {
  128. if !repo.canQuery {
  129. return false, errors.New("Cannot write database")
  130. }
  131. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  132. return false, gorm.ErrRecordNotFound
  133. }
  134. index := int(id - 1)
  135. user := *repo.users[index]
  136. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pwd)); err != nil {
  137. return false, err
  138. }
  139. return true, nil
  140. }