Skip to content

Commit

Permalink
graph/sharedbyme: populate the permissions property
Browse files Browse the repository at this point in the history
Set the exipration date, grantedToV2 and link attributes on the
permissions.

The displayName for users and groups isn't expanded yet (only the "id" is set)
Also, the "roles" attribute cannot be populated yet.
  • Loading branch information
rhafer committed Oct 23, 2023
1 parent c2cd772 commit 086b7b4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
47 changes: 47 additions & 0 deletions services/graph/pkg/service/v0/sharedbyme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package svc
import (
"context"
"net/http"
"net/url"
"path"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
Expand Down Expand Up @@ -115,6 +117,29 @@ func (g Graph) cs3UserSharesToDriveItems(ctx context.Context, shares []*collabor
}
item = *itemptr
}
perm := libregraph.Permission{}
perm.SetRoles([]string{"TODO:insert-role-here"})
perm.SetId(s.Id.OpaqueId)
grantedTo := libregraph.SharePointIdentitySet{}
switch s.Grantee.Type {
case storageprovider.GranteeType_GRANTEE_TYPE_USER:
user := libregraph.NewIdentityWithDefaults()
user.SetId(s.Grantee.GetUserId().GetOpaqueId())
grantedTo.SetUser(*user)
case storageprovider.GranteeType_GRANTEE_TYPE_GROUP:
grp := libregraph.NewIdentityWithDefaults()
grp.SetId(s.Grantee.GetGroupId().GetOpaqueId())
grantedTo.SetGroup(*grp)

}

// set expiration date
if s.GetExpiration() != nil {
perm.SetExpirationDateTime(cs3TimestampToTime(s.GetExpiration()))
}

perm.SetGrantedToV2(grantedTo)
item.Permissions = append(item.Permissions, perm)
driveItems[resIDStr] = item
}
return driveItems, nil
Expand All @@ -133,6 +158,28 @@ func (g Graph) cs3PublicSharesToDriveItems(ctx context.Context, shares []*link.P
}
item = *itemptr
}
perm := libregraph.Permission{}
perm.SetId(s.Id.OpaqueId)
perm.SetRoles([]string{"TODO:insert-role-here"})
link := libregraph.SharingLink{}
webURL, err := url.Parse(g.config.Spaces.WebDavBase)
if err != nil {
g.logger.Error().
Err(err).
Str("url", g.config.Spaces.WebDavBase).
Msg("failed to parse webURL base url")
return driveItems, err
}

webURL.Path = path.Join(webURL.Path, "s", s.GetToken())
link.SetWebUrl(webURL.String())
perm.SetLink(link)
// set expiration date
if s.GetExpiration() != nil {
perm.SetExpirationDateTime(cs3TimestampToTime(s.GetExpiration()))
}

item.Permissions = append(item.Permissions, perm)
driveItems[resIDStr] = item
}

Expand Down
44 changes: 37 additions & 7 deletions services/graph/pkg/service/v0/sharedbyme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
cs3mocks "github.com/cs3org/reva/v2/tests/cs3mocks/mocks"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
"github.com/owncloud/ocis/v2/services/graph/mocks"
"github.com/owncloud/ocis/v2/services/graph/pkg/config"
Expand All @@ -44,9 +43,8 @@ var _ = Describe("sharedbyme", func() {
identityBackend *identitymocks.Backend

rr *httptest.ResponseRecorder

newGroup *libregraph.Group
)
expiration := time.Now()
userShare := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "share-id",
Expand Down Expand Up @@ -100,7 +98,7 @@ var _ = Describe("sharedbyme", func() {
},
},
},
Expiration: utils.TimeToTS(time.Now()),
Expiration: utils.TimeToTS(expiration),
}

BeforeEach(func() {
Expand Down Expand Up @@ -148,9 +146,6 @@ var _ = Describe("sharedbyme", func() {
)

identityBackend = &identitymocks.Backend{}
newGroup = libregraph.NewGroup()
newGroup.SetMembersodataBind([]string{"/users/user1"})
newGroup.SetId("group1")

rr = httptest.NewRecorder()
ctx = context.Background()
Expand Down Expand Up @@ -237,6 +232,17 @@ var _ = Describe("sharedbyme", func() {

di := res.Value[0]
Expect(di.GetId()).To(Equal(storagespace.FormatResourceID(*userShare.GetResourceId())))

perm := di.GetPermissions()
Expect(perm[0].GetId()).To(Equal(userShare.GetId().GetOpaqueId()))
_, ok := perm[0].GetExpirationDateTimeOk()
Expect(ok).To(BeFalse())
_, ok = perm[0].GrantedToV2.GetGroupOk()
Expect(ok).To(BeFalse())
user, ok := perm[0].GrantedToV2.GetUserOk()
Expect(ok).To(BeTrue())
Expect(user.GetId()).To(Equal(userShare.GetGrantee().GetUserId().GetOpaqueId()))

})

It("returns a proper driveItem, when a single group share is returned", func() {
Expand Down Expand Up @@ -264,6 +270,16 @@ var _ = Describe("sharedbyme", func() {

di := res.Value[0]
Expect(di.GetId()).To(Equal(storagespace.FormatResourceID(*groupShare.GetResourceId())))

perm := di.GetPermissions()
Expect(perm[0].GetId()).To(Equal(userShare.GetId().GetOpaqueId()))
_, ok := perm[0].GetExpirationDateTimeOk()
Expect(ok).To(BeFalse())
_, ok = perm[0].GrantedToV2.GetUserOk()
Expect(ok).To(BeFalse())
group, ok := perm[0].GrantedToV2.GetGroupOk()
Expect(ok).To(BeTrue())
Expect(group.GetId()).To(Equal(groupShare.GetGrantee().GetGroupId().GetOpaqueId()))
})

It("returns a single driveItem, when a mulitple shares for the same resource are returned", func() {
Expand Down Expand Up @@ -292,6 +308,9 @@ var _ = Describe("sharedbyme", func() {

di := res.Value[0]
Expect(di.GetId()).To(Equal(storagespace.FormatResourceID(*groupShare.GetResourceId())))

// one permission per share
Expect(len(di.GetPermissions())).To(Equal(2))
})

It("return a driveItem with the expiration date set, for expiring shares", func() {
Expand Down Expand Up @@ -319,6 +338,17 @@ var _ = Describe("sharedbyme", func() {

di := res.Value[0]
Expect(di.GetId()).To(Equal(storagespace.FormatResourceID(*userShareWithExpiration.GetResourceId())))

perm := di.GetPermissions()
Expect(perm[0].GetId()).To(Equal(userShareWithExpiration.GetId().GetOpaqueId()))
exp, ok := perm[0].GetExpirationDateTimeOk()
Expect(ok).To(BeTrue())
Expect(exp.Equal(expiration)).To(BeTrue())
_, ok = perm[0].GrantedToV2.GetGroupOk()
Expect(ok).To(BeFalse())
user, ok := perm[0].GrantedToV2.GetUserOk()
Expect(ok).To(BeTrue())
Expect(user.GetId()).To(Equal(userShareWithExpiration.GetGrantee().GetUserId().GetOpaqueId()))
})
})
})

0 comments on commit 086b7b4

Please sign in to comment.