user_billing.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // +build ee
  2. package gorm
  3. import (
  4. "github.com/porter-dev/porter/ee/models"
  5. "github.com/porter-dev/porter/ee/repository"
  6. "github.com/porter-dev/porter/internal/encryption"
  7. "gorm.io/gorm"
  8. )
  9. // UserBillingRepository uses gorm.DB for querying the database
  10. type UserBillingRepository struct {
  11. db *gorm.DB
  12. key *[32]byte
  13. }
  14. func NewUserBillingRepository(db *gorm.DB, key *[32]byte) repository.UserBillingRepository {
  15. return &UserBillingRepository{db, key}
  16. }
  17. // CreateUserBilling adds a new User row to the Users table in the database
  18. func (repo *UserBillingRepository) CreateUserBilling(userBilling *models.UserBilling) (*models.UserBilling, error) {
  19. err := repo.EncryptUserBillingData(userBilling, repo.key)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if err := repo.db.Create(userBilling).Error; err != nil {
  24. return nil, err
  25. }
  26. err = repo.DecryptUserBillingData(userBilling, repo.key)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return userBilling, nil
  31. }
  32. func (repo *UserBillingRepository) ReadUserBilling(projectID, userID uint) (*models.UserBilling, error) {
  33. userBilling := &models.UserBilling{}
  34. if err := repo.db.Where("project_id = ? AND user_id = ?", projectID, userID).First(&userBilling).Error; err != nil {
  35. return nil, err
  36. }
  37. err := repo.DecryptUserBillingData(userBilling, repo.key)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return userBilling, nil
  42. }
  43. // UpdateUserBilling updates user billing in the db
  44. func (repo *UserBillingRepository) UpdateUserBilling(userBilling *models.UserBilling) (*models.UserBilling, error) {
  45. err := repo.EncryptUserBillingData(userBilling, repo.key)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if err := repo.db.Save(userBilling).Error; err != nil {
  50. return nil, err
  51. }
  52. err = repo.DecryptUserBillingData(userBilling, repo.key)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return userBilling, nil
  57. }
  58. // EncryptUserBillingData will encrypt the user's billing data before writing
  59. // to the DB
  60. func (repo *UserBillingRepository) EncryptUserBillingData(
  61. userBilling *models.UserBilling,
  62. key *[32]byte,
  63. ) error {
  64. if tok := userBilling.Token; len(tok) > 0 {
  65. cipherData, err := encryption.Encrypt(tok, key)
  66. if err != nil {
  67. return err
  68. }
  69. userBilling.Token = cipherData
  70. }
  71. return nil
  72. }
  73. // DecryptUserBillingData will decrypt the user's billing data before returning it
  74. // from the DB
  75. func (repo *UserBillingRepository) DecryptUserBillingData(
  76. userBilling *models.UserBilling,
  77. key *[32]byte,
  78. ) error {
  79. if tok := userBilling.Token; len(tok) > 0 {
  80. plaintext, err := encryption.Decrypt(tok, key)
  81. if err != nil {
  82. return err
  83. }
  84. userBilling.Token = plaintext
  85. }
  86. return nil
  87. }