This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
operator.go
93 lines (76 loc) · 2.24 KB
/
operator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package usecase
import (
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/scene"
"github.com/reearth/reearth-backend/pkg/user"
)
type Operator struct {
User user.ID
ReadableTeams user.TeamIDList
WritableTeams user.TeamIDList
OwningTeams user.TeamIDList
ReadableScenes scene.IDList
WritableScenes scene.IDList
OwningScenes scene.IDList
}
func (o *Operator) Teams(r user.Role) user.TeamIDList {
if o == nil {
return nil
}
if r == user.RoleReader {
return o.ReadableTeams
}
if r == user.RoleWriter {
return o.WritableTeams
}
if r == user.RoleOwner {
return o.OwningTeams
}
return nil
}
func (o *Operator) AllReadableTeams() user.TeamIDList {
return append(o.ReadableTeams, o.AllWritableTeams()...)
}
func (o *Operator) AllWritableTeams() user.TeamIDList {
return append(o.WritableTeams, o.AllOwningTeams()...)
}
func (o *Operator) AllOwningTeams() user.TeamIDList {
return o.OwningTeams
}
func (o *Operator) IsReadableTeam(team ...id.TeamID) bool {
return o.AllReadableTeams().Intersect(team).Len() > 0
}
func (o *Operator) IsWritableTeam(team ...id.TeamID) bool {
return o.AllWritableTeams().Intersect(team).Len() > 0
}
func (o *Operator) IsOwningTeam(team ...id.TeamID) bool {
return o.AllOwningTeams().Intersect(team).Len() > 0
}
func (o *Operator) AllReadableScenes() scene.IDList {
return append(o.ReadableScenes, o.AllWritableScenes()...)
}
func (o *Operator) AllWritableScenes() scene.IDList {
return append(o.WritableScenes, o.AllOwningScenes()...)
}
func (o *Operator) AllOwningScenes() scene.IDList {
return o.OwningScenes
}
func (o *Operator) IsReadableScene(scene ...id.SceneID) bool {
return o.AllReadableScenes().Has(scene...)
}
func (o *Operator) IsWritableScene(scene ...id.SceneID) bool {
return o.AllWritableScenes().Has(scene...)
}
func (o *Operator) IsOwningScene(scene ...id.SceneID) bool {
return o.AllOwningScenes().Has(scene...)
}
func (o *Operator) AddNewTeam(team id.TeamID) {
o.OwningTeams = append(o.OwningTeams, team)
}
func (o *Operator) AddNewScene(team id.TeamID, scene id.SceneID) {
if o.IsOwningTeam(team) {
o.OwningScenes = append(o.OwningScenes, scene)
} else if o.IsWritableTeam(team) {
o.WritableScenes = append(o.WritableScenes, scene)
}
}