user.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package models
  2. import (
  3. "github.com/jinzhu/gorm"
  4. )
  5. // User type that extends gorm.Model
  6. type User struct {
  7. gorm.Model
  8. // Unique email for each user
  9. Email,
  10. // Hashed password
  11. Password string
  12. // The clusters that this user has linked
  13. Clusters []ClusterConfig
  14. // The raw kubeconfig uploaded by this user
  15. RawKubeConfig []byte
  16. }
  17. // UserExternal represents the User type that is sent over REST
  18. type UserExternal struct {
  19. ID uint `json:"id"`
  20. Email string `json:"email"`
  21. Clusters []*ClusterConfigExternal `json:"clusters"`
  22. RawKubeConfig string `json:"rawKubeConfig"`
  23. }
  24. // CreateUserForm represents the accepted values for creating a user
  25. type CreateUserForm struct {
  26. Email string `json:"email" form:"required,max=255,email"`
  27. Password string `json:"password" form:"required,max=255"`
  28. }
  29. // UpdateUserForm represents the accepted values for updating a user
  30. type UpdateUserForm struct {
  31. RawKubeConfig string `json:"rawKubeConfig"`
  32. }
  33. // Externalize generates an external User to be shared over REST
  34. func (u *User) Externalize() *UserExternal {
  35. clustersExt := make([]*ClusterConfigExternal, 0)
  36. for _, cluster := range u.Clusters {
  37. clustersExt = append(clustersExt, cluster.Externalize())
  38. }
  39. return &UserExternal{
  40. ID: u.ID,
  41. Email: u.Email,
  42. Clusters: clustersExt,
  43. RawKubeConfig: string(u.RawKubeConfig),
  44. }
  45. }
  46. // ToUser converts a user form to a user
  47. //
  48. // TODO -- PASSWORD HASHING HERE
  49. func (cuf *CreateUserForm) ToUser() (*User, error) {
  50. return &User{
  51. Email: cuf.Email,
  52. Password: cuf.Password,
  53. }, nil
  54. }