2
0

user.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package forms
  2. import (
  3. "strings"
  4. "github.com/porter-dev/porter/internal/kubernetes"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/internal/repository"
  7. "golang.org/x/crypto/bcrypt"
  8. "gorm.io/gorm"
  9. )
  10. // WriteUserForm is a generic form for write operations to the User model
  11. type WriteUserForm interface {
  12. ToUser(repo repository.UserRepository) (*models.User, error)
  13. }
  14. // CreateUserForm represents the accepted values for creating a user
  15. type CreateUserForm struct {
  16. WriteUserForm
  17. Email string `json:"email" form:"required,max=255,email"`
  18. Password string `json:"password" form:"required,max=255"`
  19. }
  20. // ToUser converts a CreateUserForm to models.User
  21. func (cuf *CreateUserForm) ToUser(_ repository.UserRepository) (*models.User, error) {
  22. hashed, err := bcrypt.GenerateFromPassword([]byte(cuf.Password), 8)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &models.User{
  27. Email: cuf.Email,
  28. Password: string(hashed),
  29. }, nil
  30. }
  31. // LoginUserForm represents the accepted values for logging a user in
  32. type LoginUserForm struct {
  33. WriteUserForm
  34. ID uint `form:"required"`
  35. Email string `json:"email" form:"required,max=255,email"`
  36. Password string `json:"password" form:"required,max=255"`
  37. }
  38. // ToUser converts a LoginUserForm to models.User
  39. func (luf *LoginUserForm) ToUser(_ repository.UserRepository) (*models.User, error) {
  40. hashed, err := bcrypt.GenerateFromPassword([]byte(luf.Password), 8)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &models.User{
  45. Email: luf.Email,
  46. Password: string(hashed),
  47. }, nil
  48. }
  49. // UpdateUserForm represents the accepted values for updating a user
  50. //
  51. // ID is a query parameter, the other two are sent in JSON body
  52. type UpdateUserForm struct {
  53. WriteUserForm
  54. ID uint `form:"required"`
  55. RawKubeConfig string `json:"rawKubeConfig,omitempty"`
  56. AllowedContexts []string `json:"allowedContexts"`
  57. }
  58. // ToUser converts an UpdateUserForm to models.User by parsing the kubeconfig
  59. // and the allowed clusters to generate a list of ClusterConfigs.
  60. func (uuf *UpdateUserForm) ToUser(repo repository.UserRepository) (*models.User, error) {
  61. rawBytes := []byte(uuf.RawKubeConfig)
  62. contexts := uuf.AllowedContexts
  63. savedUser, err := repo.ReadUser(uuf.ID)
  64. if err != nil {
  65. return nil, err
  66. }
  67. // if the rawKubeConfig is empty, query the DB for a non-empty one
  68. if uuf.RawKubeConfig == "" {
  69. rawBytes = savedUser.RawKubeConfig
  70. }
  71. // if the allowedContexts is nil, query the DB for a non-nil one
  72. if uuf.AllowedContexts == nil {
  73. contexts = savedUser.ContextToSlice()
  74. }
  75. if len(rawBytes) > 0 {
  76. // validate the kubeconfig
  77. _contexts, err := kubernetes.GetContextsFromBytes(rawBytes, contexts)
  78. if err != nil {
  79. return nil, err
  80. }
  81. contexts = make([]string, 0)
  82. // ensure only joined contexts get written
  83. for _, context := range _contexts {
  84. if context.Selected {
  85. contexts = append(contexts, context.Name)
  86. }
  87. }
  88. }
  89. contextsJoin := strings.Join(contexts, ",")
  90. return &models.User{
  91. Model: gorm.Model{
  92. ID: uuf.ID,
  93. },
  94. Contexts: contextsJoin,
  95. RawKubeConfig: rawBytes,
  96. }, nil
  97. }
  98. // DeleteUserForm represents the accepted values for deleting a user
  99. type DeleteUserForm struct {
  100. WriteUserForm
  101. ID uint `form:"required"`
  102. Password string `json:"password" form:"required,max=255"`
  103. }
  104. // ToUser converts a DeleteUserForm to models.User using the user ID
  105. func (uuf *DeleteUserForm) ToUser(_ repository.UserRepository) (*models.User, error) {
  106. return &models.User{
  107. Model: gorm.Model{
  108. ID: uuf.ID,
  109. },
  110. }, nil
  111. }