Skip to content

Commit

Permalink
graph/sharedbyme: Add user and group names to permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
rhafer committed Nov 6, 2023
1 parent 580a4e0 commit d2cff38
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
46 changes: 43 additions & 3 deletions services/graph/pkg/service/v0/sharedbyme.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"net/url"
"path"

group "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/share"
"github.com/cs3org/reva/v2/pkg/storagespace"
revautils "github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/render"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode"
Expand Down Expand Up @@ -124,14 +126,52 @@ func (g Graph) cs3UserSharesToDriveItems(ctx context.Context, shares []*collabor
grantedTo := libregraph.SharePointIdentitySet{}
switch s.Grantee.Type {
case storageprovider.GranteeType_GRANTEE_TYPE_USER:
gatewayClient, err := g.gatewaySelector.Next()
if err != nil {
g.logger.Error().Err(err).Msg("could not select next gateway client")
return driveItems, errorcode.New(errorcode.GeneralException, err.Error())
}
user := libregraph.NewIdentityWithDefaults()
user.SetId(s.Grantee.GetUserId().GetOpaqueId())
grantedTo.SetUser(*user)
cs3User, err := revautils.GetUser(s.GetGrantee().GetUserId(), gatewayClient)
switch {
case revautils.IsErrNotFound(err):
g.logger.Warn().Str("userid", s.Grantee.GetUserId().GetOpaqueId()).Msg("User not found by id")
// User does not seem to exist anymore, don't add a permission for this
continue
case err != nil:
return driveItems, errorcode.New(errorcode.GeneralException, err.Error())
default:
user.SetDisplayName(cs3User.GetDisplayName())
grantedTo.SetUser(*user)
}
case storageprovider.GranteeType_GRANTEE_TYPE_GROUP:
gatewayClient, err := g.gatewaySelector.Next()
if err != nil {
g.logger.Error().Err(err).Msg("could not select next gateway client")
return driveItems, errorcode.New(errorcode.GeneralException, err.Error())
}
req := group.GetGroupRequest{
GroupId: s.Grantee.GetGroupId(),
}
res, err := gatewayClient.GetGroup(ctx, &req)
if err != nil {
return driveItems, errorcode.New(errorcode.GeneralException, err.Error())
}
grp := libregraph.NewIdentityWithDefaults()
grp.SetId(s.Grantee.GetGroupId().GetOpaqueId())
grantedTo.SetGroup(*grp)

switch res.Status.Code {
case rpc.Code_CODE_OK:
cs3Group := res.GetGroup()
grp.SetDisplayName(cs3Group.GetDisplayName())
grantedTo.SetGroup(*grp)
case rpc.Code_CODE_NOT_FOUND:
g.logger.Warn().Str("groupid", s.Grantee.GetGroupId().GetOpaqueId()).Msg("Group not found by id")
// Group does not seem to exist anymore, don't add a permission for this
continue
default:
return driveItems, errorcode.New(errorcode.GeneralException, res.Status.Message)
}
}

// set expiration date
Expand Down
47 changes: 47 additions & 0 deletions services/graph/pkg/service/v0/sharedbyme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var _ = Describe("sharedbyme", func() {

pool.RemoveSelector("GatewaySelector" + "com.owncloud.api.gateway")
gatewayClient = &cs3mocks.GatewayAPIClient{}

gatewayClient.On("Stat",
mock.Anything,
mock.MatchedBy(
Expand Down Expand Up @@ -153,6 +154,52 @@ var _ = Describe("sharedbyme", func() {
Id: userShare.ResourceId,
},
}, nil)

gatewayClient.On("GetUser",
mock.Anything,
mock.MatchedBy(func(req *userpb.GetUserRequest) bool {
return req.UserId.OpaqueId == "user-id"
})).
Return(&userpb.GetUserResponse{
Status: status.NewOK(ctx),
User: &userpb.User{
Id: &userpb.UserId{
Idp: "idp",
OpaqueId: "user-id",
},
DisplayName: "User Name",
},
}, nil)
gatewayClient.On("GetUser",
mock.Anything,
mock.Anything).
Return(&userpb.GetUserResponse{
Status: status.NewNotFound(ctx, "mock user not found"),
User: nil,
}, nil)
gatewayClient.On("GetGroup",
mock.Anything,
mock.MatchedBy(func(req *grouppb.GetGroupRequest) bool {
return req.GroupId.OpaqueId == "group-id"
})).
Return(&grouppb.GetGroupResponse{
Status: status.NewOK(ctx),
Group: &grouppb.Group{
Id: &grouppb.GroupId{
Idp: "idp",
OpaqueId: "group-id",
},
DisplayName: "Group Name",
},
}, nil)
gatewayClient.On("GetGroup",
mock.Anything,
mock.Anything).
Return(&grouppb.GetGroupResponse{
Status: status.NewNotFound(ctx, "mock group not found"),
Group: nil,
}, nil)

gatewaySelector = pool.GetSelector[gateway.GatewayAPIClient](
"GatewaySelector",
"com.owncloud.api.gateway",
Expand Down

0 comments on commit d2cff38

Please sign in to comment.