org_user.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 r == ROLE_EDITOR || r == ROLE_READ_ONLY_EDITOR {
  30. return other != ROLE_ADMIN
  31. }
  32. return r == other
  33. }
  34. func (r *RoleType) UnmarshalJSON(data []byte) error {
  35. var str string
  36. err := json.Unmarshal(data, &str)
  37. if err != nil {
  38. return err
  39. }
  40. *r = RoleType(str)
  41. if (*r).IsValid() == false {
  42. if (*r) != "" {
  43. return errors.New(fmt.Sprintf("JSON validation error: invalid role value: %s", *r))
  44. }
  45. *r = ROLE_VIEWER
  46. }
  47. return nil
  48. }
  49. type OrgUser struct {
  50. Id int64
  51. OrgId int64
  52. UserId int64
  53. Role RoleType
  54. Created time.Time
  55. Updated time.Time
  56. }
  57. // ---------------------
  58. // COMMANDS
  59. type RemoveOrgUserCommand struct {
  60. UserId int64
  61. OrgId int64
  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. Result []*OrgUserDTO
  79. }
  80. // ----------------------
  81. // Projections and DTOs
  82. type OrgUserDTO struct {
  83. OrgId int64 `json:"orgId"`
  84. UserId int64 `json:"userId"`
  85. Email string `json:"email"`
  86. Login string `json:"login"`
  87. Role string `json:"role"`
  88. LastSeenAt time.Time `json:"lastSeenAt"`
  89. LastSeenAtAge string `json:"lastSeenAtAge"`
  90. }