forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
200 lines (166 loc) · 5.97 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package group
import (
"context"
"fmt"
"strings"
"github.com/goto/salt/log"
"github.com/goto/shield/core/action"
"github.com/goto/shield/core/activity"
"github.com/goto/shield/core/namespace"
"github.com/goto/shield/core/relation"
"github.com/goto/shield/core/user"
"github.com/goto/shield/internal/schema"
pkgctx "github.com/goto/shield/pkg/context"
"github.com/goto/shield/pkg/str"
"github.com/goto/shield/pkg/uuid"
)
const (
auditKeyGroupCreate = "group.create"
auditKeyGroupUpdate = "group.update"
)
type RelationService interface {
Create(ctx context.Context, rel relation.RelationV2) (relation.RelationV2, error)
Delete(ctx context.Context, rel relation.Relation) error
CheckPermission(ctx context.Context, usr user.User, resourceNS namespace.Namespace, resourceIdxa string, action action.Action) (bool, error)
}
type UserService interface {
FetchCurrentUser(ctx context.Context) (user.User, error)
GetByID(ctx context.Context, id string) (user.User, error)
GetByIDs(ctx context.Context, userIDs []string) ([]user.User, error)
}
type ActivityService interface {
Log(ctx context.Context, action string, actor activity.Actor, data any) error
}
type Service struct {
logger log.Logger
repository Repository
relationService RelationService
userService UserService
activityService ActivityService
}
func NewService(logger log.Logger, repository Repository, relationService RelationService, userService UserService, activityService ActivityService) *Service {
return &Service{
logger: logger,
repository: repository,
relationService: relationService,
userService: userService,
activityService: activityService,
}
}
func (s Service) Create(ctx context.Context, grp Group) (Group, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Group{}, err
}
newGroup, err := s.repository.Create(ctx, grp)
if err != nil {
return Group{}, err
}
if err = s.addTeamToOrg(ctx, newGroup); err != nil {
return Group{}, err
}
go func() {
ctx := pkgctx.WithoutCancel(ctx)
groupLogData := newGroup.ToGroupLogData()
actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
if err := s.activityService.Log(ctx, auditKeyGroupCreate, actor, groupLogData); err != nil {
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()
return newGroup, nil
}
func (s Service) Get(ctx context.Context, idOrSlug string) (Group, error) {
if uuid.IsValid(idOrSlug) {
return s.repository.GetByID(ctx, idOrSlug)
}
return s.repository.GetBySlug(ctx, idOrSlug)
}
func (s Service) GetBySlug(ctx context.Context, slug string) (Group, error) {
return s.repository.GetBySlug(ctx, slug)
}
func (s Service) GetByIDs(ctx context.Context, groupIDs []string) ([]Group, error) {
return s.repository.GetByIDs(ctx, groupIDs)
}
func (s Service) List(ctx context.Context, flt Filter) ([]Group, error) {
return s.repository.List(ctx, flt)
}
func (s Service) Update(ctx context.Context, grp Group) (Group, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Group{}, err
}
if strings.TrimSpace(grp.ID) != "" {
return s.repository.UpdateByID(ctx, grp)
}
updatedGroup, err := s.repository.UpdateBySlug(ctx, grp)
if err != nil {
return Group{}, err
}
go func() {
ctx := pkgctx.WithoutCancel(ctx)
groupLogData := updatedGroup.ToGroupLogData()
actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
if err := s.activityService.Log(ctx, auditKeyGroupUpdate, actor, groupLogData); err != nil {
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()
return updatedGroup, nil
}
func (s Service) ListUserGroups(ctx context.Context, userId string, roleId string) ([]Group, error) {
return s.repository.ListUserGroups(ctx, userId, roleId)
}
func (s Service) ListGroupRelations(ctx context.Context, objectId, subjectType, role string) ([]user.User, []Group, map[string][]string, map[string][]string, error) {
relationList, err := s.repository.ListGroupRelations(ctx, objectId, subjectType, role)
if err != nil {
return []user.User{}, []Group{}, map[string][]string{}, map[string][]string{}, fmt.Errorf("%w: %s", ErrListingGroupRelations, err.Error())
}
userIDs := []string{}
groupIDs := []string{}
userIDRoleMap := map[string][]string{}
groupIDRoleMap := map[string][]string{}
users := []user.User{}
groups := []Group{}
for _, relation := range relationList {
if relation.Subject.Namespace == schema.UserPrincipal {
userIDs = append(userIDs, relation.Subject.ID)
userIDRoleMap[relation.Subject.ID] = append(userIDRoleMap[relation.Subject.ID], relation.Subject.RoleID)
} else if relation.Subject.Namespace == schema.GroupPrincipal {
groupIDs = append(groupIDs, relation.Subject.ID)
groupIDRoleMap[relation.Subject.ID] = append(groupIDRoleMap[relation.Subject.ID], relation.Subject.RoleID)
}
}
if len(userIDs) > 0 {
userList, err := s.userService.GetByIDs(ctx, userIDs)
if err != nil {
return []user.User{}, []Group{}, map[string][]string{}, map[string][]string{}, fmt.Errorf("%w: %s", ErrFetchingUsers, err.Error())
}
users = append(users, userList...)
}
if len(groupIDs) > 0 {
groupList, err := s.repository.GetByIDs(ctx, groupIDs)
if err != nil {
return []user.User{}, []Group{}, map[string][]string{}, map[string][]string{}, fmt.Errorf("%w: %s", ErrFetchingGroups, err.Error())
}
groups = append(groups, groupList...)
}
return users, groups, userIDRoleMap, groupIDRoleMap, nil
}
func (s Service) addTeamToOrg(ctx context.Context, team Group) error {
orgId := str.DefaultStringIfEmpty(team.OrganizationID, team.OrganizationID)
rel := relation.RelationV2{
Object: relation.Object{
ID: team.ID,
NamespaceID: schema.GroupNamespace,
},
Subject: relation.Subject{
ID: orgId,
Namespace: schema.OrganizationNamespace,
RoleID: schema.OrganizationRelationName,
},
}
_, err := s.relationService.Create(ctx, rel)
if err != nil {
return err
}
return nil
}