-
Notifications
You must be signed in to change notification settings - Fork 30
/
platform.go
99 lines (90 loc) · 3.01 KB
/
platform.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
package v1beta1
import (
"context"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/pkg/utils"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/raystack/frontier/internal/bootstrap/schema"
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
)
func (h Handler) AddPlatformUser(ctx context.Context, req *frontierv1beta1.AddPlatformUserRequest) (*frontierv1beta1.AddPlatformUserResponse, error) {
relationName := req.GetRelation()
if !schema.IsPlatformRelation(relationName) {
return nil, grpcBadBodyError
}
if req.GetUserId() != "" {
if err := h.userService.Sudo(ctx, req.GetUserId(), relationName); err != nil {
return nil, err
}
} else if req.GetServiceuserId() != "" {
if err := h.serviceUserService.Sudo(ctx, req.GetServiceuserId(), relationName); err != nil {
return nil, err
}
} else {
return nil, grpcBadBodyError
}
return &frontierv1beta1.AddPlatformUserResponse{}, nil
}
func (h Handler) ListPlatformUsers(ctx context.Context, req *frontierv1beta1.ListPlatformUsersRequest) (*frontierv1beta1.ListPlatformUsersResponse, error) {
logger := grpczap.Extract(ctx)
relations, err := h.relationService.List(ctx, relation.Filter{
Object: relation.Object{
ID: schema.PlatformID,
Namespace: schema.PlatformNamespace,
},
})
if err != nil {
logger.Error(err.Error())
return nil, status.Errorf(codes.Internal, err.Error())
}
// fetch users
userIDs := utils.Map(utils.Filter(relations, func(r relation.Relation) bool {
return r.Subject.Namespace == schema.UserPrincipal
}), func(r relation.Relation) string {
return r.Subject.ID
})
userPBs := make([]*frontierv1beta1.User, 0, len(userIDs))
if len(userIDs) > 0 {
users, err := h.userService.GetByIDs(ctx, userIDs)
if err != nil {
logger.Error(err.Error())
return nil, status.Errorf(codes.Internal, err.Error())
}
for _, u := range users {
userPB, err := transformUserToPB(u)
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
userPBs = append(userPBs, userPB)
}
}
// fetch service users
serviceUserIDs := utils.Map(utils.Filter(relations, func(r relation.Relation) bool {
return r.Subject.Namespace == schema.ServiceUserPrincipal
}), func(r relation.Relation) string {
return r.Subject.ID
})
serviceUserPBs := make([]*frontierv1beta1.ServiceUser, 0, len(serviceUserIDs))
if len(serviceUserIDs) > 0 {
serviceUsers, err := h.serviceUserService.GetByIDs(ctx, serviceUserIDs)
if err != nil {
logger.Error(err.Error())
return nil, status.Errorf(codes.Internal, err.Error())
}
for _, u := range serviceUsers {
serviceUserPB, err := transformServiceUserToPB(u)
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
serviceUserPBs = append(serviceUserPBs, serviceUserPB)
}
}
return &frontierv1beta1.ListPlatformUsersResponse{
Users: userPBs,
Serviceusers: serviceUserPBs,
}, nil
}