-
Notifications
You must be signed in to change notification settings - Fork 173
/
client-front-configs.go
170 lines (155 loc) · 7.17 KB
/
client-front-configs.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
/*
* Copyright (c) 2022. 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 share
import (
"context"
"time"
"github.com/pydio/cells/v4/common/config"
"github.com/pydio/cells/v4/common/nodes"
"github.com/pydio/cells/v4/common/nodes/compose"
"github.com/pydio/cells/v4/common/proto/idm"
"github.com/pydio/cells/v4/common/proto/rest"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/service/errors"
"github.com/pydio/cells/v4/common/utils/configx"
"github.com/pydio/cells/v4/common/utils/permissions"
)
type PluginOptions struct {
MaxExpiration int
MaxDownloads int
HashMinLength int
HashEditable bool
ShareForcePassword bool
enableFilePublicLinks bool
enableFileInternal bool
enableFolderPublicLinks bool
enableFolderInternal bool
}
// CheckLinkOptionsAgainstConfigs loads specific share configurations from ACLs and checks that current link complies with these.
func (sc *Client) CheckLinkOptionsAgainstConfigs(ctx context.Context, link *rest.ShareLink, wss []*tree.WorkspaceRelativePath, files, folders bool) (PluginOptions, error) {
contextParams, e := sc.aclParams(ctx)
if e != nil {
return PluginOptions{}, e
}
options := sc.defaultOptions()
checkScopes := permissions.FrontValuesScopesFromWorkspaceRelativePaths(wss)
options = sc.filterOptionsFromScopes(options, contextParams, checkScopes)
if files && !options.enableFilePublicLinks {
return options, errors.Forbidden("file.public-link.forbidden", "You are not allowed to create public link on files")
}
if folders && !options.enableFolderPublicLinks {
return options, errors.Forbidden("folder.public-link.forbidden", "You are not allowed to create public link on folders")
}
if options.MaxDownloads > 0 && link.MaxDownloads > int64(options.MaxDownloads) {
return options, errors.Forbidden("link.max-downloads.mandatory", "Please set a maximum number of downloads for links")
}
if options.MaxExpiration > 0 && (link.AccessEnd == 0 || (link.AccessEnd-time.Now().Unix()) > int64(options.MaxExpiration*24*60*60)) {
return options, errors.Forbidden("link.max-expiration.mandatory", "Please set a maximum expiration date for links")
}
if options.ShareForcePassword && !link.PasswordRequired {
return options, errors.Forbidden("link.password.required", "Share links must use a password")
}
return options, nil
}
// CheckCellOptionsAgainstConfigs loads specific share configurations from ACLs and checks that current cell complies with these.
func (sc *Client) CheckCellOptionsAgainstConfigs(ctx context.Context, request *rest.PutCellRequest) error {
router := compose.ReverseClient(sc.RuntimeContext)
acl, e := permissions.AccessListFromContextClaims(ctx)
if e != nil {
return e
}
contextParams, e := sc.aclParams(ctx)
if e != nil {
return e
}
options := sc.defaultOptions()
aclWss := acl.GetWorkspaces()
return router.WrapCallback(func(inputFilter nodes.FilterFunc, outputFilter nodes.FilterFunc) error {
for _, n := range request.Room.RootNodes {
var files, folders bool
var wss []*idm.Workspace
_, internal, _ := inputFilter(ctx, n, "in")
for _, ws := range aclWss {
if request.Room.Uuid == ws.UUID { // Ignore current room
continue
}
if _, ok := router.WorkspaceCanSeeNode(ctx, nil, ws, internal); ok {
wss = append(wss, ws)
}
}
if n.IsLeaf() {
files = true
} else {
folders = true
}
scopes := permissions.FrontValuesScopesFromWorkspaces(wss)
loopOptions := sc.filterOptionsFromScopes(options, contextParams, scopes)
if e != nil {
return e
}
if files && !loopOptions.enableFileInternal {
return errors.Forbidden("file.share-internal.forbidden", "You are not allowed to create Cells on files")
}
if folders && !loopOptions.enableFolderInternal {
return errors.Forbidden("folder.share-internal.forbidden", "You are not allowed to create Cells on folders")
}
}
return nil
})
}
func (sc *Client) defaultOptions() PluginOptions {
// Defaults
configParams := config.Get("frontend", "plugin", "action.share")
options := PluginOptions{
MaxExpiration: configParams.Val("FILE_MAX_EXPIRATION").Default(-1).Int(),
MaxDownloads: configParams.Val("FILE_MAX_DOWNLOAD").Default(-1).Int(),
HashMinLength: configParams.Val("HASH_MIN_LENGTH").Default(12).Int(),
HashEditable: configParams.Val("HASH_USER_EDITABLE").Default(true).Bool(),
enableFilePublicLinks: configParams.Val("ENABLE_FILE_PUBLIC_LINK").Default(true).Bool(),
enableFileInternal: configParams.Val("ENABLE_FILE_INTERNAL_SHARING").Default(true).Bool(),
enableFolderPublicLinks: configParams.Val("ENABLE_FOLDER_PUBLIC_LINK").Default(true).Bool(),
enableFolderInternal: configParams.Val("ENABLE_FOLDER_INTERNAL_SHARING").Default(true).Bool(),
ShareForcePassword: configParams.Val("SHARE_FORCE_PASSWORD").Default(false).Bool(),
}
return options
}
func (sc *Client) aclParams(ctx context.Context) (configx.Values, error) {
acl, e := permissions.AccessListFromContextClaims(ctx)
if e != nil {
return nil, e
}
permissions.AccessListLoadFrontValues(ctx, acl)
return acl.FlattenedFrontValues().Val("parameters", "action.share"), nil
}
func (sc *Client) filterOptionsFromScopes(options PluginOptions, contextParams configx.Values, scopes []string) PluginOptions {
// Check expiration time
for _, scope := range scopes {
options.MaxExpiration = contextParams.Val("FILE_MAX_EXPIRATION", scope).Default(options.MaxExpiration).Int()
options.MaxDownloads = contextParams.Val("FILE_MAX_DOWNLOAD", scope).Default(options.MaxDownloads).Int()
options.HashMinLength = contextParams.Val("HASH_MIN_LENGTH", scope).Default(options.HashMinLength).Int()
options.HashEditable = contextParams.Val("HASH_USER_EDITABLE", scope).Default(options.HashEditable).Bool()
options.ShareForcePassword = contextParams.Val("SHARE_FORCE_PASSWORD", scope).Default(options.ShareForcePassword).Bool()
options.enableFilePublicLinks = contextParams.Val("ENABLE_FILE_PUBLIC_LINK", scope).Default(options.enableFilePublicLinks).Bool()
options.enableFileInternal = contextParams.Val("ENABLE_FILE_INTERNAL_SHARING", scope).Default(options.enableFileInternal).Bool()
options.enableFolderPublicLinks = contextParams.Val("ENABLE_FOLDER_PUBLIC_LINK", scope).Default(options.enableFolderPublicLinks).Bool()
options.enableFolderInternal = contextParams.Val("ENABLE_FOLDER_INTERNAL_SHARING", scope).Default(options.enableFolderInternal).Bool()
}
return options
}