graph: Set roles/actions in sharedByMe response (#7703)

* unifiedrole: Add CS3ResourcePermissionsToLibregraphActions

Add function to convert CS3ResourcePermsissions to libregraph actions

* unifiedrole: Fix strings for the UnifiedRoleConditionSelf

The "Self/Owner/Grantee" string are not part the the constraint value

* graph: Move getRoleDefinitionList to unifiedrole module

rename it to GetBuiltinRoleDefinitionList and make it public

* graph: turn libregraph resource actions into string constants

* graph/sharedbyme: Set the correct roles (or actions) on permissions

Try to map CS3 resource permissions on a share to one of the default libregraph
UnifiedRoleDefinitions. If a match if found return the roleid in 'permissions.roles'
attribute of the response. If no match if found convert the
ResourcePermissions in to `libre.graph.permissions.actions` and return
those in the response.
This commit is contained in:
Ralf Haferkamp
2023-11-17 15:41:24 +01:00
committed by GitHub
parent 03989e6d56
commit 9303cdece7
6 changed files with 222 additions and 79 deletions
@@ -15,7 +15,7 @@ import (
// GetRoleDefinitions a list of permission roles than can be used when sharing with users or groups
func (g Graph) GetRoleDefinitions(w http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusOK)
render.JSON(w, r, getRoleDefinitionList(g.config.FilesSharing.EnableResharing))
render.JSON(w, r, unifiedrole.GetBuiltinRoleDefinitionList(g.config.FilesSharing.EnableResharing))
}
// GetRoleDefinition a permission role than can be used when sharing with users or groups
@@ -37,21 +37,8 @@ func (g Graph) GetRoleDefinition(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, role)
}
func getRoleDefinitionList(resharing bool) []*libregraph.UnifiedRoleDefinition {
return []*libregraph.UnifiedRoleDefinition{
unifiedrole.NewViewerUnifiedRole(resharing),
unifiedrole.NewSpaceViewerUnifiedRole(),
unifiedrole.NewEditorUnifiedRole(resharing),
unifiedrole.NewSpaceEditorUnifiedRole(),
unifiedrole.NewFileEditorUnifiedRole(resharing),
unifiedrole.NewCoownerUnifiedRole(),
unifiedrole.NewUploaderUnifiedRole(),
unifiedrole.NewManagerUnifiedRole(),
}
}
func getRoleDefinition(roleID string, resharing bool) (*libregraph.UnifiedRoleDefinition, error) {
roleList := getRoleDefinitionList(resharing)
roleList := unifiedrole.GetBuiltinRoleDefinitionList(resharing)
for _, role := range roleList {
if role != nil && role.Id != nil && *role.Id == roleID {
return role, nil
+13 -1
View File
@@ -17,6 +17,7 @@ import (
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/services/graph/pkg/identity"
"github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode"
"github.com/owncloud/ocis/v2/services/graph/pkg/unifiedrole"
)
type driveItemsByResourceID map[string]libregraph.DriveItem
@@ -160,7 +161,18 @@ func (g Graph) cs3UserSharesToDriveItems(ctx context.Context, shares []*collabor
if s.GetExpiration() != nil {
perm.SetExpirationDateTime(cs3TimestampToTime(s.GetExpiration()))
}
role := unifiedrole.CS3ResourcePermissionsToUnifiedRole(
*s.GetPermissions().GetPermissions(),
unifiedrole.UnifiedRoleConditionGrantee,
g.config.FilesSharing.EnableResharing,
)
if role != nil {
perm.SetRoles([]string{role.GetId()})
} else {
actions := unifiedrole.CS3ResourcePermissionsToLibregraphActions(*s.GetPermissions().GetPermissions())
perm.SetLibreGraphPermissionsActions(actions)
perm.SetRoles(nil)
}
perm.SetGrantedToV2(grantedTo)
item.Permissions = append(item.Permissions, perm)
driveItems[resIDStr] = item
@@ -15,6 +15,7 @@ import (
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/conversions"
"github.com/cs3org/reva/v2/pkg/rgrpc/status"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/storagespace"
@@ -28,6 +29,7 @@ import (
"github.com/owncloud/ocis/v2/services/graph/pkg/config/defaults"
identitymocks "github.com/owncloud/ocis/v2/services/graph/pkg/identity/mocks"
service "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0"
"github.com/owncloud/ocis/v2/services/graph/pkg/unifiedrole"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
)
@@ -45,6 +47,8 @@ var _ = Describe("sharedbyme", func() {
rr *httptest.ResponseRecorder
)
expiration := time.Now()
editorResourcePermissions := conversions.NewEditorRole(true).CS3ResourcePermissions()
userShare := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "share-id",
@@ -62,6 +66,9 @@ var _ = Describe("sharedbyme", func() {
},
},
},
Permissions: &collaboration.SharePermissions{
Permissions: editorResourcePermissions,
},
}
groupShare := collaboration.Share{
Id: &collaboration.ShareId{
@@ -80,6 +87,9 @@ var _ = Describe("sharedbyme", func() {
},
},
},
Permissions: &collaboration.SharePermissions{
Permissions: editorResourcePermissions,
},
}
userShareWithExpiration := collaboration.Share{
Id: &collaboration.ShareId{
@@ -98,6 +108,9 @@ var _ = Describe("sharedbyme", func() {
},
},
},
Permissions: &collaboration.SharePermissions{
Permissions: editorResourcePermissions,
},
Expiration: utils.TimeToTS(expiration),
}
@@ -218,6 +231,7 @@ var _ = Describe("sharedbyme", func() {
cfg.TokenManager.JWTSecret = "loremipsum"
cfg.Commons = &shared.Commons{}
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
cfg.FilesSharing.EnableResharing = true
svc, _ = service.NewService(
service.Config(cfg),
@@ -323,6 +337,10 @@ var _ = Describe("sharedbyme", func() {
Expect(user.GetId()).To(Equal(userShare.GetGrantee().GetUserId().GetOpaqueId()))
_, ok = perm[0].GetLinkOk()
Expect(ok).To(BeFalse())
roles, ok := perm[0].GetRolesOk()
Expect(ok).To(BeTrue())
Expect(len(roles)).To(Equal(1))
Expect(roles[0]).To(Equal(unifiedrole.UnifiedRoleEditorID))
})
It("returns a proper driveItem, when a single group share is returned", func() {
@@ -363,6 +381,10 @@ var _ = Describe("sharedbyme", func() {
Expect(group.GetId()).To(Equal(groupShare.GetGrantee().GetGroupId().GetOpaqueId()))
_, ok = perm[0].GetLinkOk()
Expect(ok).To(BeFalse())
roles, ok := perm[0].GetRolesOk()
Expect(ok).To(BeTrue())
Expect(len(roles)).To(Equal(1))
Expect(roles[0]).To(Equal(unifiedrole.UnifiedRoleEditorID))
})
It("returns a single driveItem, when a mulitple shares for the same resource are returned", func() {