-
Notifications
You must be signed in to change notification settings - Fork 113
/
user.go
243 lines (216 loc) · 7.67 KB
/
user.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
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package shares
import (
"net/http"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/conversions"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/response"
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/utils"
)
func (h *Handler) createUserShare(w http.ResponseWriter, r *http.Request, statInfo *provider.ResourceInfo, role *conversions.Role, roleVal []byte) (*collaboration.Share, *ocsError) {
ctx := r.Context()
c, err := h.getClient()
if err != nil {
return nil, &ocsError{
Code: response.MetaServerError.StatusCode,
Message: "error getting grpc gateway client",
Error: err,
}
}
shareWith := r.FormValue("shareWith")
if shareWith == "" {
return nil, &ocsError{
Code: response.MetaBadRequest.StatusCode,
Message: "missing shareWith",
Error: err,
}
}
userRes, err := c.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
Claim: "username",
Value: shareWith,
SkipFetchingUserGroups: true,
})
if err != nil {
return nil, &ocsError{
Code: response.MetaServerError.StatusCode,
Message: "error searching recipient",
Error: err,
}
}
if userRes.Status.Code != rpc.Code_CODE_OK {
return nil, &ocsError{
Code: response.MetaNotFound.StatusCode,
Message: "user not found",
Error: err,
}
}
createShareReq := &collaboration.CreateShareRequest{
Opaque: &types.Opaque{
Map: map[string]*types.OpaqueEntry{
"role": {
Decoder: "json",
Value: roleVal,
},
},
},
ResourceInfo: statInfo,
Grant: &collaboration.ShareGrant{
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{UserId: userRes.User.GetId()},
},
Permissions: &collaboration.SharePermissions{
Permissions: role.CS3ResourcePermissions(),
},
},
}
share, ocsErr := h.createCs3Share(ctx, w, r, c, createShareReq)
if ocsErr != nil {
return nil, ocsErr
}
return share, nil
}
func (h *Handler) isUserShare(r *http.Request, oid string) bool {
logger := appctx.GetLogger(r.Context())
client, err := pool.GetGatewayServiceClient(h.gatewayAddr)
if err != nil {
logger.Err(err)
}
getShareRes, err := client.GetShare(r.Context(), &collaboration.GetShareRequest{
Ref: &collaboration.ShareReference{
Spec: &collaboration.ShareReference_Id{
Id: &collaboration.ShareId{
OpaqueId: oid,
},
},
},
})
if err != nil {
logger.Err(err)
return false
}
return getShareRes.GetShare() != nil
}
func (h *Handler) removeUserShare(w http.ResponseWriter, r *http.Request, shareID string) {
ctx := r.Context()
uClient, err := h.getClient()
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error getting grpc gateway client", err)
return
}
shareRef := &collaboration.ShareReference{
Spec: &collaboration.ShareReference_Id{
Id: &collaboration.ShareId{
OpaqueId: shareID,
},
},
}
// Get the share, so that we can include it in the response.
getShareResp, err := uClient.GetShare(ctx, &collaboration.GetShareRequest{Ref: shareRef})
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error sending a grpc delete share request", err)
return
} else if getShareResp.Status.Code != rpc.Code_CODE_OK {
if getShareResp.Status.Code == rpc.Code_CODE_NOT_FOUND {
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "not found", nil)
return
}
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "deleting share failed", err)
return
}
data, err := conversions.CS3Share2ShareData(ctx, getShareResp.Share)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "deleting share failed", err)
return
}
// A deleted share should not have an ID.
data.ID = ""
uReq := &collaboration.RemoveShareRequest{Ref: shareRef}
uRes, err := uClient.RemoveShare(ctx, uReq)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error sending a grpc delete share request", err)
return
}
if uRes.Status.Code != rpc.Code_CODE_OK {
if uRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "not found", nil)
return
}
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "grpc delete share request failed", err)
return
}
response.WriteOCSSuccess(w, r, data)
}
func (h *Handler) listUserShares(r *http.Request, filters []*collaboration.Filter) ([]*conversions.ShareData, *rpc.Status, error) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
lsUserSharesRequest := collaboration.ListSharesRequest{
Filters: filters,
}
ocsDataPayload := make([]*conversions.ShareData, 0)
if h.gatewayAddr != "" {
// get a connection to the users share provider
client, err := h.getClient()
if err != nil {
return ocsDataPayload, nil, err
}
// do list shares request. filtered
lsUserSharesResponse, err := client.ListShares(ctx, &lsUserSharesRequest)
if err != nil {
return ocsDataPayload, nil, err
}
if lsUserSharesResponse.Status.Code != rpc.Code_CODE_OK {
return ocsDataPayload, lsUserSharesResponse.Status, nil
}
// build OCS response payload
for _, s := range lsUserSharesResponse.Shares {
data, err := conversions.CS3Share2ShareData(ctx, s)
if err != nil {
log.Debug().Interface("share", s).Interface("shareData", data).Err(err).Msg("could not CS3Share2ShareData, skipping")
continue
}
info, status, err := h.getResourceInfoByID(ctx, client, s.ResourceId)
if err != nil || status.Code != rpc.Code_CODE_OK {
log.Debug().Interface("share", s).Interface("status", status).Interface("shareData", data).Err(err).Msg("could not stat share, skipping")
continue
}
u := ctxpkg.ContextMustGetUser(ctx)
// check if the user has the permission to list all shares on the resource
if !utils.UserEqual(s.Creator, u.Id) && !info.GetPermissionSet().ListGrants {
log.Debug().Interface("share", s).Interface("user", u).Msg("user has no permission to list all grants and is not the creator of this share")
continue
}
if err := h.addFileInfo(ctx, data, info); err != nil {
log.Debug().Interface("share", s).Interface("info", info).Interface("shareData", data).Err(err).Msg("could not add file info, skipping")
continue
}
h.mapUserIds(ctx, client, data)
log.Debug().Interface("share", s).Interface("info", info).Interface("shareData", data).Msg("mapped")
ocsDataPayload = append(ocsDataPayload, data)
}
}
return ocsDataPayload, nil, nil
}