Skip to content

Commit

Permalink
Merge pull request #1272 from anyproto/go-3440-check-blocks-restricti…
Browse files Browse the repository at this point in the history
…on-on-paste

GO-3440 Check Blocks restriction on paste
  • Loading branch information
KirillSto authored May 24, 2024
2 parents 7859d13 + fcfebcb commit d44af3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/block/editor/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ type clipboard struct {
fileObjectService fileobject.Service
}

func (cb *clipboard) Paste(ctx session.Context, req *pb.RpcBlockPasteRequest, groupId string) (blockIds []string, uploadArr []pb.RpcBlockUploadRequest, caretPosition int32, isSameBlockCaret bool, err error) {
func (cb *clipboard) Paste(ctx session.Context, req *pb.RpcBlockPasteRequest, groupId string) (
blockIds []string, uploadArr []pb.RpcBlockUploadRequest, caretPosition int32, isSameBlockCaret bool, err error,
) {
caretPosition = -1
if err = cb.Restrictions().Object.Check(model.Restrictions_Blocks); err != nil {
return nil, nil, caretPosition, false, err
}

if len(req.FileSlot) > 0 {
blockIds, err = cb.pasteFiles(ctx, req)
return
Expand Down
16 changes: 16 additions & 0 deletions core/block/editor/clipboard/clipboard_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clipboard

import (
"errors"
"strconv"
"testing"

Expand All @@ -13,6 +14,7 @@ import (
"github.com/anyproto/anytype-heart/core/block/editor/smartblock/smarttest"
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/core/block/editor/template"
"github.com/anyproto/anytype-heart/core/block/restriction"
"github.com/anyproto/anytype-heart/core/block/simple"
_ "github.com/anyproto/anytype-heart/core/block/simple/base"
"github.com/anyproto/anytype-heart/core/block/simple/text"
Expand Down Expand Up @@ -1077,6 +1079,20 @@ func TestClipboard_TitleOps(t *testing.T) {
require.Len(t, anySlot, 1)
assert.Equal(t, "it", anySlot[0].GetText().Text)
})

t.Run("do not paste if Blocks restriction is set to smartblock", func(t *testing.T) {
// given
sb := smarttest.New("test")
sb.SetRestrictions(restriction.Restrictions{Object: restriction.ObjectRestrictions{model.Restrictions_Blocks}})
cb := newFixture(t, sb)

// when
_, _, _, _, err := cb.Paste(nil, nil, "")

// then
assert.Error(t, err)
assert.True(t, errors.Is(err, restriction.ErrRestricted))
})
}

func addDescription(st *smarttest.SmartTest, description string) {
Expand Down

0 comments on commit d44af3e

Please sign in to comment.