Skip to content

Commit 8932e38

Browse files
committed
Enhance the cbox share sql driver to store accepted group shares
1 parent a82f3d0 commit 8932e38

File tree

3 files changed

+44
-53
lines changed

3 files changed

+44
-53
lines changed

Diff for: changelog/unreleased/cbox-share-status.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Enhancement: Enhance the cbox share sql driver to store accepted group shares
2+
3+
https://github.com/cs3org/reva/pull/2211

Diff for: pkg/cbox/share/sql/sql.go

+37-43
Original file line numberDiff line numberDiff line change
@@ -283,29 +283,25 @@ func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference
283283
func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.Share, error) {
284284
uid := conversions.FormatUserID(ctxpkg.ContextMustGetUser(ctx).Id)
285285
query := `select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with,
286-
coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, id, stime, permissions, share_type
287-
FROM oc_share
288-
WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner=? or uid_initiator=?)`
286+
coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type,
287+
id, stime, permissions, share_type
288+
FROM oc_share
289+
WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner=? or uid_initiator=?)`
289290
params := []interface{}{uid, uid}
290-
var (
291-
filterQuery string
292-
filterParams []interface{}
293-
err error
294-
)
291+
295292
if len(filters) == 0 {
296-
filterQuery += "(share_type=? OR share_type=?)"
293+
query += " AND (share_type=? OR share_type=?)"
297294
params = append(params, shareTypeUser)
298295
params = append(params, shareTypeGroup)
299296
} else {
300-
filterQuery, filterParams, err = translateFilters(filters)
297+
filterQuery, filterParams, err := translateFilters(filters)
301298
if err != nil {
302299
return nil, err
303300
}
304301
params = append(params, filterParams...)
305-
}
306-
307-
if filterQuery != "" {
308-
query = fmt.Sprintf("%s AND (%s)", query, filterQuery)
302+
if filterQuery != "" {
303+
query = fmt.Sprintf("%s AND (%s)", query, filterQuery)
304+
}
309305
}
310306

311307
rows, err := m.db.Query(query, params...)
@@ -340,10 +336,10 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
340336
}
341337

342338
query := `SELECT coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with,
343-
coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, ts.id, stime,
344-
permissions, share_type, accepted, coalesce(tr.rejected_by, '') as rejected_by
345-
FROM oc_share ts LEFT JOIN oc_share_acl tr ON (ts.id = tr.id AND tr.rejected_by = ?)
346-
WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner != ? AND uid_initiator != ?)`
339+
coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type,
340+
ts.id, stime, permissions, share_type, coalesce(tr.state, 0) as state
341+
FROM oc_share ts LEFT JOIN oc_share_status tr ON (ts.id = tr.id AND tr.recipient = ?)
342+
WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner != ? AND uid_initiator != ?)`
347343
if len(user.Groups) > 0 {
348344
query += " AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))"
349345
} else {
@@ -369,7 +365,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
369365
var s conversions.DBShare
370366
shares := []*collaboration.ReceivedShare{}
371367
for rows.Next() {
372-
if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.RejectedBy); err != nil {
368+
if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil {
373369
continue
374370
}
375371
shares = append(shares, conversions.ConvertToCS3ReceivedShare(s))
@@ -391,13 +387,17 @@ func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId) (*
391387
}
392388

393389
s := conversions.DBShare{ID: id.OpaqueId}
394-
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, stime, permissions, share_type, accepted, coalesce(tr.rejected_by, '') as rejected_by FROM oc_share ts LEFT JOIN oc_share_acl tr ON (ts.id = tr.id AND tr.rejected_by = ?) WHERE (orphan = 0 or orphan IS NULL) AND ts.id=? "
390+
query := `select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with,
391+
coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type,
392+
stime, permissions, share_type, coalesce(tr.state, 0) as state
393+
FROM oc_share ts LEFT JOIN oc_share_status tr ON (ts.id = tr.id AND tr.recipient = ?)
394+
WHERE (orphan = 0 or orphan IS NULL) AND ts.id=?`
395395
if len(user.Groups) > 0 {
396-
query += "AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))"
396+
query += " AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))"
397397
} else {
398-
query += "AND (share_with=? AND share_type = 0)"
398+
query += " AND (share_with=? AND share_type = 0)"
399399
}
400-
if err := m.db.QueryRow(query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.RejectedBy); err != nil {
400+
if err := m.db.QueryRow(query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil {
401401
if err == sql.ErrNoRows {
402402
return nil, errtypes.NotFound(id.OpaqueId)
403403
}
@@ -417,14 +417,18 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey)
417417
}
418418

419419
s := conversions.DBShare{}
420-
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, ts.id, stime, permissions, share_type, accepted, coalesce(tr.rejected_by, '') as rejected_by FROM oc_share ts LEFT JOIN oc_share_acl tr ON (ts.id = tr.id AND tr.rejected_by = ?) WHERE (orphan = 0 or orphan IS NULL) AND uid_owner=? AND fileid_prefix=? AND item_source=? AND share_type=? AND share_with=? "
420+
query := `select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with,
421+
coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type,
422+
ts.id, stime, permissions, share_type, coalesce(tr.state, 0) as state
423+
FROM oc_share ts LEFT JOIN oc_share_status tr ON (ts.id = tr.id AND tr.recipient = ?)
424+
WHERE (orphan = 0 or orphan IS NULL) AND uid_owner=? AND fileid_prefix=? AND item_source=? AND share_type=? AND share_with=?`
421425
if len(user.Groups) > 0 {
422-
query += "AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))"
426+
query += " AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))"
423427
} else {
424-
query += "AND (share_with=? AND share_type = 0)"
428+
query += " AND (share_with=? AND share_type = 0)"
425429
}
426430

427-
if err := m.db.QueryRow(query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.RejectedBy); err != nil {
431+
if err := m.db.QueryRow(query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil {
428432
if err == sql.ErrNoRows {
429433
return nil, errtypes.NotFound(key.String())
430434
}
@@ -470,16 +474,17 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.Rece
470474
}
471475
}
472476

473-
var query, queryAccept string
474-
params := []interface{}{rs.Share.Id.OpaqueId, conversions.FormatUserID(user.Id)}
477+
state := 0
475478
switch rs.GetState() {
476479
case collaboration.ShareState_SHARE_STATE_REJECTED:
477-
query = "insert into oc_share_acl(id, rejected_by) values(?, ?)"
480+
state = -1
478481
case collaboration.ShareState_SHARE_STATE_ACCEPTED:
479-
query = "delete from oc_share_acl where id=? AND rejected_by=?"
480-
queryAccept = "update oc_share set accepted=1 where id=?"
482+
state = 1
481483
}
482484

485+
params := []interface{}{rs.Share.Id.OpaqueId, conversions.FormatUserID(user.Id), state, state}
486+
query := "insert into oc_share_status(id, recipient, state) values(?, ?, ?) ON DUPLICATE KEY UPDATE state = ?"
487+
483488
stmt, err := m.db.Prepare(query)
484489
if err != nil {
485490
return nil, err
@@ -489,17 +494,6 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.Rece
489494
return nil, err
490495
}
491496

492-
if queryAccept != "" {
493-
stmt, err = m.db.Prepare(queryAccept)
494-
if err != nil {
495-
return nil, err
496-
}
497-
_, err = stmt.Exec(rs.Share.Id.OpaqueId)
498-
if err != nil {
499-
return nil, err
500-
}
501-
}
502-
503497
return rs, nil
504498
}
505499

Diff for: pkg/cbox/utils/conversions.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ type DBShare struct {
4646
ShareName string
4747
STime int
4848
FileTarget string
49-
RejectedBy string
5049
State int
5150
}
5251

@@ -172,6 +171,8 @@ func IntToShareState(g int) collaboration.ShareState {
172171
return collaboration.ShareState_SHARE_STATE_PENDING
173172
case 1:
174173
return collaboration.ShareState_SHARE_STATE_ACCEPTED
174+
case -1:
175+
return collaboration.ShareState_SHARE_STATE_REJECTED
175176
default:
176177
return collaboration.ShareState_SHARE_STATE_INVALID
177178
}
@@ -226,16 +227,9 @@ func ConvertToCS3Share(s DBShare) *collaboration.Share {
226227

227228
// ConvertToCS3ReceivedShare converts a DBShare to a CS3API collaboration received share
228229
func ConvertToCS3ReceivedShare(s DBShare) *collaboration.ReceivedShare {
229-
share := ConvertToCS3Share(s)
230-
var state collaboration.ShareState
231-
if s.RejectedBy != "" {
232-
state = collaboration.ShareState_SHARE_STATE_REJECTED
233-
} else {
234-
state = IntToShareState(s.State)
235-
}
236230
return &collaboration.ReceivedShare{
237-
Share: share,
238-
State: state,
231+
Share: ConvertToCS3Share(s),
232+
State: IntToShareState(s.State),
239233
}
240234
}
241235

0 commit comments

Comments
 (0)