2
0

user.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // ReadUserByGithubUserID finds a single user based on their github id field
  84. func (repo *UserRepository) ReadUserByGithubUserID(id int64) (*models.User, error) {
  85. if !repo.canQuery {
  86. return nil, errors.New("Cannot read from database")
  87. }
  88. for _, u := range repo.users {
  89. if u.GithubUserID == id && id != 0 {
  90. return u, nil
  91. }
  92. }
  93. return nil, gorm.ErrRecordNotFound
  94. }
  95. // ReadUserByGoogleUserID finds a single user based on their github id field
  96. func (repo *UserRepository) ReadUserByGoogleUserID(id string) (*models.User, error) {
  97. if !repo.canQuery {
  98. return nil, errors.New("Cannot read from database")
  99. }
  100. for _, u := range repo.users {
  101. if u.GoogleUserID == id && id != "" {
  102. return u, nil
  103. }
  104. }
  105. return nil, gorm.ErrRecordNotFound
  106. }
  107. // UpdateUser modifies an existing User in the database
  108. func (repo *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
  109. if !repo.canQuery {
  110. return nil, errors.New("Cannot write database")
  111. }
  112. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  113. return nil, gorm.ErrRecordNotFound
  114. }
  115. index := int(user.ID - 1)
  116. oldUser := *repo.users[index]
  117. repo.users[index] = user
  118. user.Email = oldUser.Email
  119. user.Password = oldUser.Password
  120. return user, nil
  121. }
  122. // DeleteUser deletes a single user using their unique id
  123. func (repo *UserRepository) DeleteUser(user *models.User) (*models.User, error) {
  124. if !repo.canQuery || strings.Contains(repo.failingMethods, DeleteUserMethod) {
  125. return nil, errors.New("Cannot write database")
  126. }
  127. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  128. return nil, gorm.ErrRecordNotFound
  129. }
  130. index := int(user.ID - 1)
  131. repo.users[index] = nil
  132. return user, nil
  133. }
  134. // CheckPassword checks the input password is correct for the provided user id.
  135. func (repo *UserRepository) CheckPassword(id int, pwd string) (bool, error) {
  136. if !repo.canQuery {
  137. return false, errors.New("Cannot write database")
  138. }
  139. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  140. return false, gorm.ErrRecordNotFound
  141. }
  142. index := int(id - 1)
  143. user := *repo.users[index]
  144. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pwd)); err != nil {
  145. return false, err
  146. }
  147. return true, nil
  148. }