forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler-path-workspace.go
189 lines (174 loc) · 6.79 KB
/
handler-path-workspace.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
/*
* 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 views
import (
"context"
"fmt"
"strings"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/tree"
servicecontext "github.com/pydio/cells/common/service/context"
context2 "github.com/pydio/cells/common/utils/context"
"github.com/pydio/cells/common/utils/permissions"
)
// PathWorkspaceHandler is an AbstractBranchFilter extracting workspace and managing path inside the workspace.
type PathWorkspaceHandler struct {
AbstractBranchFilter
}
func NewPathWorkspaceHandler() *PathWorkspaceHandler {
u := &PathWorkspaceHandler{}
u.inputMethod = u.updateBranchInfo
u.outputMethod = u.updateOutputBranch
return u
}
func (a *PathWorkspaceHandler) extractWs(ctx context.Context, node *tree.Node) (*idm.Workspace, bool, error) {
// Admin context, fake workspace with root ROOT
if admin, a := ctx.Value(ctxAdminContextKey{}).(bool); admin && a {
ws := &idm.Workspace{}
ws.UUID = "ROOT"
ws.RootUUIDs = []string{"ROOT"}
ws.Slug = "ROOT"
return ws, true, nil
}
// User context, folder path must start with /wsId/ or we are listing the root.
if accessList, ok := ctx.Value(CtxUserAccessListKey{}).(*permissions.AccessList); ok {
parts := strings.Split(strings.Trim(node.Path, "/"), "/")
if len(parts) > 0 && len(parts[0]) > 0 {
// Find by slug
for _, ws := range accessList.Workspaces {
if ws.Slug == parts[0] {
node.Path = strings.Join(parts[1:], "/")
return ws, true, nil
}
}
// There is a workspace but it is not in the ACL !
return nil, false, errors.Forbidden("workspace.not.accessible", fmt.Sprintf("Workspace %s is not accessible", parts[0]))
} else {
// Root without workspace part
return nil, false, nil
}
}
return nil, false, nil
}
func (a *PathWorkspaceHandler) updateBranchInfo(ctx context.Context, node *tree.Node, identifier string) (context.Context, *tree.Node, error) {
if info, alreadySet := GetBranchInfo(ctx, identifier); alreadySet && info.Client != nil {
return ctx, node, nil
}
branchInfo := BranchInfo{}
out := node.Clone()
ws, ok, err := a.extractWs(ctx, out)
if err != nil {
return ctx, node, err
} else if ok {
branchInfo.Workspace = *ws
ctx = context2.WithAdditionalMetadata(ctx, map[string]string{
servicecontext.CtxWorkspaceUuid: ws.UUID,
})
return WithBranchInfo(ctx, identifier, branchInfo), out, nil
}
return ctx, node, errors.NotFound(VIEWS_LIBRARY_NAME, "Workspace not found in Path")
}
func (a *PathWorkspaceHandler) updateOutputBranch(ctx context.Context, node *tree.Node, identifier string) (context.Context, *tree.Node, error) {
// Prepend Slug to path
if info, set := GetBranchInfo(ctx, identifier); set && info.UUID != "ROOT" {
out := node.Clone()
out.Path = info.Slug + "/" + node.Path
return ctx, out, nil
}
return ctx, node, nil
}
func (a *PathWorkspaceHandler) ReadNode(ctx context.Context, in *tree.ReadNodeRequest, opts ...client.CallOption) (*tree.ReadNodeResponse, error) {
_, _, err := a.updateBranchInfo(ctx, &tree.Node{Path: in.Node.Path}, "in")
if err != nil {
if errors.Parse(err.Error()).Status == "Not Found" {
// Return a fake root node
return &tree.ReadNodeResponse{Success: true, Node: &tree.Node{Path: ""}}, nil
} else {
return nil, err
}
}
return a.AbstractBranchFilter.ReadNode(ctx, in, opts...)
}
func (a *PathWorkspaceHandler) ListNodes(ctx context.Context, in *tree.ListNodesRequest, opts ...client.CallOption) (tree.NodeProvider_ListNodesClient, error) {
_, _, wsFound := a.updateBranchInfo(ctx, &tree.Node{Path: in.Node.Path}, "in")
if wsFound != nil && errors.Parse(wsFound.Error()).Status == "Not Found" {
// List user workspaces here
accessList, ok := ctx.Value(CtxUserAccessListKey{}).(*permissions.AccessList)
if !ok {
return nil, errors.InternalServerError(VIEWS_LIBRARY_NAME, "Cannot find user workspaces")
}
streamer := NewWrappingStreamer()
go func() {
defer streamer.Close()
wss := accessList.Workspaces
for wsId, wsPermissions := range accessList.GetAccessibleWorkspaces(ctx) {
ws, o := wss[wsId]
if !o {
// This is the case if wsId is "settings" or "homepage" => ignore!
continue
}
if len(ws.RootUUIDs) > 0 {
node := &tree.Node{
Type: tree.NodeType_COLLECTION,
Uuid: ws.RootUUIDs[0],
Path: ws.Slug,
}
// Pass workspace data along in node MetaStore
node.SetMeta(common.MetaFlagWorkspaceScope, ws.Scope.String())
node.SetMeta(common.MetaFlagWorkspacePermissions, wsPermissions)
node.SetMeta(common.MetaFlagWorkspaceLabel, ws.Label)
node.SetMeta(common.MetaFlagWorkspaceDescription, ws.Description)
node.SetMeta(common.MetaFlagWorkspaceSlug, ws.Slug)
node.SetMeta(common.MetaFlagWorkspaceUuid, ws.UUID)
attributes := ws.LoadAttributes()
if common.PackageType == "PydioHome" && ws.Scope == idm.WorkspaceScope_ADMIN {
node.SetMeta(common.MetaFlagWorkspaceSyncable, true)
} else if attributes.AllowSync {
// Trigger a read to make sure that sync is not disabled by policy
if readCtx, readNode, er := a.updateBranchInfo(ctx, node.Clone(), "in"); er == nil {
readNode.SetMeta("acl-check-syncable", true)
if r, e := a.next.ReadNode(readCtx, &tree.ReadNodeRequest{Node: readNode}); e == nil {
var v bool
if r.GetNode().HasMetaKey(common.MetaFlagWorkspaceSyncable) {
r.GetNode().GetMeta(common.MetaFlagWorkspaceSyncable, &v)
} else {
v = true
}
node.SetMeta(common.MetaFlagWorkspaceSyncable, v)
} else {
log.Logger(ctx).Error("Cannot read workspace node during list nodes for workspaces", zap.Error(e))
}
} else {
log.Logger(ctx).Error("Cannot update branch info on workspace node", zap.Error(er))
}
}
streamer.Send(&tree.ListNodesResponse{Node: node})
}
}
}()
return streamer, nil
}
return a.AbstractBranchFilter.ListNodes(ctx, in, opts...)
}