2
0

invite.go 775 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package models
  2. import (
  3. "time"
  4. "gorm.io/gorm"
  5. "github.com/porter-dev/porter/api/types"
  6. )
  7. // Invite type that extends gorm.Model
  8. type Invite struct {
  9. gorm.Model
  10. Token string `gorm:"unique"`
  11. Expiry *time.Time
  12. Email string
  13. // Kind is the role kind that this refers to
  14. Kind string
  15. ProjectID uint
  16. UserID uint
  17. }
  18. // ToInviteType generates an external Invite to be shared over REST
  19. func (i *Invite) ToInviteType() *types.Invite {
  20. return &types.Invite{
  21. ID: i.Model.ID,
  22. Token: i.Token,
  23. Email: i.Email,
  24. Expired: i.IsExpired(),
  25. Accepted: i.IsAccepted(),
  26. Kind: i.Kind,
  27. }
  28. }
  29. func (i *Invite) IsExpired() bool {
  30. timeLeft := i.Expiry.Sub(time.Now())
  31. return timeLeft < 0
  32. }
  33. func (i *Invite) IsAccepted() bool {
  34. return i.UserID != 0
  35. }