org_user.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "time"
  7. )
  8. // Typed errors
  9. var (
  10. ErrInvalidRoleType = errors.New("Invalid role type")
  11. ErrLastOrgAdmin = errors.New("Cannot remove last organization admin")
  12. ErrOrgUserNotFound = errors.New("Cannot find the organization user")
  13. ErrOrgUserAlreadyAdded = errors.New("User is already added to organization")
  14. )
  15. type RoleType string
  16. const (
  17. ROLE_VIEWER RoleType = "Viewer"
  18. ROLE_EDITOR RoleType = "Editor"
  19. ROLE_ADMIN RoleType = "Admin"
  20. )
  21. func (r RoleType) IsValid() bool {
  22. return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR
  23. }
  24. func (r RoleType) Includes(other RoleType) bool {
  25. if r == ROLE_ADMIN {
  26. return true
  27. }
  28. if r == ROLE_EDITOR {
  29. return other != ROLE_ADMIN
  30. }
  31. return false
  32. }
  33. func (r *RoleType) UnmarshalJSON(data []byte) error {
  34. var str string
  35. err := json.Unmarshal(data, &str)
  36. if err != nil {
  37. return err
  38. }
  39. *r = RoleType(str)
  40. if !(*r).IsValid() {
  41. if (*r) != "" {
  42. return fmt.Errorf("JSON validation error: invalid role value: %s", *r)
  43. }
  44. *r = ROLE_VIEWER
  45. }
  46. return nil
  47. }
  48. type OrgUser struct {
  49. Id int64
  50. OrgId int64
  51. UserId int64
  52. Role RoleType
  53. Created time.Time
  54. Updated time.Time
  55. }
  56. // ---------------------
  57. // COMMANDS
  58. type RemoveOrgUserCommand struct {
  59. UserId int64
  60. OrgId int64
  61. ShouldDeleteOrphanedUser bool
  62. }
  63. type AddOrgUserCommand struct {
  64. LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
  65. Role RoleType `json:"role" binding:"Required"`
  66. OrgId int64 `json:"-"`
  67. UserId int64 `json:"-"`
  68. }
  69. type UpdateOrgUserCommand struct {
  70. Role RoleType `json:"role" binding:"Required"`
  71. OrgId int64 `json:"-"`
  72. UserId int64 `json:"-"`
  73. }
  74. // ----------------------
  75. // QUERIES
  76. type GetOrgUsersQuery struct {
  77. OrgId int64
  78. Query string
  79. Limit int
  80. Result []*OrgUserDTO
  81. }
  82. // ----------------------
  83. // Projections and DTOs
  84. type OrgUserDTO struct {
  85. OrgId int64 `json:"orgId"`
  86. UserId int64 `json:"userId"`
  87. Email string `json:"email"`
  88. AvatarUrl string `json:"avatarUrl"`
  89. Login string `json:"login"`
  90. Role string `json:"role"`
  91. LastSeenAt time.Time `json:"lastSeenAt"`
  92. LastSeenAtAge string `json:"lastSeenAtAge"`
  93. }