dashboard_acl.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. type PermissionType int
  7. const (
  8. PERMISSION_VIEW PermissionType = 1 << iota
  9. PERMISSION_EDIT
  10. PERMISSION_ADMIN
  11. )
  12. func (p PermissionType) String() string {
  13. names := map[int]string{
  14. int(PERMISSION_VIEW): "View",
  15. int(PERMISSION_EDIT): "Edit",
  16. int(PERMISSION_ADMIN): "Admin",
  17. }
  18. return names[int(p)]
  19. }
  20. // Typed errors
  21. var (
  22. ErrDashboardAclInfoMissing = errors.New("User id and team id cannot both be empty for a dashboard permission.")
  23. ErrDashboardPermissionDashboardEmpty = errors.New("Dashboard Id must be greater than zero for a dashboard permission.")
  24. ErrFolderAclInfoMissing = errors.New("User id and team id cannot both be empty for a folder permission.")
  25. ErrFolderPermissionFolderEmpty = errors.New("Folder Id must be greater than zero for a folder permission.")
  26. )
  27. // Dashboard ACL model
  28. type DashboardAcl struct {
  29. Id int64
  30. OrgId int64
  31. DashboardId int64
  32. UserId int64
  33. TeamId int64
  34. Role *RoleType // pointer to be nullable
  35. Permission PermissionType
  36. Created time.Time
  37. Updated time.Time
  38. }
  39. type DashboardAclInfoDTO struct {
  40. OrgId int64 `json:"-"`
  41. DashboardId int64 `json:"dashboardId,omitempty"`
  42. FolderId int64 `json:"folderId,omitempty"`
  43. Created time.Time `json:"created"`
  44. Updated time.Time `json:"updated"`
  45. UserId int64 `json:"userId"`
  46. UserLogin string `json:"userLogin"`
  47. UserEmail string `json:"userEmail"`
  48. UserAvatarUrl string `json:"userAvatarUrl"`
  49. TeamId int64 `json:"teamId"`
  50. TeamEmail string `json:"teamEmail"`
  51. TeamAvatarUrl string `json:"teamAvatarUrl"`
  52. Team string `json:"team"`
  53. Role *RoleType `json:"role,omitempty"`
  54. Permission PermissionType `json:"permission"`
  55. PermissionName string `json:"permissionName"`
  56. Uid string `json:"uid"`
  57. Title string `json:"title"`
  58. Slug string `json:"slug"`
  59. IsFolder bool `json:"isFolder"`
  60. Url string `json:"url"`
  61. Inherited bool `json:"inherited"`
  62. }
  63. func (dto *DashboardAclInfoDTO) hasSameRoleAs(other *DashboardAclInfoDTO) bool {
  64. if dto.Role == nil || other.Role == nil {
  65. return false
  66. }
  67. return dto.UserId <= 0 && dto.TeamId <= 0 && dto.UserId == other.UserId && dto.TeamId == other.TeamId && *dto.Role == *other.Role
  68. }
  69. func (dto *DashboardAclInfoDTO) hasSameUserAs(other *DashboardAclInfoDTO) bool {
  70. return dto.UserId > 0 && dto.UserId == other.UserId
  71. }
  72. func (dto *DashboardAclInfoDTO) hasSameTeamAs(other *DashboardAclInfoDTO) bool {
  73. return dto.TeamId > 0 && dto.TeamId == other.TeamId
  74. }
  75. // IsDuplicateOf returns true if other item has same role, same user or same team
  76. func (dto *DashboardAclInfoDTO) IsDuplicateOf(other *DashboardAclInfoDTO) bool {
  77. return dto.hasSameRoleAs(other) || dto.hasSameUserAs(other) || dto.hasSameTeamAs(other)
  78. }
  79. //
  80. // COMMANDS
  81. //
  82. type UpdateDashboardAclCommand struct {
  83. DashboardId int64
  84. Items []*DashboardAcl
  85. }
  86. //
  87. // QUERIES
  88. //
  89. type GetDashboardAclInfoListQuery struct {
  90. DashboardId int64
  91. OrgId int64
  92. Result []*DashboardAclInfoDTO
  93. }