forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
228 lines (209 loc) · 6.92 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
/*
* 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 (
"github.com/emicklei/go-restful"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/auth"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/rest"
"github.com/pydio/cells/common/service"
service2 "github.com/pydio/cells/common/service/proto"
"github.com/pydio/cells/common/utils/permissions"
"github.com/pydio/cells/common/views"
)
type GraphHandler struct {
router *views.Router
}
// 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
}
func (h *GraphHandler) getRouter() *views.Router {
if h.router == nil {
h.router = views.NewStandardRouter(views.RouterOptions{WatchRegistry: true})
}
return h.router
}
// 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),
}
accessListWsNodes := accessList.GetWorkspacesNodes()
state.WorkspacesAccesses = accessList.GetAccessibleWorkspaces(ctx)
wsCli := idm.NewWorkspaceServiceClient(common.ServiceGrpcNamespace_+common.ServiceWorkspace, defaults.NewClient())
query := &service2.Query{
SubQueries: []*any.Any{},
Operation: service2.OperationType_OR,
}
for wsId, _ := range state.WorkspacesAccesses {
q, _ := ptypes.MarshalAny(&idm.WorkspaceSingleQuery{
Uuid: wsId,
})
query.SubQueries = append(query.SubQueries, q)
}
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.Close()
for {
resp, e := streamer.Recv()
if resp == nil || e != nil {
break
}
if resp.Workspace != nil {
respWs := resp.Workspace
for nodeId, _ := range accessListWsNodes[respWs.UUID] {
respWs.RootUUIDs = append(respWs.RootUUIDs, nodeId)
}
state.Workspaces = append(state.Workspaces, respWs)
}
}
}
rsp.WriteEntity(state)
}
// Compute 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.GetAccessibleWorkspaces(ctx)
targetWorkspaces := targetUserAccessList.GetAccessibleWorkspaces(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(common.ServiceGrpcNamespace_+common.ServiceWorkspace, defaults.NewClient())
query := &service2.Query{
SubQueries: []*any.Any{},
Operation: service2.OperationType_OR,
}
// Cell Workspaces accessible by both users
for wsId, _ := range commonWorkspaces {
q, _ := ptypes.MarshalAny(&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.Close()
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(common.ServiceGrpcNamespace_+common.ServiceRole, defaults.NewClient())
var uuids []string
for _, role := range targetUserAccessList.OrderedRoles {
uuids = append(uuids, role.Uuid)
}
roleQ, _ := ptypes.MarshalAny(&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: []*any.Any{roleQ},
ResourcePolicyQuery: &service2.ResourcePolicyQuery{
Subjects: limitSubjects,
},
},
})
if e != nil {
return
}
defer rStreamer.Close()
for {
roleResp, er := rStreamer.Recv()
if er != nil {
break
}
if roleResp == nil {
continue
}
responseObject.BelongsToTeams = append(responseObject.BelongsToTeams, roleResp.Role)
}
rsp.WriteEntity(responseObject)
}