user.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // UpdateUser modifies an existing User in the database
  73. func (repo *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
  74. if !repo.canQuery {
  75. return nil, errors.New("Cannot write database")
  76. }
  77. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  78. return nil, gorm.ErrRecordNotFound
  79. }
  80. index := int(user.ID - 1)
  81. oldUser := *repo.users[index]
  82. repo.users[index] = user
  83. user.Email = oldUser.Email
  84. user.Password = oldUser.Password
  85. return user, nil
  86. }
  87. // DeleteUser deletes a single user using their unique id
  88. func (repo *UserRepository) DeleteUser(user *models.User) (*models.User, error) {
  89. if !repo.canQuery {
  90. return nil, errors.New("Cannot write database")
  91. }
  92. if int(user.ID-1) >= len(repo.users) || repo.users[user.ID-1] == nil {
  93. return nil, gorm.ErrRecordNotFound
  94. }
  95. index := int(user.ID - 1)
  96. repo.users[index] = nil
  97. return user, nil
  98. }
  99. // CheckPassword checks the input password is correct for the provided user id.
  100. func (repo *UserRepository) CheckPassword(id int, pwd string) (bool, error) {
  101. if !repo.canQuery {
  102. return false, errors.New("Cannot write database")
  103. }
  104. if int(id-1) >= len(repo.users) || repo.users[id-1] == nil {
  105. return false, gorm.ErrRecordNotFound
  106. }
  107. index := int(id - 1)
  108. user := *repo.users[index]
  109. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pwd)); err != nil {
  110. return false, err
  111. }
  112. return true, nil
  113. }