auth_code.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "gorm.io/gorm"
  7. )
  8. // AuthCodeRepository uses gorm.DB for querying the database
  9. type AuthCodeRepository struct {
  10. canQuery bool
  11. authCodes []*models.AuthCode
  12. }
  13. // NewAuthCodeRepository returns a AuthCodeRepository which uses
  14. // gorm.DB for querying the database
  15. func NewAuthCodeRepository(canQuery bool) repository.AuthCodeRepository {
  16. return &AuthCodeRepository{canQuery, []*models.AuthCode{}}
  17. }
  18. // CreateAuthCode creates a new invite
  19. func (repo *AuthCodeRepository) CreateAuthCode(a *models.AuthCode) (*models.AuthCode, error) {
  20. if !repo.canQuery {
  21. return nil, errors.New("Cannot write database")
  22. }
  23. repo.authCodes = append(repo.authCodes, a)
  24. a.ID = uint(len(repo.authCodes))
  25. return a, nil
  26. }
  27. // ReadAuthCode gets an auth code object specified by the unique code
  28. func (repo *AuthCodeRepository) ReadAuthCode(code string) (*models.AuthCode, error) {
  29. if !repo.canQuery {
  30. return nil, errors.New("Cannot read from database")
  31. }
  32. var res *models.AuthCode
  33. for _, a := range repo.authCodes {
  34. if code == a.AuthorizationCode {
  35. res = a
  36. }
  37. }
  38. if res == nil {
  39. return nil, gorm.ErrRecordNotFound
  40. }
  41. return res, nil
  42. }