github.com/cs3org/reva/v2@v2.27.7/pkg/ocm/invite/repository/memory/memory.go (about) 1 // Copyright 2018-2023 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 memory 20 21 import ( 22 "context" 23 "strings" 24 "sync" 25 26 userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 27 invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1" 28 "github.com/cs3org/reva/v2/pkg/errtypes" 29 "github.com/cs3org/reva/v2/pkg/ocm/invite" 30 "github.com/cs3org/reva/v2/pkg/ocm/invite/repository/registry" 31 "github.com/cs3org/reva/v2/pkg/utils" 32 "github.com/cs3org/reva/v2/pkg/utils/list" 33 ) 34 35 func init() { 36 registry.Register("memory", New) 37 } 38 39 // New returns a new invite manager. 40 func New(m map[string]interface{}) (invite.Repository, error) { 41 return &manager{ 42 Invites: sync.Map{}, 43 AcceptedUsers: sync.Map{}, 44 }, nil 45 } 46 47 type manager struct { 48 Invites sync.Map 49 AcceptedUsers sync.Map 50 } 51 52 func (m *manager) AddToken(ctx context.Context, token *invitepb.InviteToken) error { 53 m.Invites.Store(token.GetToken(), token) 54 return nil 55 } 56 57 func (m *manager) GetToken(ctx context.Context, token string) (*invitepb.InviteToken, error) { 58 if v, ok := m.Invites.Load(token); ok { 59 return v.(*invitepb.InviteToken), nil 60 } 61 return nil, invite.ErrTokenNotFound 62 } 63 64 func (m *manager) ListTokens(ctx context.Context, initiator *userpb.UserId) ([]*invitepb.InviteToken, error) { 65 tokens := []*invitepb.InviteToken{} 66 m.Invites.Range(func(_, value any) bool { 67 token := value.(*invitepb.InviteToken) 68 if utils.UserEqual(token.UserId, initiator) { 69 tokens = append(tokens, token) 70 } 71 return true 72 }) 73 return tokens, nil 74 } 75 76 func (m *manager) AddRemoteUser(ctx context.Context, initiator *userpb.UserId, remoteUser *userpb.User) error { 77 usersList, ok := m.AcceptedUsers.Load(initiator) 78 acceptedUsers := usersList.([]*userpb.User) 79 if ok { 80 for _, acceptedUser := range acceptedUsers { 81 if acceptedUser.Id.GetOpaqueId() == remoteUser.Id.OpaqueId && acceptedUser.Id.GetIdp() == remoteUser.Id.Idp { 82 return invite.ErrUserAlreadyAccepted 83 } 84 } 85 86 acceptedUsers = append(acceptedUsers, remoteUser) 87 m.AcceptedUsers.Store(initiator.GetOpaqueId(), acceptedUsers) 88 } else { 89 acceptedUsers := []*userpb.User{remoteUser} 90 m.AcceptedUsers.Store(initiator.GetOpaqueId(), acceptedUsers) 91 } 92 return nil 93 } 94 95 func (m *manager) GetRemoteUser(ctx context.Context, initiator *userpb.UserId, remoteUserID *userpb.UserId) (*userpb.User, error) { 96 usersList, ok := m.AcceptedUsers.Load(initiator) 97 if !ok { 98 return nil, errtypes.NotFound(remoteUserID.OpaqueId) 99 } 100 101 acceptedUsers := usersList.([]*userpb.User) 102 for _, acceptedUser := range acceptedUsers { 103 if (acceptedUser.Id.GetOpaqueId() == remoteUserID.OpaqueId) && (remoteUserID.Idp == "" || acceptedUser.Id.GetIdp() == remoteUserID.Idp) { 104 return acceptedUser, nil 105 } 106 } 107 return nil, errtypes.NotFound(remoteUserID.OpaqueId) 108 } 109 110 func (m *manager) FindRemoteUsers(ctx context.Context, initiator *userpb.UserId, query string) ([]*userpb.User, error) { 111 usersList, ok := m.AcceptedUsers.Load(initiator) 112 if !ok { 113 return []*userpb.User{}, nil 114 } 115 116 users := []*userpb.User{} 117 acceptedUsers := usersList.([]*userpb.User) 118 for _, acceptedUser := range acceptedUsers { 119 if query == "" || userContains(acceptedUser, query) { 120 users = append(users, acceptedUser) 121 } 122 } 123 return users, nil 124 } 125 126 func userContains(u *userpb.User, query string) bool { 127 query = strings.ToLower(query) 128 return strings.Contains(strings.ToLower(u.Username), query) || strings.Contains(strings.ToLower(u.DisplayName), query) || 129 strings.Contains(strings.ToLower(u.Mail), query) || strings.Contains(strings.ToLower(u.Id.OpaqueId), query) 130 } 131 132 func (m *manager) DeleteRemoteUser(ctx context.Context, initiator *userpb.UserId, remoteUser *userpb.UserId) error { 133 usersList, ok := m.AcceptedUsers.Load(initiator) 134 if !ok { 135 return nil 136 } 137 138 acceptedUsers := usersList.([]*userpb.User) 139 for i, user := range acceptedUsers { 140 if (user.Id.GetOpaqueId() == remoteUser.OpaqueId) && (remoteUser.Idp == "" || user.Id.GetIdp() == remoteUser.Idp) { 141 m.AcceptedUsers.Store(initiator, list.Remove(acceptedUsers, i)) 142 return nil 143 } 144 } 145 return nil 146 }