Skip to content

Commit 84b551c

Browse files
committed
Split up mgr into shareMgr and publicShareMgr
1 parent 641d885 commit 84b551c

File tree

4 files changed

+85
-60
lines changed

4 files changed

+85
-60
lines changed

share/sql/public_link.go

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,75 @@ import (
2929
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
3030
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
3131
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
32+
"github.com/cs3org/reva"
3233
"github.com/cs3org/reva/pkg/appctx"
3334
conversions "github.com/cs3org/reva/pkg/cbox/utils"
3435
"github.com/cs3org/reva/pkg/errtypes"
3536
"github.com/cs3org/reva/pkg/publicshare"
3637
"github.com/cs3org/reva/pkg/utils"
38+
"github.com/cs3org/reva/pkg/utils/cfg"
3739
"github.com/pkg/errors"
3840
"golang.org/x/crypto/bcrypt"
3941
"gorm.io/datatypes"
42+
"gorm.io/driver/mysql"
43+
"gorm.io/driver/sqlite"
4044
"gorm.io/gorm"
4145

4246
// Provides mysql drivers.
4347
_ "github.com/go-sql-driver/mysql"
4448
)
4549

50+
type publicShareMgr struct {
51+
c *config
52+
db *gorm.DB
53+
}
54+
55+
func (publicShareMgr) RevaPlugin() reva.PluginInfo {
56+
return reva.PluginInfo{
57+
ID: "grpc.services.publicshareprovider.drivers.gorm",
58+
New: NewPublicShareManager,
59+
}
60+
}
61+
62+
func NewPublicShareManager(ctx context.Context, m map[string]interface{}) (publicshare.Manager, error) {
63+
var c config
64+
if err := cfg.Decode(m, &c); err != nil {
65+
return nil, err
66+
}
67+
68+
var db *gorm.DB
69+
var err error
70+
switch c.Engine {
71+
case "sqlite":
72+
db, err = gorm.Open(sqlite.Open(c.DBName), &gorm.Config{})
73+
case "mysql":
74+
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", c.DBUsername, c.DBPassword, c.DBHost, c.DBPort, c.DBName)
75+
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
76+
default: // default is mysql
77+
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", c.DBUsername, c.DBPassword, c.DBHost, c.DBPort, c.DBName)
78+
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
79+
}
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
// Migrate schemas
85+
err = db.AutoMigrate(&model.PublicLink{})
86+
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
mgr := &publicShareMgr{
92+
c: &c,
93+
db: db,
94+
}
95+
return mgr, nil
96+
}
97+
4698
// These follow the interface defined in github.com/cs3org/reva/pkg/publishare/publicshare.go
4799

