user.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package test
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/internal/repository"
  7. "golang.org/x/crypto/bcrypt"
  8. "gorm.io/gorm"
  9. )
  10. const (
  11. CreateUserMethod string = "create_user_0"
  12. ReadUserMethod string = "read_user_0"
  13. ReadUserByEmailMethod string = "read_user_by_email_0"
  14. DeleteUserMethod string = "delete_user_0"
  15. )
  16. // UserRepository will return errors on queries if canQuery is false
  17. // and only stores a small set of users in-memory that are indexed by their
  18. // array index + 1
  19. type UserRepository struct {
  20. canQuery bool
  21. failingMethods string
  22. users []*models.User
  23. }
  24. // NewUserRepository will return errors if canQuery is false
  25. func NewUserRepository(canQuery bool, failingMethods ...string) repository.UserRepository {
  26. return &UserRepository{canQuery, strings.Join(failingMethods, ","), []*models.User{}}
  27. }
  28. // CreateUser adds a new User row to the Users table in array memory
  29. func (repo *UserRepository) CreateUser(user *models.User) (*models.User, error) {
  30. if !repo.canQuery || strings.Contains(repo.failingMethods, CreateUserMethod) {
  31. return nil, errors.New("Cannot write database")
  32. }
  33. // make sure email doesn't exist
  34. for _, u := range repo.users {
  35. if u.Email == user.Email {
  36. return nil, errors.New("Cannot write database")
  37. }
  38. }
  39. users := repo.users
  40. users = append(users, user)
  41. repo.users = users
  42. user.ID = uint(len(repo.users))
  43. return user, nil
  44. }
  45. // ReadUser finds a single user based on their unique id
  46. func (repo *UserRepository) ReadUser(id uint) (*models.User, error) {
  47. if !repo.canQuery || strings.Contains(repo.failingMethods, ReadUserMethod) {
  48. return nil, errors.New("Cannot read from database")
  49. }
  50. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  51. return nil, gorm.ErrRecordNotFound
  52. }
  53. index := int(id - 1)
  54. return repo.users[index], nil
  55. }
  56. func (repo *UserRepository) ListUsersByIDs(ids []uint) ([]*models.User, error) {
  57. if !repo.canQuery {
  58. return nil, errors.New("Cannot read from database")
  59. }
  60. resp := make([]*models.User, 0)
  61. // find all roles matching
  62. for _, user := range repo.users {
  63. for _, userID := range ids {
  64. if userID == user.ID {
  65. resp = append(resp, user)
  66. }
  67. }
  68. }
  69. return resp, nil
  70. }
  71. // ReadUserByEmail finds a single user based on their unique email
  72. func (repo *UserRepository) ReadUserByEmail(email string) (*models.User, error) {
  73. if !repo.canQuery || strings.Contains(repo.failingMethods, ReadUserByEmailMethod) {
  74. return nil, errors.New("Cannot read from database")
  75. }
  76. for _, u := range repo.users {
  77. if u.Email == email {
  78. return u, nil
  79. }
  80. }
  81. return nil, gorm.ErrRecordNotFound
  82. }
  83. // ReadUserByAuthProvider finds a single user based on their auth provider and external id
  84. func (repo *UserRepository) ReadUserByAuthProvider(authProvider string, externalId string) (*models.User, error) {
  85. return nil, errors.New("Cannot read from database")
  86. }
  87. // ReadUserByGithubUserID finds a single user based on their github id field
  88. func (repo *UserRepository) ReadUserByGithubUserID(id int64) (*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.GithubUserID == id && id != 0 {
  94. return u, nil
  95. }
  96. }
  97. return nil, gorm.ErrRecordNotFound
  98. }
  99. // ReadUserByGoogleUserID finds a single user based on their github id field
  100. func (repo *UserRepository) ReadUserByGoogleUserID(id string) (*models.User, error) {
  101. if !repo.canQuery {
  102. return nil, errors.New("Cannot read from database")
  103. }
  104. for _, u := range repo.users {
  105. if u.GoogleUserID == id && id != "" {
  106. return u, nil
  107. }
  108. }
  109. return nil, gorm.ErrRecordNotFound
  110. }
  111. // UpdateUser modifies an existing User in the database
  112. func (repo *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
  113. if !repo.canQuery {
  114. return nil, errors.New("Cannot write database")
  115. }
  116. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  117. return nil, gorm.ErrRecordNotFound
  118. }
  119. index := int(user.ID - 1)
  120. oldUser := *repo.users[index]
  121. repo.users[index] = user
  122. user.Email = oldUser.Email
  123. user.Password = oldUser.Password
  124. return user, nil
  125. }
  126. // DeleteUser deletes a single user using their unique id
  127. func (repo *UserRepository) DeleteUser(user *models.User) (*models.User, error) {
  128. if !repo.canQuery || strings.Contains(repo.failingMethods, DeleteUserMethod) {
  129. return nil, errors.New("Cannot write database")
  130. }
  131. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  132. return nil, gorm.ErrRecordNotFound
  133. }
  134. index := int(user.ID - 1)
  135. repo.users[index] = nil
  136. return user, nil
  137. }
  138. // CheckPassword checks the input password is correct for the provided user id.
  139. func (repo *UserRepository) CheckPassword(id int, pwd string) (bool, error) {
  140. if !repo.canQuery {
  141. return false, errors.New("Cannot write database")
  142. }
  143. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  144. return false, gorm.ErrRecordNotFound
  145. }
  146. index := int(id - 1)
  147. user := *repo.users[index]
  148. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pwd)); err != nil {
  149. return false, err
  150. }
  151. return true, nil
  152. }
  153. // ListUsers retrieves all users from the database (that are not deleted)
  154. func (repo *UserRepository) ListUsers() ([]*models.User, error) {
  155. return nil, errors.New("Cannot read from database")
  156. }