invite.go 909 B

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