org_user.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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() == false {
  41. if (*r) != "" {
  42. return errors.New(fmt.Sprintf("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. }
  62. type AddOrgUserCommand struct {
  63. LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
  64. Role RoleType `json:"role" binding:"Required"`
  65. OrgId int64 `json:"-"`
  66. UserId int64 `json:"-"`
  67. }
  68. type UpdateOrgUserCommand struct {
  69. Role RoleType `json:"role" binding:"Required"`
  70. OrgId int64 `json:"-"`
  71. UserId int64 `json:"-"`
  72. }
  73. // ----------------------
  74. // QUERIES
  75. type GetOrgUsersQuery struct {
  76. OrgId int64
  77. Query string
  78. Limit int
  79. Result []*OrgUserDTO
  80. }
  81. // ----------------------
  82. // Projections and DTOs
  83. type OrgUserDTO struct {
  84. OrgId int64 `json:"orgId"`
  85. UserId int64 `json:"userId"`
  86. Email string `json:"email"`
  87. AvatarUrl string `json:"avatarUrl"`
  88. Login string `json:"login"`
  89. Role string `json:"role"`
  90. LastSeenAt time.Time `json:"lastSeenAt"`
  91. LastSeenAtAge string `json:"lastSeenAtAge"`
  92. }