-
Notifications
You must be signed in to change notification settings - Fork 2
/
operator.go
81 lines (68 loc) · 2.34 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
package accountusecase
import (
"github.com/reearth/reearthx/account/accountdomain"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearthx/util"
)
type Operator struct {
User *accountdomain.UserID
ReadableWorkspaces accountdomain.WorkspaceIDList
WritableWorkspaces accountdomain.WorkspaceIDList
OwningWorkspaces accountdomain.WorkspaceIDList
MaintainableWorkspaces accountdomain.WorkspaceIDList
DefaultPolicy *workspace.PolicyID
}
func (o *Operator) Workspaces(r workspace.Role) accountdomain.WorkspaceIDList {
if o == nil {
return nil
}
if r == workspace.RoleReader {
return o.ReadableWorkspaces
}
if r == workspace.RoleWriter {
return o.WritableWorkspaces
}
if r == workspace.RoleMaintainer {
return o.MaintainableWorkspaces
}
if r == workspace.RoleOwner {
return o.OwningWorkspaces
}
return nil
}
func (o *Operator) AllReadableWorkspaces() accountdomain.WorkspaceIDList {
return append(o.ReadableWorkspaces, o.AllWritableWorkspaces()...)
}
func (o *Operator) AllWritableWorkspaces() accountdomain.WorkspaceIDList {
return append(o.WritableWorkspaces, o.AllMaintainingWorkspaces()...)
}
func (o *Operator) AllMaintainingWorkspaces() accountdomain.WorkspaceIDList {
return append(o.MaintainableWorkspaces, o.AllOwningWorkspaces()...)
}
func (o *Operator) AllOwningWorkspaces() accountdomain.WorkspaceIDList {
return o.OwningWorkspaces
}
func (o *Operator) IsReadableWorkspace(ws ...accountdomain.WorkspaceID) bool {
return o.AllReadableWorkspaces().Intersect(ws).Len() > 0
}
func (o *Operator) IsWritableWorkspace(ws ...accountdomain.WorkspaceID) bool {
return o.AllWritableWorkspaces().Intersect(ws).Len() > 0
}
func (o *Operator) IsMaintainingWorkspace(workspace ...accountdomain.WorkspaceID) bool {
return o.AllMaintainingWorkspaces().Intersect(workspace).Len() > 0
}
func (o *Operator) IsOwningWorkspace(ws ...accountdomain.WorkspaceID) bool {
return o.AllOwningWorkspaces().Intersect(ws).Len() > 0
}
func (o *Operator) AddNewWorkspace(ws accountdomain.WorkspaceID) {
o.OwningWorkspaces = append(o.OwningWorkspaces, ws)
}
func (o *Operator) Policy(p *workspace.PolicyID) *workspace.PolicyID {
if p == nil && o.DefaultPolicy != nil && *o.DefaultPolicy != "" {
return util.CloneRef(o.DefaultPolicy)
}
if p != nil && *p == "" {
return nil
}
return p
}