invite.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // InviteExternal represents the Invite type that is sent over REST
  19. type InviteExternal struct {
  20. ID uint `json:"id"`
  21. Token string `json:"token"`
  22. Expired bool `json:"expired"`
  23. Email string `json:"email"`
  24. Accepted bool `json:"accepted"`
  25. Kind string `json:"kind"`
  26. }
  27. // Externalize generates an external Invite to be shared over REST
  28. func (i *Invite) Externalize() *InviteExternal {
  29. return &InviteExternal{
  30. ID: i.Model.ID,
  31. Token: i.Token,
  32. Email: i.Email,
  33. Expired: i.IsExpired(),
  34. Accepted: i.IsAccepted(),
  35. Kind: i.Kind,
  36. }
  37. }
  38. // ToInviteType generates an external Invite to be shared over REST
  39. func (i *Invite) ToInviteType() *types.Invite {
  40. return &types.Invite{
  41. ID: i.Model.ID,
  42. Token: i.Token,
  43. Email: i.Email,
  44. Expired: i.IsExpired(),
  45. Accepted: i.IsAccepted(),
  46. Kind: i.Kind,
  47. }
  48. }
  49. func (i *Invite) IsExpired() bool {
  50. timeLeft := i.Expiry.Sub(time.Now())
  51. return timeLeft < 0
  52. }
  53. func (i *Invite) IsAccepted() bool {
  54. return i.UserID != 0
  55. }