From 8932e381168f1b311e71de63e0d081755b542880 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Wed, 27 Oct 2021 10:50:41 +0200 Subject: [PATCH] Enhance the cbox share sql driver to store accepted group shares --- changelog/unreleased/cbox-share-status.md | 3 + pkg/cbox/share/sql/sql.go | 80 +++++++++++------------ pkg/cbox/utils/conversions.go | 14 ++-- 3 files changed, 44 insertions(+), 53 deletions(-) create mode 100644 changelog/unreleased/cbox-share-status.md diff --git a/changelog/unreleased/cbox-share-status.md b/changelog/unreleased/cbox-share-status.md new file mode 100644 index 0000000000..0f09d10d04 --- /dev/null +++ b/changelog/unreleased/cbox-share-status.md @@ -0,0 +1,3 @@ +Enhancement: Enhance the cbox share sql driver to store accepted group shares + +https://github.com/cs3org/reva/pull/2211 \ No newline at end of file diff --git a/pkg/cbox/share/sql/sql.go b/pkg/cbox/share/sql/sql.go index 868b39f23b..c7bd3caa69 100644 --- a/pkg/cbox/share/sql/sql.go +++ b/pkg/cbox/share/sql/sql.go @@ -283,29 +283,25 @@ func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.Share, error) { uid := conversions.FormatUserID(ctxpkg.ContextMustGetUser(ctx).Id) 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, id, stime, permissions, share_type - FROM oc_share - WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner=? or uid_initiator=?)` + coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, + id, stime, permissions, share_type + FROM oc_share + WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner=? or uid_initiator=?)` params := []interface{}{uid, uid} - var ( - filterQuery string - filterParams []interface{} - err error - ) + if len(filters) == 0 { - filterQuery += "(share_type=? OR share_type=?)" + query += " AND (share_type=? OR share_type=?)" params = append(params, shareTypeUser) params = append(params, shareTypeGroup) } else { - filterQuery, filterParams, err = translateFilters(filters) + filterQuery, filterParams, err := translateFilters(filters) if err != nil { return nil, err } params = append(params, filterParams...) - } - - if filterQuery != "" { - query = fmt.Sprintf("%s AND (%s)", query, filterQuery) + if filterQuery != "" { + query = fmt.Sprintf("%s AND (%s)", query, filterQuery) + } } rows, err := m.db.Query(query, params...) @@ -340,10 +336,10 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F } 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 uid_initiator != ?)` + coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, + ts.id, stime, permissions, share_type, coalesce(tr.state, 0) as state + FROM oc_share ts LEFT JOIN oc_share_status tr ON (ts.id = tr.id AND tr.recipient = ?) + WHERE (orphan = 0 or orphan IS NULL) AND (uid_owner != ? AND uid_initiator != ?)` if len(user.Groups) > 0 { query += " AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" } else { @@ -369,7 +365,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F var s conversions.DBShare shares := []*collaboration.ReceivedShare{} for rows.Next() { - 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 { + 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 { continue } shares = append(shares, conversions.ConvertToCS3ReceivedShare(s)) @@ -391,13 +387,17 @@ func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId) (* } s := conversions.DBShare{ID: id.OpaqueId} - 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=? " + 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, coalesce(tr.state, 0) as state + FROM oc_share ts LEFT JOIN oc_share_status tr ON (ts.id = tr.id AND tr.recipient = ?) + WHERE (orphan = 0 or orphan IS NULL) AND ts.id=?` if len(user.Groups) > 0 { - query += "AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" + query += " AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" } else { - query += "AND (share_with=? AND share_type = 0)" + query += " AND (share_with=? AND share_type = 0)" } - 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 { + 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 { if err == sql.ErrNoRows { return nil, errtypes.NotFound(id.OpaqueId) } @@ -417,14 +417,18 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey) } s := conversions.DBShare{} - 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=? " + 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, coalesce(tr.state, 0) as state + FROM oc_share ts LEFT JOIN oc_share_status tr ON (ts.id = tr.id AND tr.recipient = ?) + WHERE (orphan = 0 or orphan IS NULL) AND uid_owner=? AND fileid_prefix=? AND item_source=? AND share_type=? AND share_with=?` if len(user.Groups) > 0 { - query += "AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" + query += " AND ((share_with=? AND share_type = 0) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" } else { - query += "AND (share_with=? AND share_type = 0)" + query += " AND (share_with=? AND share_type = 0)" } - 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 { + 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 { if err == sql.ErrNoRows { return nil, errtypes.NotFound(key.String()) } @@ -470,16 +474,17 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.Rece } } - var query, queryAccept string - params := []interface{}{rs.Share.Id.OpaqueId, conversions.FormatUserID(user.Id)} + state := 0 switch rs.GetState() { case collaboration.ShareState_SHARE_STATE_REJECTED: - query = "insert into oc_share_acl(id, rejected_by) values(?, ?)" + state = -1 case collaboration.ShareState_SHARE_STATE_ACCEPTED: - query = "delete from oc_share_acl where id=? AND rejected_by=?" - queryAccept = "update oc_share set accepted=1 where id=?" + state = 1 } + params := []interface{}{rs.Share.Id.OpaqueId, conversions.FormatUserID(user.Id), state, state} + query := "insert into oc_share_status(id, recipient, state) values(?, ?, ?) ON DUPLICATE KEY UPDATE state = ?" + stmt, err := m.db.Prepare(query) if err != nil { return nil, err @@ -489,17 +494,6 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.Rece return nil, err } - if queryAccept != "" { - stmt, err = m.db.Prepare(queryAccept) - if err != nil { - return nil, err - } - _, err = stmt.Exec(rs.Share.Id.OpaqueId) - if err != nil { - return nil, err - } - } - return rs, nil } diff --git a/pkg/cbox/utils/conversions.go b/pkg/cbox/utils/conversions.go index 20d2297b4f..787880ec89 100644 --- a/pkg/cbox/utils/conversions.go +++ b/pkg/cbox/utils/conversions.go @@ -46,7 +46,6 @@ type DBShare struct { ShareName string STime int FileTarget string - RejectedBy string State int } @@ -172,6 +171,8 @@ func IntToShareState(g int) collaboration.ShareState { return collaboration.ShareState_SHARE_STATE_PENDING case 1: return collaboration.ShareState_SHARE_STATE_ACCEPTED + case -1: + return collaboration.ShareState_SHARE_STATE_REJECTED default: return collaboration.ShareState_SHARE_STATE_INVALID } @@ -226,16 +227,9 @@ func ConvertToCS3Share(s DBShare) *collaboration.Share { // ConvertToCS3ReceivedShare converts a DBShare to a CS3API collaboration received share func ConvertToCS3ReceivedShare(s DBShare) *collaboration.ReceivedShare { - share := ConvertToCS3Share(s) - var state collaboration.ShareState - if s.RejectedBy != "" { - state = collaboration.ShareState_SHARE_STATE_REJECTED - } else { - state = IntToShareState(s.State) - } return &collaboration.ReceivedShare{ - Share: share, - State: state, + Share: ConvertToCS3Share(s), + State: IntToShareState(s.State), } }