org_user.go 2.5 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_READ_ONLY_EDITOR RoleType = "Read Only Editor"
  20. ROLE_ADMIN RoleType = "Admin"
  21. )
  22. func (r RoleType) IsValid() bool {
  23. return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR || r == ROLE_READ_ONLY_EDITOR
  24. }
  25. func (r RoleType) Includes(other RoleType) bool {
  26. if r == ROLE_ADMIN {
  27. return true
  28. }
  29. if other == ROLE_READ_ONLY_EDITOR {
  30. return r == ROLE_EDITOR || r == ROLE_READ_ONLY_EDITOR
  31. }
  32. if other == ROLE_EDITOR {
  33. return r == ROLE_EDITOR
  34. }
  35. if other == ROLE_VIEWER {
  36. return r == ROLE_READ_ONLY_EDITOR || r == ROLE_EDITOR || r == ROLE_VIEWER
  37. }
  38. return false
  39. }
  40. func (r *RoleType) UnmarshalJSON(data []byte) error {
  41. var str string
  42. err := json.Unmarshal(data, &str)
  43. if err != nil {
  44. return err
  45. }
  46. *r = RoleType(str)
  47. if (*r).IsValid() == false {
  48. if (*r) != "" {
  49. return errors.New(fmt.Sprintf("JSON validation error: invalid role value: %s", *r))
  50. }
  51. *r = ROLE_VIEWER
  52. }
  53. return nil
  54. }
  55. type OrgUser struct {
  56. Id int64
  57. OrgId int64
  58. UserId int64
  59. Role RoleType
  60. Created time.Time
  61. Updated time.Time
  62. }
  63. // ---------------------
  64. // COMMANDS
  65. type RemoveOrgUserCommand struct {
  66. UserId int64
  67. OrgId int64
  68. }
  69. type AddOrgUserCommand struct {
  70. LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
  71. Role RoleType `json:"role" binding:"Required"`
  72. OrgId int64 `json:"-"`
  73. UserId int64 `json:"-"`
  74. }
  75. type UpdateOrgUserCommand struct {
  76. Role RoleType `json:"role" binding:"Required"`
  77. OrgId int64 `json:"-"`
  78. UserId int64 `json:"-"`
  79. }
  80. // ----------------------
  81. // QUERIES
  82. type GetOrgUsersQuery struct {
  83. OrgId int64
  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. }