48-
func (m *mgr) CreatePublicShare(ctx context.Context, u *user.User, md *provider.ResourceInfo, g *link.Grant, description string, internal bool, notifyUploads bool, notifyUploadsExtraRecipients string) (*link.PublicShare, error) {
100+
func (m *publicShareMgr) CreatePublicShare(ctx context.Context, u *user.User, md *provider.ResourceInfo, g *link.Grant, description string, internal bool, notifyUploads bool, notifyUploadsExtraRecipients string) (*link.PublicShare, error) {
49101
user := appctx.ContextMustGetUser(ctx)
50102
if user == nil {
51103
return nil, errors.New("no user found in context")
@@ -100,7 +152,7 @@ func (m *mgr) CreatePublicShare(ctx context.Context, u *user.User, md *provider.
100152
return publiclink.AsCS3PublicShare(), nil
101153
}
102154

103-
func (m *mgr) UpdatePublicShare(ctx context.Context, u *user.User, req *link.UpdatePublicShareRequest, g *link.Grant) (*link.PublicShare, error) {
155+
func (m *publicShareMgr) UpdatePublicShare(ctx context.Context, u *user.User, req *link.UpdatePublicShareRequest, g *link.Grant) (*link.PublicShare, error) {
104156
var publiclink *model.PublicLink
105157
var err error
106158

@@ -152,7 +204,7 @@ func (m *mgr) UpdatePublicShare(ctx context.Context, u *user.User, req *link.Upd
152204

153205
}
154206

155-
func (m *mgr) GetPublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference, sign bool) (*link.PublicShare, error) {
207+
func (m *publicShareMgr) GetPublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference, sign bool) (*link.PublicShare, error) {
156208
var ln *model.PublicLink
157209
var err error
158210
switch {
@@ -177,7 +229,7 @@ func (m *mgr) GetPublicShare(ctx context.Context, u *user.User, ref *link.Public
177229
return l, nil
178230
}
179231

180-
func (m *mgr) ListPublicShares(ctx context.Context, u *user.User, filters []*link.ListPublicSharesRequest_Filter, md *provider.ResourceInfo, sign bool) ([]*link.PublicShare, error) {
232+
func (m *publicShareMgr) ListPublicShares(ctx context.Context, u *user.User, filters []*link.ListPublicSharesRequest_Filter, md *provider.ResourceInfo, sign bool) ([]*link.PublicShare, error) {
181233
query := m.db.Model(&model.PublicLink{}).
182234
Where("orphan = ?", false)
183235

@@ -203,7 +255,7 @@ func (m *mgr) ListPublicShares(ctx context.Context, u *user.User, filters []*lin
203255
return cs3links, nil
204256
}
205257

206-
func (m *mgr) RevokePublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference) error {
258+
func (m *publicShareMgr) RevokePublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference) error {
207259
var err error
208260
var publiclink *model.PublicLink
209261
// We need to actually get the link to make sure it is not expired
@@ -222,7 +274,7 @@ func (m *mgr) RevokePublicShare(ctx context.Context, u *user.User, ref *link.Pub
222274

223275
// Get a PublicShare identified by token. This function returns `errtypes.InvalidCredentials` if `auth` does not contain
224276
// a valid password or signature in case the PublicShare is password-protected
225-
func (m *mgr) GetPublicShareByToken(ctx context.Context, token string, auth *link.PublicShareAuthentication, sign bool) (*link.PublicShare, error) {
277+
func (m *publicShareMgr) GetPublicShareByToken(ctx context.Context, token string, auth *link.PublicShareAuthentication, sign bool) (*link.PublicShare, error) {
226278
publiclink, err := m.getLinkByToken(ctx, token)
227279
if err != nil {
228280
return nil, err
@@ -248,7 +300,7 @@ func (m *mgr) GetPublicShareByToken(ctx context.Context, token string, auth *lin
248300
}
249301

250302
// Get Link by ID. Does not return orphans or expired links.
251-
func (m *mgr) getLinkByID(ctx context.Context, id *link.PublicShareId) (*model.PublicLink, error) {
303+
func (m *publicShareMgr) getLinkByID(ctx context.Context, id *link.PublicShareId) (*model.PublicLink, error) {
252304
var link model.PublicLink
253305
res := m.db.First(&link, id.OpaqueId)
254306

@@ -260,7 +312,7 @@ func (m *mgr) getLinkByID(ctx context.Context, id *link.PublicShareId) (*model.P
260312
}
261313

262314
// Get Link by token. Does not return orphans or expired links.
263-
func (m *mgr) getLinkByToken(ctx context.Context, token string) (*model.PublicLink, error) {
315+
func (m *publicShareMgr) getLinkByToken(ctx context.Context, token string) (*model.PublicLink, error) {
264316
if token == "" {
265317
return nil, errors.New("no token provided to getLinkByToken")
266318
}
@@ -321,7 +373,7 @@ func isExpired(l model.PublicLink) bool {
321373
return false
322374
}
323375

324-
func (m *mgr) appendLinkFiltersToQuery(query *gorm.DB, filters []*link.ListPublicSharesRequest_Filter) {
376+
func (m *publicShareMgr) appendLinkFiltersToQuery(query *gorm.DB, filters []*link.ListPublicSharesRequest_Filter) {
325377
// We want to chain filters of different types with AND
326378
// and filters of the same type with OR
327379
// Therefore, we group them by type

share/sql/public_link_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func setupSuiteLinks(tb testing.TB) (publicshare.Manager, error, func(tb testing
2222
"engine": "sqlite",
2323
"db_name": dbName,
2424
}
25-
mgr, err := New(ctx, cfg)
25+
mgr, err := NewPublicShareManager(ctx, cfg)
2626
if err != nil {
2727
os.Remove(dbName)
2828
return nil, err, nil

share/sql/share.go

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ func (shareMgr) RevaPlugin() reva.PluginInfo {
7575
}
7676
}
7777

78-
func (publicShareMgr) RevaPlugin() reva.PluginInfo {
79-
return reva.PluginInfo{
80-
ID: "grpc.services.publicshareprovider.drivers.gorm",
81-
New: NewPublicShareManager,
82-
}
83-
}
84-
8578
type config struct {
8679
Engine string `mapstructure:"engine"` // mysql | sqlite
8780
DBUsername string `mapstructure:"db_username"`
@@ -95,15 +88,6 @@ type config struct {
9588

9689
// Aliased so we can export the right plugin ID
9790
type shareMgr struct {
98-
mgr
99-
}
100-
101-
// Aliased so we can export the right plugin ID
102-
type publicShareMgr struct {
103-
mgr
104-
}
105-
106-
type mgr struct {
10791
c *config
10892
db *gorm.DB
10993
}
@@ -113,18 +97,6 @@ func (c *config) ApplyDefaults() {
11397
}
11498

11599
func NewShareManager(ctx context.Context, m map[string]interface{}) (revashare.Manager, error) {
116-
shareMgr, err := New(ctx, m)
117-
return shareMgr.(revashare.Manager), err
118-
}
119-
120-
func NewPublicShareManager(ctx context.Context, m map[string]interface{}) (publicshare.Manager, error) {
121-
shareMgr, err := New(ctx, m)
122-
return shareMgr.(publicshare.Manager), err
123-
124-
}
125-
126-
// New returns a new ShareAndPublicShareManager.
127-
func New(ctx context.Context, m map[string]interface{}) (ShareAndPublicShareManager, error) {
128100
var c config
129101
if err := cfg.Decode(m, &c); err != nil {
130102
return nil, err
@@ -147,19 +119,20 @@ func New(ctx context.Context, m map[string]interface{}) (ShareAndPublicShareMana
147119
}
148120

149121
// Migrate schemas
150-
err = db.AutoMigrate(&model.Share{}, &model.PublicLink{}, &model.ShareState{})
122+
err = db.AutoMigrate(&model.Share{}, &model.ShareState{})
151123

152124
if err != nil {
153125
return nil, err
154126
}
155127

156-
return &mgr{
128+
mgr := &shareMgr{
157129
c: &c,
158130
db: db,
159-
}, nil
131+
}
132+
return mgr, nil
160133
}
161134

162-
func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collaboration.ShareGrant) (*collaboration.Share, error) {
135+
func (m *shareMgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collaboration.ShareGrant) (*collaboration.Share, error) {
163136
user := appctx.ContextMustGetUser(ctx)
164137

165138
// do not allow share to myself or the owner if share is for a user
@@ -207,7 +180,7 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collabora
207180
}
208181

209182
// Get Share by ID. Does not return orphans.
210-
func (m *mgr) getShareByID(ctx context.Context, id *collaboration.ShareId) (*model.Share, error) {
183+
func (m *shareMgr) getShareByID(ctx context.Context, id *collaboration.ShareId) (*model.Share, error) {
211184
var share model.Share
212185
res := m.db.First(&share, id.OpaqueId)
213186

@@ -219,7 +192,7 @@ func (m *mgr) getShareByID(ctx context.Context, id *collaboration.ShareId) (*mod
219192
}
220193

221194
// Get Share by Key. Does not return orphans.
222-
func (m *mgr) getShareByKey(ctx context.Context, key *collaboration.ShareKey, checkOwner bool) (*model.Share, error) {
195+
func (m *shareMgr) getShareByKey(ctx context.Context, key *collaboration.ShareKey, checkOwner bool) (*model.Share, error) {
223196
owner := conversions.FormatUserID(key.Owner)
224197

225198
var share model.Share
@@ -248,7 +221,7 @@ func (m *mgr) getShareByKey(ctx context.Context, key *collaboration.ShareKey, ch
248221
return &share, nil
249222
}
250223

251-
func (m *mgr) getShare(ctx context.Context, ref *collaboration.ShareReference) (*model.Share, error) {
224+
func (m *shareMgr) getShare(ctx context.Context, ref *collaboration.ShareReference) (*model.Share, error) {
252225
var s *model.Share
253226
var err error
254227
switch {
@@ -283,7 +256,7 @@ func (m *mgr) getShare(ctx context.Context, ref *collaboration.ShareReference) (
283256
return nil, errtypes.NotFound(ref.String())
284257
}
285258

286-
func (m *mgr) GetShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.Share, error) {
259+
func (m *shareMgr) GetShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.Share, error) {
287260
share, err := m.getShare(ctx, ref)
288261
if err != nil {
289262
return nil, err
@@ -295,7 +268,7 @@ func (m *mgr) GetShare(ctx context.Context, ref *collaboration.ShareReference) (
295268
return cs3share, nil
296269
}
297270

298-
func (m *mgr) Unshare(ctx context.Context, ref *collaboration.ShareReference) error {
271+
func (m *shareMgr) Unshare(ctx context.Context, ref *collaboration.ShareReference) error {
299272
var share *model.Share
300273
var err error
301274
if id := ref.GetId(); id != nil {
@@ -310,7 +283,7 @@ func (m *mgr) Unshare(ctx context.Context, ref *collaboration.ShareReference) er
310283
return res.Error
311284
}
312285

313-
func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference, p *collaboration.SharePermissions) (*collaboration.Share, error) {
286+
func (m *shareMgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference, p *collaboration.SharePermissions) (*collaboration.Share, error) {
314287
var share *model.Share
315288
var err error
316289
if id := ref.GetId(); id != nil {
@@ -331,7 +304,7 @@ func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference
331304
return m.GetShare(ctx, ref)
332305
}
333306

334-
func (m *mgr) getPath(ctx context.Context, resID *provider.ResourceId) (string, error) {
307+
func (m *shareMgr) getPath(ctx context.Context, resID *provider.ResourceId) (string, error) {
335308
client, err := pool.GetGatewayServiceClient(pool.Endpoint(m.c.GatewaySvc))
336309
if err != nil {
337310
return "", err
@@ -353,7 +326,7 @@ func (m *mgr) getPath(ctx context.Context, resID *provider.ResourceId) (string,
353326
return "", errors.New(res.Status.Code.String() + ": " + res.Status.Message)
354327
}
355328

356-
func (m *mgr) isProjectAdmin(u *userpb.User, path string) bool {
329+
func (m *shareMgr) isProjectAdmin(u *userpb.User, path string) bool {
357330
if strings.HasPrefix(path, projectPathPrefix) {
358331
// The path will look like /eos/project/c/cernbox, we need to extract the project name
359332
parts := strings.SplitN(path, "/", 6)
@@ -373,7 +346,7 @@ func (m *mgr) isProjectAdmin(u *userpb.User, path string) bool {
373346
return false
374347
}
375348

376-
func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.Share, error) {
349+
func (m *shareMgr) ListShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.Share, error) {
377350
uid := conversions.FormatUserID(appctx.ContextMustGetUser(ctx).Id)
378351

379352
query := m.db.Model(&model.Share{}).
@@ -400,7 +373,7 @@ func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) (
400373
}
401374

402375
// we list the shares that are targeted to the user in context or to the user groups.
403-
func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.ReceivedShare, error) {
376+
func (m *shareMgr) ListReceivedShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.ReceivedShare, error) {
404377
user := appctx.ContextMustGetUser(ctx)
405378

406379
// We need to do this to parse the result
@@ -448,7 +421,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
448421
return receivedShares, nil
449422
}
450423

451-
func (m *mgr) getShareState(ctx context.Context, share *model.Share, user *userpb.User) (*model.ShareState, error) {
424+
func (m *shareMgr) getShareState(ctx context.Context, share *model.Share, user *userpb.User) (*model.ShareState, error) {
452425
var shareState model.ShareState
453426
query := m.db.Model(&shareState).
454427
Where("share_id = ?", share.ID).
@@ -484,7 +457,7 @@ func emptyShareWithId(id string) (*model.Share, error) {
484457
return share, nil
485458
}
486459

487-
func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId, gtype userpb.UserType) (*collaboration.ReceivedShare, error) {
460+
func (m *shareMgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId, gtype userpb.UserType) (*collaboration.ReceivedShare, error) {
488461
user := appctx.ContextMustGetUser(ctx)
489462
share, err := m.getShareByID(ctx, id)
490463
if err != nil {
@@ -500,7 +473,7 @@ func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId, gt
500473
return receivedShare, nil
501474
}
502475

503-
func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey, gtype userpb.UserType) (*collaboration.ReceivedShare, error) {
476+
func (m *shareMgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey, gtype userpb.UserType) (*collaboration.ReceivedShare, error) {
504477
user := appctx.ContextMustGetUser(ctx)
505478
share, err := m.getShareByKey(ctx, key, false)
506479
if err != nil {
@@ -516,7 +489,7 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey,
516489
return receivedShare, nil
517490
}
518491

519-
func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.ReceivedShare, error) {
492+
func (m *shareMgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.ReceivedShare, error) {
520493
var s *collaboration.ReceivedShare
521494
var err error
522495
switch {
@@ -540,7 +513,7 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe
540513
return s, nil
541514
}
542515

543-
func (m *mgr) UpdateReceivedShare(ctx context.Context, recvShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
516+
func (m *shareMgr) UpdateReceivedShare(ctx context.Context, recvShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
544517

545518
user := appctx.ContextMustGetUser(ctx)
546519

@@ -587,7 +560,7 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, recvShare *collaboration.
587560
return rs, nil
588561
}
589562

590-
func (m *mgr) getUserType(ctx context.Context, username string) (userpb.UserType, error) {
563+
func (m *shareMgr) getUserType(ctx context.Context, username string) (userpb.UserType, error) {
591564
client, err := pool.GetGatewayServiceClient(pool.Endpoint(m.c.GatewaySvc))
592565
if err != nil {
593566
return userpb.UserType_USER_TYPE_PRIMARY, err
@@ -606,7 +579,7 @@ func (m *mgr) getUserType(ctx context.Context, username string) (userpb.UserType
606579
return userRes.GetUser().Id.Type, nil
607580
}
608581

609-
func (m *mgr) appendShareFiltersToQuery(query *gorm.DB, filters []*collaboration.Filter) {
582+
func (m *shareMgr) appendShareFiltersToQuery(query *gorm.DB, filters []*collaboration.Filter) {
610583
// We want to chain filters of different types with AND
611584
// and filters of the same type with OR
612585
// Therefore, we group them by type

share/sql/share_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func setupSuiteShares(tb testing.TB) (revashare.Manager, error, func(tb testing.
3030
"engine": "sqlite",
3131
"db_name": dbName,
3232
}
33-
mgr, err := New(ctx, cfg)
33+
mgr, err := NewShareManager(ctx, cfg)
3434
if err != nil {
3535
return nil, err, nil
3636
}

0 commit comments

Comments
 (0)