-
Notifications
You must be signed in to change notification settings - Fork 172
/
rest.go
345 lines (316 loc) · 10.4 KB
/
rest.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package rest
import (
"context"
"fmt"
"github.com/pydio/cells/v4/common/config"
"path"
"sync"
restful "github.com/emicklei/go-restful/v3"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/anypb"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/auth"
"github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/nodes/compose"
"github.com/pydio/cells/v4/common/proto/activity"
"github.com/pydio/cells/v4/common/proto/idm"
"github.com/pydio/cells/v4/common/proto/rest"
service2 "github.com/pydio/cells/v4/common/proto/service"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/service"
"github.com/pydio/cells/v4/common/utils/permissions"
"github.com/pydio/cells/v4/idm/meta/namespace"
)
type GraphHandler struct {
runtimeContext context.Context
}
// SwaggerTags list the names of the service tags declared in the swagger json implemented by this service
func (h *GraphHandler) SwaggerTags() []string {
return []string{"GraphService"}
}
// Filter returns a function to filter the swagger path
func (h *GraphHandler) Filter() func(string) string {
return nil
}
// UserState is an alias for requests without roleID
func (h *GraphHandler) UserState(req *restful.Request, rsp *restful.Response) {
ctx := req.Request.Context()
log.Logger(ctx).Debug("Received Graph.UserState API request for uuid")
accessList, err := permissions.AccessListFromContextClaims(ctx)
if err != nil {
service.RestError500(req, rsp, err)
return
}
state := &rest.UserStateResponse{
Workspaces: []*idm.Workspace{},
WorkspacesAccesses: make(map[string]string),
}
aW := accessList.GetWorkspaces()
for wsId, right := range accessList.DetectedWsRights(ctx) {
state.WorkspacesAccesses[wsId] = right.String()
if ws, ok := aW[wsId]; ok {
state.Workspaces = append(state.Workspaces, ws)
}
}
rsp.WriteEntity(state)
}
// Relation computes workspaces shared in common, and teams belonging.
func (h *GraphHandler) Relation(req *restful.Request, rsp *restful.Response) {
userName := req.PathParameter("UserId")
ctx := req.Request.Context()
responseObject := &rest.RelationResponse{}
// Find all workspaces in common
contextAccessList, err := permissions.AccessListFromContextClaims(ctx)
if err != nil {
service.RestError500(req, rsp, err)
return
}
targetUserAccessList, _, err := permissions.AccessListFromUser(ctx, userName, false)
if err != nil {
service.RestError500(req, rsp, err)
return
}
// Intersect workspace nodes
contextWorkspaces := contextAccessList.DetectedWsRights(ctx)
targetWorkspaces := targetUserAccessList.DetectedWsRights(ctx)
commonWorkspaces := map[string]string{}
for uWs := range contextWorkspaces {
if _, has := targetWorkspaces[uWs]; has {
commonWorkspaces[uWs] = uWs
}
}
for tWs := range targetWorkspaces {
if _, has := targetWorkspaces[tWs]; has {
commonWorkspaces[tWs] = tWs
}
}
log.Logger(ctx).Debug("Common Workspaces", zap.Any("common", commonWorkspaces), zap.Any("context", contextWorkspaces), zap.Any("target", targetWorkspaces))
wsCli := idm.NewWorkspaceServiceClient(grpc.GetClientConnFromCtx(ctx, common.ServiceWorkspace))
query := &service2.Query{
SubQueries: []*anypb.Any{},
Operation: service2.OperationType_OR,
}
// Cell Workspaces accessible by both users
for wsId := range commonWorkspaces {
q, _ := anypb.New(&idm.WorkspaceSingleQuery{
Uuid: wsId,
Scope: idm.WorkspaceScope_ROOM,
})
query.SubQueries = append(query.SubQueries, q)
}
// Build a ResourcePolicyQuery to restrict next request only to actual visible workspaces
subjects, e := auth.SubjectsForResourcePolicyQuery(ctx, &rest.ResourcePolicyQuery{
Type: rest.ResourcePolicyQuery_CONTEXT,
})
if e != nil {
service.RestErrorDetect(req, rsp, e)
return
}
query.ResourcePolicyQuery = &service2.ResourcePolicyQuery{
Subjects: subjects,
}
log.Logger(ctx).Debug("QUERY", zap.Any("q", query))
if len(query.SubQueries) > 0 {
streamer, e := wsCli.SearchWorkspace(ctx, &idm.SearchWorkspaceRequest{Query: query})
if e != nil {
service.RestError500(req, rsp, e)
return
}
defer streamer.CloseSend()
for {
resp, e := streamer.Recv()
if resp == nil || e != nil {
break
}
responseObject.SharedCells = append(responseObject.SharedCells, resp.Workspace)
}
}
// Load the current user teams, to check if the current user is part of one of them
roleCli := idm.NewRoleServiceClient(grpc.GetClientConnFromCtx(ctx, common.ServiceRole))
var uuids []string
for _, role := range targetUserAccessList.GetRoles() {
uuids = append(uuids, role.Uuid)
}
roleQ, _ := anypb.New(&idm.RoleSingleQuery{
Uuid: uuids,
IsTeam: true,
})
limitSubjects, _ := auth.SubjectsForResourcePolicyQuery(ctx, &rest.ResourcePolicyQuery{Type: rest.ResourcePolicyQuery_CONTEXT})
rStreamer, e := roleCli.SearchRole(ctx, &idm.SearchRoleRequest{
Query: &service2.Query{
SubQueries: []*anypb.Any{roleQ},
ResourcePolicyQuery: &service2.ResourcePolicyQuery{
Subjects: limitSubjects,
},
},
})
if e != nil {
return
}
defer rStreamer.CloseSend()
for {
roleResp, er := rStreamer.Recv()
if er != nil {
break
}
if roleResp == nil {
continue
}
responseObject.BelongsToTeams = append(responseObject.BelongsToTeams, roleResp.Role)
}
rsp.WriteEntity(responseObject)
}
// Recommend computes recommendations for home page by loading activities, bookmarks, and finally workspaces
func (h *GraphHandler) Recommend(req *restful.Request, rsp *restful.Response) {
request := &rest.RecommendRequest{}
if e := req.ReadEntity(request); e != nil {
service.RestError500(req, rsp, e)
return
}
ctx := req.Request.Context()
// Return empty if accessList i s empty
accessList, err := permissions.AccessListFromContextClaims(ctx)
if err != nil || len(accessList.GetWorkspaces()) == 0 {
rsp.WriteEntity(&rest.RecommendResponse{})
return
}
uName, _ := permissions.FindUserNameInContext(ctx)
ak := map[string]struct{}{}
var an, bn []*tree.Node
router := compose.UuidClient(h.runtimeContext)
showActivities := config.Get("services", common.ServiceRestNamespace_+common.ServiceGraph, "recommendations", "activities").Default(true).Bool()
showBookmarks := config.Get("services", common.ServiceRestNamespace_+common.ServiceGraph, "recommendations", "bookmarks").Default(true).Bool()
wg := &sync.WaitGroup{}
if showActivities {
wg.Add(1)
go func() {
defer wg.Done()
// Load activities
ac := activity.NewActivityServiceClient(grpc.GetClientConnFromCtx(h.runtimeContext, common.ServiceActivity))
acReq := &activity.StreamActivitiesRequest{
Context: activity.StreamContext_USER_ID,
ContextData: uName,
PointOfView: activity.SummaryPointOfView_ACTOR,
BoxName: "outbox",
Limit: int64(request.Limit) * 2,
}
if streamer, er := ac.StreamActivities(ctx, acReq); er == nil {
for {
resp, e := streamer.Recv()
if e != nil {
break
}
a := resp.GetActivity()
if a.Object == nil || a.Object.Name == "" {
continue
}
if a.Object.Type == activity.ObjectType_Delete {
continue
}
if a.Object.Type != activity.ObjectType_Document && a.Object.Type != activity.ObjectType_Folder {
continue
}
if _, already := ak[a.Object.Id]; already {
continue
}
n := &tree.Node{Uuid: a.Object.Id}
n.MustSetMeta("reco-annotation", fmt.Sprintf("activity:%d", a.Updated.GetSeconds()))
an = append(an, n)
ak[a.Object.Id] = struct{}{}
}
}
}()
}
if showBookmarks {
wg.Add(1)
go func() {
defer wg.Done()
subjects, e := auth.SubjectsForResourcePolicyQuery(ctx, nil)
if e != nil {
return
}
// Append Subjects
sr := &idm.SearchUserMetaRequest{
Namespace: namespace.ReservedNamespaceBookmark,
ResourceQuery: &service2.ResourcePolicyQuery{
Subjects: subjects,
},
}
userMetaClient := idm.NewUserMetaServiceClient(grpc.GetClientConnFromCtx(ctx, common.ServiceUserMeta))
stream, er := userMetaClient.SearchUserMeta(ctx, sr)
if er != nil {
return
}
for {
resp, e := stream.Recv()
if e != nil {
break
}
if resp == nil {
continue
}
node := &tree.Node{Uuid: resp.GetUserMeta().GetNodeUuid()}
node.MustSetMeta("reco-annotation", "bookmark")
bn = append(bn, node)
}
}()
}
wg.Wait()
resp := &rest.RecommendResponse{}
for _, n := range bn {
if _, already := ak[n.Uuid]; already {
continue
}
an = append(an, n)
}
for _, n := range an {
if r, er := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: n}); er == nil {
node := r.GetNode()
if len(node.AppearsIn) == 0 || node.AppearsIn[0].Path == "" { // empty Path = workspace root
continue
}
node.Path = path.Join(node.AppearsIn[0].WsSlug, node.AppearsIn[0].Path)
node.MustSetMeta("reco-annotation", n.GetStringMeta("reco-annotation"))
node.MustSetMeta("repository_id", node.AppearsIn[0].WsUuid)
resp.Nodes = append(resp.Nodes, node.WithoutReservedMetas())
}
}
// Not enough data, load accessible workspaces as nodes
if len(resp.Nodes) < int(request.Limit) {
pr := compose.PathClient(h.runtimeContext)
_ = pr.ListNodesWithCallback(ctx, &tree.ListNodesRequest{Node: &tree.Node{Path: "/"}}, func(ctx context.Context, node *tree.Node, err error) error {
if err == nil && node.Type != tree.NodeType_UNKNOWN {
if wsu := node.GetStringMeta("ws_uuid"); wsu != "" {
node.MustSetMeta("repository_id", wsu)
node.MustSetMeta("reco-annotation", "workspace")
}
if wsl := node.GetStringMeta("ws_label"); wsl != "" {
node.MustSetMeta("name", wsl)
}
resp.Nodes = append(resp.Nodes, node)
}
return nil
}, false)
}
rsp.WriteEntity(resp)
}