Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Feb 12, 2024
1 parent 883641e commit 2ebaae2
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 449 deletions.
2 changes: 1 addition & 1 deletion cmd/revad/runtime/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ import (
_ "github.com/cs3org/reva/pkg/ocm/share/repository/loader"
_ "github.com/cs3org/reva/pkg/permission/manager/loader"
_ "github.com/cs3org/reva/pkg/preferences/loader"
_ "github.com/cs3org/reva/pkg/projects/manager/loader"
_ "github.com/cs3org/reva/pkg/prom/loader"
_ "github.com/cs3org/reva/pkg/publicshare/manager/loader"
_ "github.com/cs3org/reva/pkg/rhttp/datatx/manager/loader"
_ "github.com/cs3org/reva/pkg/share/cache/loader"
_ "github.com/cs3org/reva/pkg/share/cache/warmup/loader"
_ "github.com/cs3org/reva/pkg/share/manager/loader"
_ "github.com/cs3org/reva/pkg/spaces/manager/loader"
_ "github.com/cs3org/reva/pkg/storage/favorite/loader"
_ "github.com/cs3org/reva/pkg/storage/fs/loader"
_ "github.com/cs3org/reva/pkg/storage/registry/loader"
Expand Down
119 changes: 101 additions & 18 deletions internal/grpc/services/spacesregistry/spacesregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ package spacesregistry

import (
"context"
"encoding/base32"
"errors"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/plugin"
"github.com/cs3org/reva/pkg/projects"
"github.com/cs3org/reva/pkg/projects/manager/registry"
pu "github.com/cs3org/reva/pkg/projects/utils"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/cs3org/reva/pkg/spaces"
"github.com/cs3org/reva/pkg/spaces/manager/registry"
"github.com/cs3org/reva/pkg/utils"
"github.com/cs3org/reva/pkg/utils/cfg"
"google.golang.org/grpc"
Expand All @@ -55,8 +61,9 @@ func (c *config) ApplyDefaults() {
}

type service struct {
c *config
spaces spaces.Manager
c *config
projects projects.Catalogue
gw gateway.GatewayAPIClient
}

func New(ctx context.Context, m map[string]interface{}) (rgrpc.Service, error) {
Expand All @@ -68,14 +75,21 @@ func New(ctx context.Context, m map[string]interface{}) (rgrpc.Service, error) {
if err != nil {
return nil, err
}

client, err := pool.GetGatewayServiceClient(pool.Endpoint(sharedconf.GetGatewaySVC("")))
if err != nil {
return nil, err
}

svc := service{
c: &c,
spaces: s,
c: &c,
projects: s,
gw: client,
}
return &svc, nil
}

func getSpacesDriver(ctx context.Context, driver string, cfg map[string]map[string]any) (spaces.Manager, error) {
func getSpacesDriver(ctx context.Context, driver string, cfg map[string]map[string]any) (projects.Catalogue, error) {
if f, ok := registry.NewFuncs[driver]; ok {
return f(ctx, cfg[driver])
}
Expand All @@ -88,21 +102,90 @@ func (s *service) CreateStorageSpace(ctx context.Context, req *provider.CreateSt

func (s *service) ListStorageSpaces(ctx context.Context, req *provider.ListStorageSpacesRequest) (*provider.ListStorageSpacesResponse, error) {
user := appctx.ContextMustGetUser(ctx)
spaces, err := s.spaces.ListSpaces(ctx, user, req.Filters)
if err != nil {
return &provider.ListStorageSpacesResponse{
Status: status.NewInternal(ctx, err, "error listing storage spaces"),
}, nil
filters := req.Filters

sp := []*provider.StorageSpace{}
if len(filters) == 0 {
homes, err := s.listSpacesByType(ctx, user, spaces.SpaceTypeHome)
if err != nil {
return &provider.ListStorageSpacesResponse{Status: status.NewInternal(ctx, err, err.Error())}, nil
}
sp = append(sp, homes...)

projects, err := s.listSpacesByType(ctx, user, spaces.SpaceTypeProject)
if err != nil {
return &provider.ListStorageSpacesResponse{Status: status.NewInternal(ctx, err, err.Error())}, nil
}
sp = append(sp, projects...)
}

for _, s := range spaces {
s.Id = &provider.StorageSpaceId{
OpaqueId: base32.StdEncoding.EncodeToString([]byte(s.RootInfo.Path)),
for _, filter := range filters {
switch filter.Type {
case provider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE:
spaces, err := s.listSpacesByType(ctx, user, spaces.SpaceType(filter.Term.(*provider.ListStorageSpacesRequest_Filter_SpaceType).SpaceType))
if err != nil {
return &provider.ListStorageSpacesResponse{Status: status.NewInternal(ctx, err, err.Error())}, nil
}
sp = append(sp, spaces...)
default:
return nil, errtypes.NotSupported("filter not supported")
}
}
return &provider.ListStorageSpacesResponse{
Status: status.NewOK(ctx),
StorageSpaces: spaces,
return &provider.ListStorageSpacesResponse{Status: status.NewOK(ctx), StorageSpaces: sp}, nil
}

func (s *service) listSpacesByType(ctx context.Context, user *userpb.User, spaceType spaces.SpaceType) ([]*provider.StorageSpace, error) {
sp := []*provider.StorageSpace{}

if spaceType == spaces.SpaceTypeHome {
space, err := s.userSpace(ctx, user)
if err != nil {
return nil, err
}
if space != nil {
sp = append(sp, space)
}
} else if spaceType == spaces.SpaceTypeProject {
projects, err := s.projects.ListProjects(ctx, user)
if err != nil {
return nil, err
}
sp = append(sp, projects...)
}

return sp, nil
}

func (s *service) userSpace(ctx context.Context, user *userpb.User) (*provider.StorageSpace, error) {
if utils.UserIsLightweight(user) {
return nil, nil // lightweight accounts and federated do not have a user space
}

home, err := s.gw.GetHome(ctx, &provider.GetHomeRequest{})
if err != nil {
return nil, err
}

stat, err := s.gw.Stat(ctx, &provider.StatRequest{
Ref: &provider.Reference{
Path: home.Path,
},
})
if err != nil {
return nil, err
}

return &provider.StorageSpace{
Id: &provider.StorageSpaceId{
OpaqueId: pu.EncodeSpaceID(stat.Info.Id.StorageId, home.Path),
},
Owner: user,
Name: user.Username,
SpaceType: spaces.SpaceTypeHome.AsString(),
RootInfo: &provider.ResourceInfo{
PermissionSet: conversions.NewManagerRole().CS3ResourcePermissions(),
Path: home.Path,
},
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ package loader

import (
// Load core spacews backends.
_ "github.com/cs3org/reva/pkg/spaces/manager/memory"
_ "github.com/cs3org/reva/pkg/projects/manager/memory"
// Add your own here.
)
116 changes: 116 additions & 0 deletions pkg/projects/manager/memory/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2018-2023 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package memory

import (
"context"
"slices"

"github.com/cs3org/reva/pkg/projects"
"github.com/cs3org/reva/pkg/projects/manager/registry"
"github.com/cs3org/reva/pkg/spaces"
pu "github.com/cs3org/reva/pkg/projects/utils"
"github.com/cs3org/reva/pkg/utils/cfg"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
conversions "github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"
)

func init() {
registry.Register("memory", New)
}

type SpaceDescription struct {
StorageID string `mapstructure:"storage_id" validate:"required"`
Path string `mapstructure:"path" validate:"required"`
Name string `mapstructure:"name" validate:"required"`
Type string `mapstructure:"type" validate:"required"`
Owner string `mapstructure:"owner" validate:"required"`
Readers string `mapstructure:"readers" validate:"required"`
Writers string `mapstructure:"writers" validate:"required"`
Admins string `mapstructure:"admins" validate:"required"`
}

type Config struct {
Spaces []SpaceDescription `mapstructure:"spaces"`
}

type service struct {
c *Config
}

func New(ctx context.Context, m map[string]any) (projects.Catalogue, error) {
var c Config
if err := cfg.Decode(m, &c); err != nil {
return nil, err
}
return NewWithConfig(ctx, &c)
}

func NewWithConfig(ctx context.Context, c *Config) (projects.Catalogue, error) {
return &service{c: c}, nil
}

func (s *service) StoreProject(ctx context.Context, owner *userpb.UserId, path, name string, quota *provider.Quota) error {
panic("not yet implemented")
}

func (s *service) ListProjects(ctx context.Context, user *userpb.User) ([]*provider.StorageSpace, error) {
projects := []*provider.StorageSpace{}
for _, space := range s.c.Spaces {
if perms, ok := projectBelongToUser(user, &space); ok {
projects = append(projects, &provider.StorageSpace{
Id: &provider.StorageSpaceId{
OpaqueId: pu.EncodeSpaceID(space.StorageID, space.Path),
},
Owner: &userpb.User{
Id: &userpb.UserId{
OpaqueId: space.Owner,
},
},
Name: space.Name,
SpaceType: spaces.SpaceTypeProject.AsString(),
RootInfo: &provider.ResourceInfo{
Path: space.Path,
PermissionSet: perms,
},
})
}
}
return projects, nil
}

func projectBelongToUser(user *userpb.User, project *SpaceDescription) (*provider.ResourcePermissions, bool) {
if user.Id.OpaqueId == project.Owner {
return conversions.NewManagerRole().CS3ResourcePermissions(), true
}
if slices.Contains(user.Groups, project.Admins) {
return conversions.NewManagerRole().CS3ResourcePermissions(), true
}
if slices.Contains(user.Groups, project.Writers) {
return conversions.NewEditorRole().CS3ResourcePermissions(), true
}
if slices.Contains(user.Groups, project.Readers) {
return conversions.NewViewerRole().CS3ResourcePermissions(), true
}
return nil, false
}

var _ projects.Catalogue = (*service)(nil)
Loading

0 comments on commit 2ebaae2

Please sign in to comment.