user.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package models
  2. import (
  3. "strings"
  4. "gorm.io/gorm"
  5. )
  6. // User type that extends gorm.Model
  7. type User struct {
  8. gorm.Model
  9. Email string `json:"email" gorm:"unique"`
  10. Password string `json:"password"`
  11. Contexts string `json:"contexts"`
  12. RawKubeConfig []byte `json:"rawKubeConfig"`
  13. }
  14. // UserExternal represents the User type that is sent over REST
  15. type UserExternal struct {
  16. ID uint `json:"id"`
  17. Email string `json:"email"`
  18. Contexts []string `json:"contexts"`
  19. RawKubeConfig string `json:"rawKubeConfig"`
  20. }
  21. // Externalize generates an external User to be shared over REST
  22. func (u *User) Externalize() *UserExternal {
  23. return &UserExternal{
  24. ID: u.ID,
  25. Email: u.Email,
  26. Contexts: u.ContextToSlice(),
  27. RawKubeConfig: string(u.RawKubeConfig),
  28. }
  29. }
  30. // ContextToSlice converts the serialized context string to an array of strings
  31. func (u *User) ContextToSlice() []string {
  32. contexts := strings.Split(u.Contexts, ",")
  33. if u.Contexts == "" {
  34. contexts = make([]string, 0)
  35. }
  36. return contexts
  37. }