org_user.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. // Typed errors
  7. var (
  8. ErrInvalidRoleType = errors.New("Invalid role type")
  9. ErrLastOrgAdmin = errors.New("Cannot remove last organization admin")
  10. ErrOrgUserNotFound = errors.New("Cannot find the organization user")
  11. ErrOrgUserAlreadyAdded = errors.New("User is already added to organization")
  12. )
  13. type RoleType string
  14. const (
  15. ROLE_VIEWER RoleType = "Viewer"
  16. ROLE_EDITOR RoleType = "Editor"
  17. ROLE_READ_ONLY_EDITOR RoleType = "Read Only Editor"
  18. ROLE_ADMIN RoleType = "Admin"
  19. )
  20. func (r RoleType) IsValid() bool {
  21. return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR || r == ROLE_READ_ONLY_EDITOR
  22. }
  23. func (r RoleType) Includes(other RoleType) bool {
  24. if r == ROLE_ADMIN {
  25. return true
  26. }
  27. if r == ROLE_EDITOR || r == ROLE_READ_ONLY_EDITOR {
  28. return other != ROLE_ADMIN
  29. }
  30. return r == other
  31. }
  32. type OrgUser struct {
  33. Id int64
  34. OrgId int64
  35. UserId int64
  36. Role RoleType
  37. Created time.Time
  38. Updated time.Time
  39. }
  40. // ---------------------
  41. // COMMANDS
  42. type RemoveOrgUserCommand struct {
  43. UserId int64
  44. OrgId int64
  45. }
  46. type AddOrgUserCommand struct {
  47. LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
  48. Role RoleType `json:"role" binding:"Required"`
  49. OrgId int64 `json:"-"`
  50. UserId int64 `json:"-"`
  51. }
  52. type UpdateOrgUserCommand struct {
  53. Role RoleType `json:"role" binding:"Required"`
  54. OrgId int64 `json:"-"`
  55. UserId int64 `json:"-"`
  56. }
  57. // ----------------------
  58. // QUERIES
  59. type GetOrgUsersQuery struct {
  60. OrgId int64
  61. Result []*OrgUserDTO
  62. }
  63. // ----------------------
  64. // Projections and DTOs
  65. type OrgUserDTO struct {
  66. OrgId int64 `json:"orgId"`
  67. UserId int64 `json:"userId"`
  68. Email string `json:"email"`
  69. Login string `json:"login"`
  70. Role string `json:"role"`
  71. }