Skip to content

Commit

Permalink
implement removing a member from space
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Nov 9, 2021
1 parent 0bc2584 commit 4528446
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 46 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/space-membership.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Implement space membership endpoints

Implemented endpoints to add and remove members to spaces.

https://github.com/owncloud/ocis/issues/2740
https://github.com/cs3org/reva/pull/2250
4 changes: 3 additions & 1 deletion internal/http/services/owncloud/ocs/conversions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const (

// ShareTypeFederatedCloudShare represents a federated share
ShareTypeFederatedCloudShare ShareType = 6
ShareTypeSpaceMembership ShareType = 7

// ShareTypeSpaceMembership represents an action regarding space members
ShareTypeSpaceMembership ShareType = 7
)

// ResourceType indicates the OCS type of the resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strconv"

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"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -206,32 +205,10 @@ func (h *Handler) isPublicShare(r *http.Request, oid string) bool {
})
if err != nil {
logger.Err(err)
}

if psRes.GetShare() != nil {
return true
}

// check if we have a user share
uRes, err := client.GetShare(r.Context(), &collaboration.GetShareRequest{
Ref: &collaboration.ShareReference{
Spec: &collaboration.ShareReference_Id{
Id: &collaboration.ShareId{
OpaqueId: oid,
},
},
},
})
if err != nil {
logger.Err(err)
}

if uRes.GetShare() != nil {
return false
}

// TODO token is neither a public or a user share.
return false
return psRes.GetShare() != nil
}

func (h *Handler) updatePublicShare(w http.ResponseWriter, r *http.Request, shareID string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,15 @@ func (h *Handler) updateShare(w http.ResponseWriter, r *http.Request, shareID st
// RemoveShare handles DELETE requests on /apps/files_sharing/api/v1/shares/(shareid)
func (h *Handler) RemoveShare(w http.ResponseWriter, r *http.Request) {
shareID := chi.URLParam(r, "shareid")
if h.isPublicShare(r, shareID) {
switch {
case h.isPublicShare(r, shareID):
h.removePublicShare(w, r, shareID)
return
case h.isUserShare(r, shareID):
h.removeUserShare(w, r, shareID)
default:
// The request is a remove space member request.
h.removeSpaceMember(w, r, shareID)
}
h.removeUserShare(w, r, shareID)
}

// ListShares handles GET requests on /apps/files_sharing/api/v1/shares
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,69 @@ package shares

import (
"context"
"fmt"
"net/http"

groupv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
registry "github.com/cs3org/go-cs3apis/cs3/storage/registry/v1beta1"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/response"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/utils"
"github.com/pkg/errors"
)

func (h *Handler) addSpaceMember(w http.ResponseWriter, r *http.Request, info *provider.ResourceInfo, role *conversions.Role, roleVal []byte) {
ctx := r.Context()
func (h *Handler) getGrantee(ctx context.Context, name string) (provider.Grantee, error) {
log := appctx.GetLogger(ctx)
client, err := pool.GetGatewayServiceClient(h.gatewayAddr)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error getting grpc gateway client", err)
return
return provider.Grantee{}, err
}
userRes, err := client.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
Claim: "username",
Value: name,
})
if err == nil && userRes.Status.Code == rpc.Code_CODE_OK {
return provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{UserId: userRes.User.Id},
}, nil
}
log.Debug().Str("name", name).Msg("no user found")

groupRes, err := client.GetGroupByClaim(ctx, &groupv1beta1.GetGroupByClaimRequest{
Claim: "group_name",
Value: name,
})
if err == nil && groupRes.Status.Code == rpc.Code_CODE_OK {
return provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_GROUP,
Id: &provider.Grantee_GroupId{GroupId: groupRes.Group.Id},
}, nil
}
log.Debug().Str("name", name).Msg("no group found")

return provider.Grantee{}, fmt.Errorf("no grantee found with name %s", name)
}

func (h *Handler) addSpaceMember(w http.ResponseWriter, r *http.Request, info *provider.ResourceInfo, role *conversions.Role, roleVal []byte) {
ctx := r.Context()

shareWith := r.FormValue("shareWith")
if shareWith == "" {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, "missing shareWith", nil)
return
}

userRes, err := client.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
Claim: "username",
Value: shareWith,
})
grantee, err := h.getGrantee(ctx, shareWith)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error searching recipient", err)
return
}

if userRes.Status.Code != rpc.Code_CODE_OK {
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "user not found", err)
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "error getting grantee", err)
return
}

Expand All @@ -79,10 +103,7 @@ func (h *Handler) addSpaceMember(w http.ResponseWriter, r *http.Request, info *p
addGrantRes, err := providerClient.AddGrant(ctx, &provider.AddGrantRequest{
Ref: ref,
Grant: &provider.Grant{
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{UserId: userRes.User.Id},
},
Grantee: &grantee,
Permissions: role.CS3ResourcePermissions(),
},
})
Expand All @@ -94,6 +115,57 @@ func (h *Handler) addSpaceMember(w http.ResponseWriter, r *http.Request, info *p
response.WriteOCSSuccess(w, r, nil)
}

func (h *Handler) removeSpaceMember(w http.ResponseWriter, r *http.Request, spaceID string) {
ctx := r.Context()

shareWith := r.URL.Query().Get("shareWith")
if shareWith == "" {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, "missing shareWith", nil)
return
}

grantee, err := h.getGrantee(ctx, shareWith)
if err != nil {
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "error getting grantee", err)
return
}

ref, err := utils.ParseStorageSpaceReference(spaceID)
if err != nil {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, "could not parse space id", err)
return
}

providers, err := h.findProviders(ctx, &ref)
if err != nil {
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "error getting storage provider", err)
return
}

providerClient, err := h.getStorageProviderClient(providers[0])
if err != nil {
response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "error getting storage provider", err)
return
}

removeGrantRes, err := providerClient.RemoveGrant(ctx, &provider.RemoveGrantRequest{
Ref: &ref,
Grant: &provider.Grant{
Grantee: &grantee,
},
})
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error removing grant", err)
return
}
if removeGrantRes.Status.Code != rpc.Code_CODE_OK {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error removing grant", err)
return
}

response.WriteOCSSuccess(w, r, nil)
}

func (h *Handler) getStorageProviderClient(p *registry.ProviderInfo) (provider.ProviderAPIClient, error) {
c, err := pool.GetStorageProviderServiceClient(p.Address)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ func (h *Handler) createUserShare(w http.ResponseWriter, r *http.Request, statIn
h.createCs3Share(ctx, w, r, c, createShareReq, statInfo)
}

func (h *Handler) isUserShare(r *http.Request, oid string) bool {
logger := appctx.GetLogger(r.Context())
client, err := pool.GetGatewayServiceClient(h.gatewayAddr)
if err != nil {
logger.Err(err)
}

getShareRes, err := client.GetShare(r.Context(), &collaboration.GetShareRequest{
Ref: &collaboration.ShareReference{
Spec: &collaboration.ShareReference_Id{
Id: &collaboration.ShareId{
OpaqueId: oid,
},
},
},
})
if err != nil {
logger.Err(err)
return false
}

return getShareRes.GetShare() != nil
}

func (h *Handler) removeUserShare(w http.ResponseWriter, r *http.Request, shareID string) {
ctx := r.Context()

Expand Down

0 comments on commit 4528446

Please sign in to comment.