Skip to content

Commit

Permalink
allow service users to accept shares for other users
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Aug 31, 2023
1 parent 99b5126 commit 4380d86
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ func (s *service) UpdateReceivedShare(ctx context.Context, req *collaboration.Up
}, nil
}

share, err := s.sm.UpdateReceivedShare(ctx, req.Share, req.UpdateMask)
// TODO: get user from somewhere
share, err := s.sm.UpdateReceivedShare(ctx, req.Share, req.UpdateMask, nil)
if err != nil {
return &collaboration.UpdateReceivedShareResponse{
Status: status.NewInternal(ctx, "error updating received share"),
Expand Down
3 changes: 2 additions & 1 deletion pkg/cbox/share/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"time"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -453,7 +454,7 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe

}

func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, _ *userpb.User) (*collaboration.ReceivedShare, error) {
user := ctxpkg.ContextMustGetUser(ctx)

rs, err := m.GetReceivedShare(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: share.Share.Id}})
Expand Down
9 changes: 7 additions & 2 deletions pkg/share/manager/cs3/cs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (m *Manager) GetReceivedShare(ctx context.Context, ref *collaboration.Share
}

// UpdateReceivedShare updates the received share with share state.
func (m *Manager) UpdateReceivedShare(ctx context.Context, rshare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *Manager) UpdateReceivedShare(ctx context.Context, rshare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userpb.User) (*collaboration.ReceivedShare, error) {
if err := m.initialize(); err != nil {
return nil, err
}
Expand All @@ -643,7 +643,12 @@ func (m *Manager) UpdateReceivedShare(ctx context.Context, rshare *collaboration
}
}

err = m.persistReceivedShare(ctx, user.Id, rs)
uid := user.GetId()
if user.GetId().GetType() == userpb.UserType_USER_TYPE_SERVICE {
uid = forUser.GetId()
}

err = m.persistReceivedShare(ctx, uid, rs)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/share/manager/cs3/cs3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ var _ = Describe("Manager", func() {
rs.MountPoint.Path = "newPath/"

rrs, err := m.UpdateReceivedShare(granteeCtx,
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}})
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rrs).ToNot(BeNil())
Expect(rrs.Share.ResourceId).ToNot(BeNil())
Expand All @@ -630,7 +630,7 @@ var _ = Describe("Manager", func() {
rs.MountPoint.Path = "newPath/"

rrs, err := m.UpdateReceivedShare(granteeCtx,
rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rrs).ToNot(BeNil())
Expect(rrs.Share.ResourceId).ToNot(BeNil())
Expand Down
20 changes: 12 additions & 8 deletions pkg/share/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func (m *mgr) getReceived(ctx context.Context, ref *collaboration.ShareReference
return m.convert(user.Id, s), nil
}

func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userv1beta1.User) (*collaboration.ReceivedShare, error) {
rs, err := m.getReceived(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}})
if err != nil {
return nil, err
Expand All @@ -568,27 +568,31 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaborat
}
}

user := ctxpkg.ContextMustGetUser(ctx)
u := ctxpkg.ContextMustGetUser(ctx)
uid := u.GetId().String()
if u.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE {
uid = forUser.GetId().String()
}
// Persist state
if v, ok := m.model.State[user.Id.String()]; ok {
if v, ok := m.model.State[uid]; ok {
v[rs.Share.Id.String()] = rs.State
m.model.State[user.Id.String()] = v
m.model.State[uid] = v
} else {
a := map[string]collaboration.ShareState{
rs.Share.Id.String(): rs.State,
}
m.model.State[user.Id.String()] = a
m.model.State[uid] = a
}

// Persist mount point
if v, ok := m.model.MountPoint[user.Id.String()]; ok {
if v, ok := m.model.MountPoint[uid]; ok {
v[rs.Share.Id.String()] = rs.MountPoint
m.model.MountPoint[user.Id.String()] = v
m.model.MountPoint[uid] = v
} else {
a := map[string]*provider.Reference{
rs.Share.Id.String(): rs.MountPoint,
}
m.model.MountPoint[user.Id.String()] = a
m.model.MountPoint[uid] = a
}

if err := m.model.Save(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/share/manager/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var _ = Describe("Json", func() {
rs.MountPoint = &providerv1beta1.Reference{Path: "newPath/"}

_, err = m.UpdateReceivedShare(granteeCtx,
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}})
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
})

Expand Down
11 changes: 7 additions & 4 deletions pkg/share/manager/jsoncs3/jsoncs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ func (m *Manager) getReceived(ctx context.Context, ref *collaboration.ShareRefer
}

// UpdateReceivedShare updates the received share with share state.
func (m *Manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *Manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userv1beta1.User) (*collaboration.ReceivedShare, error) {
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "UpdateReceivedShare")
defer span.End()

Expand All @@ -1003,10 +1003,13 @@ func (m *Manager) UpdateReceivedShare(ctx context.Context, receivedShare *collab
}

// write back
u := ctxpkg.ContextMustGetUser(ctx)
uid := u.GetId().GetOpaqueId()
if u.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE {
uid = forUser.GetId().GetOpaqueId()
}

userID := ctxpkg.ContextMustGetUser(ctx)

err = m.UserReceivedStates.Add(ctx, userID.GetId().GetOpaqueId(), rs.Share.ResourceId.StorageId+shareid.IDDelimiter+rs.Share.ResourceId.SpaceId, rs)
err = m.UserReceivedStates.Add(ctx, uid, rs.Share.ResourceId.StorageId+shareid.IDDelimiter+rs.Share.ResourceId.SpaceId, rs)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/share/manager/jsoncs3/jsoncs3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(err).ToNot(HaveOccurred())

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())

