/
user_impl.go
429 lines (346 loc) · 11.3 KB
/
user_impl.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package scheduler
import (
"context"
"fmt"
"time"
"github.com/Filecoin-Titan/titan/api"
"github.com/Filecoin-Titan/titan/api/terrors"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/node/cidutil"
"github.com/Filecoin-Titan/titan/node/handler"
"github.com/Filecoin-Titan/titan/node/scheduler/user"
"github.com/filecoin-project/go-jsonrpc/auth"
"golang.org/x/xerrors"
)
const (
userAssetGroupMaxCount = 20
rootGroup = 0
)
// UserAPIKeysExists checks if the user exists.
func (s *Scheduler) UserAPIKeysExists(ctx context.Context, userID string) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
u := s.newUser(userID)
keys, err := u.GetAPIKeys(ctx)
if err != nil {
return err
}
if len(keys) == 0 {
return fmt.Errorf("user %s api keys not exist", userID)
}
return nil
}
// AllocateStorage allocates storage space.
func (s *Scheduler) AllocateStorage(ctx context.Context, userID string) (*types.UserInfo, error) {
u := s.newUser(userID)
info, err := u.AllocateStorage(ctx, s.SchedulerCfg.UserFreeStorageSize)
if err != nil {
return nil, xerrors.Errorf("AllocateStorage err:%s", err.Error())
}
return info, nil
}
// GetUserInfo get user info
func (s *Scheduler) GetUserInfo(ctx context.Context, userID string) (*types.UserInfo, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
return s.loadUserInfo(userID)
}
// GetUserInfos get user infos
func (s *Scheduler) GetUserInfos(ctx context.Context, userIDs []string) (map[string]*types.UserInfo, error) {
out := make(map[string]*types.UserInfo, 0)
for _, userID := range userIDs {
info, err := s.loadUserInfo(userID)
if err != nil {
continue
}
out[userID] = info
}
return out, nil
}
func (s *Scheduler) loadUserInfo(userID string) (*types.UserInfo, error) {
u := s.newUser(userID)
return u.GetInfo()
}
// CreateAPIKey creates a key for the client API.
func (s *Scheduler) CreateAPIKey(ctx context.Context, userID, keyName string, perms []types.UserAccessControl) (string, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
u := s.newUser(userID)
info, err := u.CreateAPIKey(ctx, keyName, perms, s.SchedulerCfg, s.CommonAPI)
if err != nil {
return "", err
}
return info, nil
}
// GetAPIKeys get all api key for user.
func (s *Scheduler) GetAPIKeys(ctx context.Context, userID string) (map[string]types.UserAPIKeysInfo, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
u := s.newUser(userID)
info, err := u.GetAPIKeys(ctx)
if err != nil {
return nil, err
}
return info, nil
}
func (s *Scheduler) DeleteAPIKey(ctx context.Context, userID, name string) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
u := s.newUser(userID)
return u.DeleteAPIKey(ctx, name)
}
func (s *Scheduler) UpdateShareStatus(ctx context.Context, userID, assetCID string) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
u := s.newUser(userID)
return u.SetAssetAtShareStatus(ctx, assetCID)
}
func (s *Scheduler) newUser(userID string) *user.User {
return &user.User{ID: userID, SQLDB: s.AssetManager.SQLDB, Manager: s.AssetManager}
}
// UserAssetDownloadResult download result
func (s *Scheduler) UserAssetDownloadResult(ctx context.Context, userID, cid string, totalTraffic, peakBandwidth int64) error {
nodeID := handler.GetNodeID(ctx)
cNode := s.NodeManager.GetNode(nodeID)
if cNode == nil {
return xerrors.Errorf("UserAssetDownloadResult node not found: %s", nodeID)
}
err := s.db.UpdateUserInfo(userID, totalTraffic, 1)
if err != nil {
return err
}
return s.db.UpdateUserPeakSize(userID, peakBandwidth)
}
func (s *Scheduler) SetUserVIP(ctx context.Context, userID string, enableVIP bool) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
storageSize := s.SchedulerCfg.UserFreeStorageSize
if enableVIP {
storageSize = s.SchedulerCfg.UserVipStorageSize
}
return s.db.UpdateUserVIPAndStorageSize(userID, enableVIP, storageSize)
}
func (s *Scheduler) GetUserAccessToken(ctx context.Context, userID string) (string, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
_, err := s.GetUserInfo(ctx, userID)
if err != nil {
return "", err
}
payload := types.JWTPayload{ID: userID, Allow: []auth.Permission{api.RoleUser}, AccessControlList: types.UserAccessControlAll}
tk, err := s.AuthNew(ctx, &payload)
if err != nil {
return "", err
}
return tk, nil
}
// GetUserStorageStats get user storage info
func (s *Scheduler) GetUserStorageStats(ctx context.Context, userID string) (*types.StorageStats, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
return s.db.LoadStorageStatsOfUser(userID)
}
// ListUserStorageStats list storage info
func (s *Scheduler) ListUserStorageStats(ctx context.Context, limit, offset int) (*types.ListStorageStatsRsp, error) {
return s.db.ListStorageStatsOfUsers(limit, offset)
}
// CreateAssetGroup create file group
func (s *Scheduler) CreateAssetGroup(ctx context.Context, userID, name string, parent int) (*types.AssetGroup, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
if parent != rootGroup {
exist, err := s.db.AssetGroupExists(userID, parent)
if err != nil {
return nil, err
}
if !exist {
return nil, &api.ErrWeb{Code: terrors.GroupNotExist.Int(), Message: fmt.Sprintf("CreateAssetGroup failed, group parent [%d] is not exist ", parent)}
}
}
count, err := s.db.GetAssetGroupCount(userID)
if err != nil {
return nil, err
}
if count >= userAssetGroupMaxCount {
return nil, &api.ErrWeb{Code: terrors.GroupLimit.Int(), Message: fmt.Sprintf("CreateAssetGroup failed, Exceed the limit %d", userAssetGroupMaxCount)}
}
info, err := s.db.CreateAssetGroup(&types.AssetGroup{UserID: userID, Parent: parent, Name: name})
if err != nil {
return nil, err
}
return info, nil
}
// ListAssetGroup list file group
func (s *Scheduler) ListAssetGroup(ctx context.Context, userID string, parent, limit int, offset int) (*types.ListAssetGroupRsp, error) {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
return s.db.ListAssetGroupForUser(userID, parent, limit, offset)
}
// ListAssetSummary list file group
func (s *Scheduler) ListAssetSummary(ctx context.Context, userID string, parent, limit, offset int) (*types.ListAssetSummaryRsp, error) {
startTime := time.Now()
defer func() {
log.Debugf("ListAssetSummary [userID:%s,parent:%d,limit:%d,offset:%d] request time:%s", userID, parent, limit, offset, time.Since(startTime))
}()
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
out := new(types.ListAssetSummaryRsp)
// list group
groupRsp, err := s.db.ListAssetGroupForUser(userID, parent, limit, offset)
if err != nil {
return nil, xerrors.Errorf("ListAssetGroupForUser err:%s", err.Error())
}
for _, group := range groupRsp.AssetGroups {
i := new(types.UserAssetSummary)
i.AssetGroup = group
out.List = append(out.List, i)
}
out.Total += groupRsp.Total
aLimit := limit - len(groupRsp.AssetGroups)
if aLimit < 0 {
aLimit = 0
}
aOffset := offset - groupRsp.Total
if aOffset < 0 {
aOffset = 0
}
u := s.newUser(userID)
assetRsp, err := u.ListAssets(ctx, aLimit, aOffset, s.SchedulerCfg.MaxCountOfVisitShareLink, parent)
if err != nil {
return nil, xerrors.Errorf("ListAssets err:%s", err.Error())
}
for _, asset := range assetRsp.AssetOverviews {
i := new(types.UserAssetSummary)
i.AssetOverview = asset
out.List = append(out.List, i)
}
out.Total += assetRsp.Total
return out, nil
}
// DeleteAssetGroup delete asset group
func (s *Scheduler) DeleteAssetGroup(ctx context.Context, userID string, gid int) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
gCount, err := s.db.GetUserAssetCountByGroupID(userID, gid)
if err != nil {
return err
}
if gCount > 0 {
return &api.ErrWeb{Code: terrors.GroupNotEmptyCannotBeDelete.Int(), Message: "There are assets in the group and the group cannot be deleted"}
}
rsp, err := s.db.ListAssetGroupForUser(userID, gid, 1, 0)
if err != nil {
return err
}
if rsp.Total > 0 {
return &api.ErrWeb{Code: terrors.GroupNotEmptyCannotBeDelete.Int(), Message: "There are assets in the group and the group cannot be deleted"}
}
return s.db.DeleteAssetGroup(userID, gid)
}
// RenameAssetGroup rename group
func (s *Scheduler) RenameAssetGroup(ctx context.Context, userID, newName string, groupID int) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
return s.db.UpdateAssetGroupName(userID, newName, groupID)
}
// MoveAssetToGroup move a file to group
func (s *Scheduler) MoveAssetToGroup(ctx context.Context, userID, cid string, groupID int) error {
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
hash, err := cidutil.CIDToHash(cid)
if err != nil {
return err
}
return s.db.UpdateAssetGroup(hash, userID, groupID)
}
// MoveAssetGroup move a asset group
func (s *Scheduler) MoveAssetGroup(ctx context.Context, userID string, groupID, targetGroupID int) error {
startTime := time.Now()
defer func() {
log.Debugf("MoveAssetGroup [userID:%s,gid:%d,targetGroupID:%d] request time:%s", userID, groupID, targetGroupID, time.Since(startTime))
}()
uID := handler.GetUserID(ctx)
if len(uID) > 0 {
userID = uID
}
if groupID == rootGroup {
return &api.ErrWeb{Code: terrors.RootGroupCannotMoved.Int(), Message: "the root group cannot be moved"}
}
if groupID == targetGroupID {
return &api.ErrWeb{Code: terrors.GroupsAreSame.Int(), Message: "groups are the same"}
}
if targetGroupID != rootGroup {
exist, err := s.db.AssetGroupExists(userID, targetGroupID)
if err != nil {
return err
}
if !exist {
return &api.ErrWeb{Code: terrors.GroupNotExist.Int(), Message: fmt.Sprintf("MoveAssetGroup failed, group parent [%d] is not exist ", targetGroupID)}
}
// Prevent loops
gid := targetGroupID
for {
gid, err = s.db.GetAssetGroupParent(gid)
if err != nil {
return err
}
if gid == groupID {
return &api.ErrWeb{Code: terrors.CannotMoveToSubgroup.Int(), Message: "cannot move to subgroup"}
}
if gid == rootGroup {
break
}
}
}
return s.db.UpdateAssetGroupParent(userID, groupID, targetGroupID)
}
// GetAPPKeyPermissions get the permission of user app key
func (s *Scheduler) GetAPPKeyPermissions(ctx context.Context, userID string, keyName string) ([]string, error) {
keyMap, err := s.GetAPIKeys(ctx, userID)
if err != nil {
return nil, err
}
key, ok := keyMap[keyName]
if !ok {
return nil, &api.ErrWeb{Code: terrors.APPKeyNotFound.Int(), Message: fmt.Sprintf("the API key %s already exist", keyName)}
}
payload, err := s.AuthVerify(ctx, key.APIKey)
if err != nil {
return nil, err
}
permissions := make([]string, 0, len(payload.AccessControlList))
for _, accessControl := range payload.AccessControlList {
permissions = append(permissions, string(accessControl))
}
return permissions, nil
}