2
0

user.go 3.2 KB

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