received, err := m.ListReceivedShares(granteeCtx, []*collaboration.Filter{})
Expand Down Expand Up @@ -921,7 +921,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

Expand All @@ -946,7 +946,7 @@ var _ = Describe("Jsoncs3", func() {
rs.MountPoint = &providerv1beta1.Reference{
Path: "newMP",
}
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.MountPoint.Path).To(Equal("newMP"))

Expand All @@ -967,7 +967,7 @@ var _ = Describe("Jsoncs3", func() {
})
Expect(err).ToNot(HaveOccurred())

_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"invalid"}})
_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"invalid"}}, nil)
Expect(err).To(HaveOccurred())
})

Expand All @@ -992,7 +992,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

Expand All @@ -1015,7 +1015,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

Expand Down
22 changes: 14 additions & 8 deletions pkg/share/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cs3org/reva/v2/pkg/share"
"google.golang.org/genproto/protobuf/field_mask"

userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -343,13 +344,12 @@ func (m *manager) getReceived(ctx context.Context, ref *collaboration.ShareRefer
return nil, errtypes.NotFound(ref.String())
}

func (m *manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userv1beta1.User) (*collaboration.ReceivedShare, error) {
rs, err := m.getReceived(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}})
if err != nil {
return nil, err
}

user := ctxpkg.ContextMustGetUser(ctx)
m.lock.Lock()
defer m.lock.Unlock()

Expand All @@ -364,25 +364,31 @@ func (m *manager) UpdateReceivedShare(ctx context.Context, receivedShare *collab
}
}

u := ctxpkg.ContextMustGetUser(ctx)
uid := u.GetId().String()
if u.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE {
uid = u.GetId().String()
}

// Persist state
if v, ok := m.shareState[user.Id.String()]; ok {
if v, ok := m.shareState[uid]; ok {
v[rs.Share.Id] = rs.State
m.shareState[user.Id.String()] = v
m.shareState[uid] = v
} else {
a := map[*collaboration.ShareId]collaboration.ShareState{
rs.Share.Id: rs.State,
}
m.shareState[user.Id.String()] = a
m.shareState[uid] = a
}
// Persist mount point
if v, ok := m.shareMountPoint[user.Id.String()]; ok {
if v, ok := m.shareMountPoint[uid]; ok {
v[rs.Share.Id] = rs.MountPoint
m.shareMountPoint[user.Id.String()] = v
m.shareMountPoint[uid] = v
} else {
a := map[*collaboration.ShareId]*provider.Reference{
rs.Share.Id: rs.MountPoint,
}
m.shareMountPoint[user.Id.String()] = a
m.shareMountPoint[uid] = a
}

return rs, nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/share/manager/owncloudsql/owncloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"time"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -422,7 +423,7 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe

}

func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, _ *userpb.User) (*collaboration.ReceivedShare, error) {
rs, err := m.GetReceivedShare(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}})
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/share/manager/owncloudsql/owncloudsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ var _ = Describe("SQL manager", func() {
Expect(share.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

share.State = collaboration.ShareState_SHARE_STATE_REJECTED
_, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"foo"}})
_, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"foo"}}, nil)
Expect(err).To(HaveOccurred())
})

Expand All @@ -386,12 +386,12 @@ var _ = Describe("SQL manager", func() {

share.State = collaboration.ShareState_SHARE_STATE_REJECTED

share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

share.State = collaboration.ShareState_SHARE_STATE_REJECTED
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.State).To(Equal(collaboration.ShareState_SHARE_STATE_REJECTED))

Expand All @@ -411,12 +411,12 @@ var _ = Describe("SQL manager", func() {

share.MountPoint = &provider.Reference{Path: "foo"}

share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.MountPoint.Path).To(Equal("shared"))

share.MountPoint = &provider.Reference{Path: "foo"}
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.MountPoint.Path).To(Equal("foo"))

Expand Down Expand Up @@ -465,7 +465,7 @@ var _ = Describe("SQL manager", func() {
share.MountPoint = &provider.Reference{Path: "foo"}

By("overriding the child share information for the current user")
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.MountPoint.Path).To(Equal("foo"))

Expand Down
20 changes: 11 additions & 9 deletions pkg/share/mocks/Manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Manager interface {
GetReceivedShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.ReceivedShare, error)

// UpdateReceivedShare updates the received share with share state.
UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error)
UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, user *userv1beta1.User) (*collaboration.ReceivedShare, error)
}

// ReceivedShareWithUser holds the relevant information for representing a received share of a user
Expand Down

0 comments on commit 4380d86

Please sign in to comment.