org_user.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if r == ROLE_VIEWER {
  32. return other == ROLE_VIEWER
  33. }
  34. return false
  35. }
  36. func (r *RoleType) UnmarshalJSON(data []byte) error {
  37. var str string
  38. err := json.Unmarshal(data, &str)
  39. if err != nil {
  40. return err
  41. }
  42. *r = RoleType(str)
  43. if !(*r).IsValid() {
  44. if (*r) != "" {
  45. return fmt.Errorf("JSON validation error: invalid role value: %s", *r)
  46. }
  47. *r = ROLE_VIEWER
  48. }
  49. return nil
  50. }
  51. type OrgUser struct {
  52. Id int64
  53. OrgId int64
  54. UserId int64
  55. Role RoleType
  56. Created time.Time
  57. Updated time.Time
  58. }
  59. // ---------------------
  60. // COMMANDS
  61. type RemoveOrgUserCommand struct {
  62. UserId int64
  63. OrgId int64
  64. ShouldDeleteOrphanedUser bool
  65. UserWasDeleted bool
  66. }
  67. type AddOrgUserCommand struct {
  68. LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
  69. Role RoleType `json:"role" binding:"Required"`
  70. OrgId int64 `json:"-"`
  71. UserId int64 `json:"-"`
  72. }
  73. type UpdateOrgUserCommand struct {
  74. Role RoleType `json:"role" binding:"Required"`
  75. OrgId int64 `json:"-"`
  76. UserId int64 `json:"-"`
  77. }
  78. // ----------------------
  79. // QUERIES
  80. type GetOrgUsersQuery struct {
  81. OrgId int64
  82. Query string
  83. Limit int
  84. Result []*OrgUserDTO
  85. }
  86. // ----------------------
  87. // Projections and DTOs
  88. type OrgUserDTO struct {
  89. OrgId int64 `json:"orgId"`
  90. UserId int64 `json:"userId"`
  91. Email string `json:"email"`
  92. AvatarUrl string `json:"avatarUrl"`
  93. Login string `json:"login"`
  94. Role string `json:"role"`
  95. LastSeenAt time.Time `json:"lastSeenAt"`
  96. LastSeenAtAge string `json:"lastSeenAtAge"`
  97. }