github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/groupprovider/groupprovider.go (about)

     1  // Copyright 2018-2020 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package groupprovider
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"sort"
    25  
    26  	grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/errtypes"
    28  	"github.com/cs3org/reva/v2/pkg/group"
    29  	"github.com/cs3org/reva/v2/pkg/group/manager/registry"
    30  	"github.com/cs3org/reva/v2/pkg/rgrpc"
    31  	"github.com/cs3org/reva/v2/pkg/rgrpc/status"
    32  	"github.com/mitchellh/mapstructure"
    33  	"github.com/pkg/errors"
    34  	"github.com/rs/zerolog"
    35  	"google.golang.org/grpc"
    36  )
    37  
    38  func init() {
    39  	rgrpc.Register("groupprovider", New)
    40  }
    41  
    42  type config struct {
    43  	Driver  string                            `mapstructure:"driver"`
    44  	Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
    45  }
    46  
    47  func (c *config) init() {
    48  	if c.Driver == "" {
    49  		c.Driver = "json"
    50  	}
    51  }
    52  
    53  func parseConfig(m map[string]interface{}) (*config, error) {
    54  	c := &config{}
    55  	if err := mapstructure.Decode(m, c); err != nil {
    56  		err = errors.Wrap(err, "error decoding conf")
    57  		return nil, err
    58  	}
    59  	c.init()
    60  	return c, nil
    61  }
    62  
    63  func getDriver(c *config) (group.Manager, error) {
    64  	if f, ok := registry.NewFuncs[c.Driver]; ok {
    65  		return f(c.Drivers[c.Driver])
    66  	}
    67  
    68  	return nil, errtypes.NotFound(fmt.Sprintf("driver %s not found for group manager", c.Driver))
    69  }
    70  
    71  // New returns a new GroupProviderServiceServer.
    72  func New(m map[string]interface{}, ss *grpc.Server, _ *zerolog.Logger) (rgrpc.Service, error) {
    73  	c, err := parseConfig(m)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	groupManager, err := getDriver(c)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	svc := &service{groupmgr: groupManager}
    84  
    85  	return svc, nil
    86  }
    87  
    88  type service struct {
    89  	groupmgr group.Manager
    90  }
    91  
    92  func (s *service) Close() error {
    93  	return nil
    94  }
    95  
    96  func (s *service) UnprotectedEndpoints() []string {
    97  	return []string{}
    98  }
    99  
   100  func (s *service) Register(ss *grpc.Server) {
   101  	grouppb.RegisterGroupAPIServer(ss, s)
   102  }
   103  
   104  func (s *service) GetGroup(ctx context.Context, req *grouppb.GetGroupRequest) (*grouppb.GetGroupResponse, error) {
   105  	if req.GroupId == nil {
   106  		res := &grouppb.GetGroupResponse{
   107  			Status: status.NewInvalid(ctx, "groupid missing"),
   108  		}
   109  		return res, nil
   110  	}
   111  	group, err := s.groupmgr.GetGroup(ctx, req.GroupId, req.SkipFetchingMembers)
   112  	if err != nil {
   113  		res := &grouppb.GetGroupResponse{}
   114  		if _, ok := err.(errtypes.NotFound); ok {
   115  			res.Status = status.NewNotFound(ctx, "group not found")
   116  		} else {
   117  			res.Status = status.NewInternal(ctx, "error getting group")
   118  		}
   119  		return res, nil
   120  	}
   121  
   122  	return &grouppb.GetGroupResponse{
   123  		Status: status.NewOK(ctx),
   124  		Group:  group,
   125  	}, nil
   126  }
   127  
   128  func (s *service) GetGroupByClaim(ctx context.Context, req *grouppb.GetGroupByClaimRequest) (*grouppb.GetGroupByClaimResponse, error) {
   129  	group, err := s.groupmgr.GetGroupByClaim(ctx, req.Claim, req.Value, req.SkipFetchingMembers)
   130  	if err != nil {
   131  		res := &grouppb.GetGroupByClaimResponse{}
   132  		if _, ok := err.(errtypes.NotFound); ok {
   133  			res.Status = status.NewNotFound(ctx, fmt.Sprintf("group not found %s %s", req.Claim, req.Value))
   134  		} else {
   135  			res.Status = status.NewInternal(ctx, "error getting group by claim")
   136  		}
   137  		return res, nil
   138  	}
   139  
   140  	return &grouppb.GetGroupByClaimResponse{
   141  		Status: status.NewOK(ctx),
   142  		Group:  group,
   143  	}, nil
   144  }
   145  
   146  func (s *service) FindGroups(ctx context.Context, req *grouppb.FindGroupsRequest) (*grouppb.FindGroupsResponse, error) {
   147  	groups, err := s.groupmgr.FindGroups(ctx, req.Filter, req.SkipFetchingMembers)
   148  	if err != nil {
   149  		return &grouppb.FindGroupsResponse{
   150  			Status: status.NewInternal(ctx, "error finding groups"),
   151  		}, nil
   152  	}
   153  
   154  	// sort group by groupname
   155  	sort.Slice(groups, func(i, j int) bool {
   156  		return groups[i].GroupName <= groups[j].GroupName
   157  	})
   158  
   159  	return &grouppb.FindGroupsResponse{
   160  		Status: status.NewOK(ctx),
   161  		Groups: groups,
   162  	}, nil
   163  }
   164  
   165  func (s *service) GetMembers(ctx context.Context, req *grouppb.GetMembersRequest) (*grouppb.GetMembersResponse, error) {
   166  	if req.GroupId == nil {
   167  		res := &grouppb.GetMembersResponse{
   168  			Status: status.NewInvalid(ctx, "groupid missing"),
   169  		}
   170  		return res, nil
   171  	}
   172  	members, err := s.groupmgr.GetMembers(ctx, req.GroupId)
   173  	if err != nil {
   174  		return &grouppb.GetMembersResponse{
   175  			Status: status.NewInternal(ctx, "error getting group members"),
   176  		}, nil
   177  	}
   178  
   179  	return &grouppb.GetMembersResponse{
   180  		Status:  status.NewOK(ctx),
   181  		Members: members,
   182  	}, nil
   183  }
   184  
   185  func (s *service) HasMember(ctx context.Context, req *grouppb.HasMemberRequest) (*grouppb.HasMemberResponse, error) {
   186  	if req.GroupId == nil || req.UserId == nil {
   187  		res := &grouppb.HasMemberResponse{
   188  			Status: status.NewInvalid(ctx, "groupid or userid missing"),
   189  		}
   190  		return res, nil
   191  	}
   192  	ok, err := s.groupmgr.HasMember(ctx, req.GroupId, req.UserId)
   193  	if err != nil {
   194  		return &grouppb.HasMemberResponse{
   195  			Status: status.NewInternal(ctx, "error checking for group member"),
   196  		}, nil
   197  	}
   198  
   199  	return &grouppb.HasMemberResponse{
   200  		Status: status.NewOK(ctx),
   201  		Ok:     ok,
   202  	}, nil
   203  }