-
Notifications
You must be signed in to change notification settings - Fork 32
/
service.go
174 lines (152 loc) · 4.51 KB
/
service.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package policy
import (
"context"
"github.com/raystack/frontier/pkg/utils"
"github.com/raystack/frontier/core/role"
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/internal/bootstrap/schema"
)
type RelationService interface {
Create(ctx context.Context, rel relation.Relation) (relation.Relation, error)
Delete(ctx context.Context, rel relation.Relation) error
}
type RoleService interface {
Get(ctx context.Context, id string) (role.Role, error)
List(ctx context.Context, f role.Filter) ([]role.Role, error)
}
type Service struct {
repository Repository
relationService RelationService
roleService RoleService
}
func NewService(repository Repository, relationService RelationService, roleService RoleService) *Service {
return &Service{
repository: repository,
relationService: relationService,
roleService: roleService,
}
}
func (s Service) Get(ctx context.Context, id string) (Policy, error) {
return s.repository.Get(ctx, id)
}
func (s Service) List(ctx context.Context, f Filter) ([]Policy, error) {
return s.repository.List(ctx, f)
}
func (s Service) Count(ctx context.Context, f Filter) (int64, error) {
return s.repository.Count(ctx, f)
}
func (s Service) Create(ctx context.Context, policy Policy) (Policy, error) {
// check if role exists and get its ID if it was passed by name
policyRole, err := s.roleService.Get(ctx, policy.RoleID)
if err != nil {
return Policy{}, err
}
policy.RoleID = policyRole.ID
createdPolicy, err := s.repository.Upsert(ctx, policy)
if err != nil {
return Policy{}, err
}
if err = s.AssignRole(ctx, createdPolicy); err != nil {
return createdPolicy, err
}
return createdPolicy, err
}
func (s Service) Delete(ctx context.Context, id string) error {
if err := s.relationService.Delete(ctx, relation.Relation{
Object: relation.Object{
ID: id,
Namespace: schema.RoleBindingNamespace,
},
}); err != nil {
return err
}
return s.repository.Delete(ctx, id)
}
// AssignRole Note: ideally this should be in a single transaction
// read more about how user defined roles work in spicedb https://authzed.com/blog/user-defined-roles
func (s Service) AssignRole(ctx context.Context, pol Policy) error {
// bind role with user
subjectSubRelation := ""
if pol.PrincipalType == schema.GroupPrincipal {
subjectSubRelation = schema.MemberRelationName
}
_, err := s.relationService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: pol.ID,
Namespace: schema.RoleBindingNamespace,
},
Subject: relation.Subject{
ID: pol.PrincipalID,
Namespace: pol.PrincipalType,
SubRelationName: subjectSubRelation,
},
RelationName: schema.RoleBearerRelationName,
})
if err != nil {
return err
}
_, err = s.relationService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: pol.ID,
Namespace: schema.RoleBindingNamespace,
},
Subject: relation.Subject{
ID: pol.RoleID,
Namespace: schema.RoleNamespace,
},
RelationName: schema.RoleRelationName,
})
if err != nil {
return err
}
// bind policy to resource
_, err = s.relationService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: pol.ResourceID,
Namespace: pol.ResourceType,
},
Subject: relation.Subject{
ID: pol.ID,
Namespace: schema.RoleBindingNamespace,
},
RelationName: schema.RoleGrantRelationName,
})
if err != nil {
return err
}
return nil
}
// ListRoles lists roles assigned via policies to a user
func (s Service) ListRoles(ctx context.Context, principalType, principalID, objectNamespace, objectID string) ([]role.Role, error) {
flt := Filter{
PrincipalType: principalType,
PrincipalID: principalID,
}
switch objectNamespace {
case schema.OrganizationNamespace:
flt.OrgID = objectID
case schema.ProjectNamespace:
flt.ProjectID = objectID
case schema.GroupNamespace:
flt.GroupID = objectID
}
policies, err := s.List(ctx, flt)
if err != nil {
return nil, err
}
roleIDs := utils.Map(policies, func(p Policy) string {
return p.RoleID
})
return s.roleService.List(ctx, role.Filter{
IDs: roleIDs,
})
}
func (s Service) GroupMemberCount(ctx context.Context, ids []string) ([]MemberCount, error) {
return s.repository.GroupMemberCount(ctx, ids)
}
func (s Service) ProjectMemberCount(ctx context.Context, ids []string) ([]MemberCount, error) {
return s.repository.ProjectMemberCount(ctx, ids)
}
func (s Service) OrgMemberCount(ctx context.Context, id string) (MemberCount, error) {
return s.repository.OrgMemberCount(ctx, id)
}