Skip to content

Commit 4a5d8eb

Browse files
committed
Following cs3org/reva#4849, fetch the grantee user type when retrieving shares
1 parent e034e86 commit 4a5d8eb

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

share/sql/sql.go

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ import (
2727
"strings"
2828
"time"
2929

30-
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
30+
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
31+
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
3132
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
3233
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
3334
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
3435
"github.com/cs3org/reva"
3536
"github.com/cs3org/reva/pkg/appctx"
3637
conversions "github.com/cs3org/reva/pkg/cbox/utils"
3738
"github.com/cs3org/reva/pkg/errtypes"
39+
"github.com/cs3org/reva/pkg/rgrpc/status"
3840
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
3941
"github.com/cs3org/reva/pkg/share"
4042
"github.com/cs3org/reva/pkg/sharedconf"
@@ -120,7 +122,7 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collabora
120122
ResourceId: md.Id,
121123
Grantee: g.Grantee,
122124
}
123-
_, err := m.getByKey(ctx, key, true)
125+
_, err := m.getByKey(ctx, key, g.Grantee.GetUserId().Type, true)
124126

125127
// share already exists
126128
if err == nil {
@@ -175,7 +177,7 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collabora
175177
}, nil
176178
}
177179

178-
func (m *mgr) getByID(ctx context.Context, id *collaboration.ShareId, checkOwner bool) (*collaboration.Share, error) {
180+
func (m *mgr) getByID(ctx context.Context, id *collaboration.ShareId, gtype userpb.UserType, checkOwner bool) (*collaboration.Share, error) {
179181
uid := conversions.FormatUserID(appctx.ContextMustGetUser(ctx).Id)
180182
s := conversions.DBShare{ID: id.OpaqueId}
181183
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, lower(coalesce(share_with, '')) as share_with, coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, stime, permissions, share_type FROM oc_share WHERE (orphan = 0 or orphan IS NULL) AND id=?"
@@ -190,10 +192,10 @@ func (m *mgr) getByID(ctx context.Context, id *collaboration.ShareId, checkOwner
190192
}
191193
return nil, err
192194
}
193-
return conversions.ConvertToCS3Share(s), nil
195+
return conversions.ConvertToCS3Share(s, gtype), nil
194196
}
195197

196-
func (m *mgr) getByKey(ctx context.Context, key *collaboration.ShareKey, checkOwner bool) (*collaboration.Share, error) {
198+
func (m *mgr) getByKey(ctx context.Context, key *collaboration.ShareKey, gtype userpb.UserType, checkOwner bool) (*collaboration.Share, error) {
197199
owner := conversions.FormatUserID(key.Owner)
198200
uid := conversions.FormatUserID(appctx.ContextMustGetUser(ctx).Id)
199201

@@ -211,35 +213,36 @@ func (m *mgr) getByKey(ctx context.Context, key *collaboration.ShareKey, checkOw
211213
}
212214
return nil, err
213215
}
214-
return conversions.ConvertToCS3Share(s), nil
216+
return conversions.ConvertToCS3Share(s, gtype), nil
215217
}
216218

217219
func (m *mgr) GetShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.Share, error) {
218-
219220
var s *collaboration.Share
220221
var err error
221222
switch {
222223
case ref.GetId() != nil:
223-
s, err = m.getByID(ctx, ref.GetId(), false)
224+
s, err = m.getByID(ctx, ref.GetId(), userpb.UserType_USER_TYPE_INVALID, false)
224225
if err != nil {
225226
return nil, err
226227
}
227228
case ref.GetKey() != nil:
228-
s, err = m.getByKey(ctx, ref.GetKey(), false)
229+
s, err = m.getByKey(ctx, ref.GetKey(), userpb.UserType_USER_TYPE_INVALID, false)
229230
if err != nil {
230231
return nil, err
231232
}
232233
default:
233234
err = errtypes.NotFound(ref.String())
234235
}
235236

237+
// resolve grantee's user type
238+
s.Grantee.GetUserId().Type, _ = m.getUserType(ctx, s.Grantee.GetUserId().OpaqueId)
239+
236240
path, err := m.getPath(ctx, s.ResourceId)
237241
if err != nil {
238242
return nil, err
239243
}
240244

241245
user := appctx.ContextMustGetUser(ctx)
242-
243246
if m.isProjectAdmin(user, path) {
244247
return s, nil
245248
}
@@ -359,7 +362,8 @@ func (m *mgr) addPathIntoCtx(ctx context.Context, ref *collaboration.ShareRefere
359362
var err error
360363
switch {
361364
case ref.GetId() != nil:
362-
share, err := m.getByID(ctx, ref.GetId(), false)
365+
// here we don't manipulate the grantee's user type, so just assume PRIMARY
366+
share, err := m.getByID(ctx, ref.GetId(), userpb.UserType_USER_TYPE_PRIMARY, false)
363367
if err != nil {
364368
return nil, err
365369
}
@@ -379,15 +383,15 @@ func (m *mgr) addPathIntoCtx(ctx context.Context, ref *collaboration.ShareRefere
379383
return appctx.ContextSetResourcePath(ctx, path), nil
380384
}
381385

382-
func (m *mgr) isProjectAdminFromCtx(ctx context.Context, u *user.User) bool {
386+
func (m *mgr) isProjectAdminFromCtx(ctx context.Context, u *userpb.User) bool {
383387
path, ok := appctx.ContextGetResourcePath(ctx)
384388
if !ok {
385389
return false
386390
}
387391
return m.isProjectAdmin(u, path)
388392
}
389393

390-
func (m *mgr) isProjectAdmin(u *user.User, path string) bool {
394+
func (m *mgr) isProjectAdmin(u *userpb.User, path string) bool {
391395
if strings.HasPrefix(path, projectPathPrefix) {
392396
// The path will look like /eos/project/c/cernbox, we need to extract the project name
393397
parts := strings.SplitN(path, "/", 6)
@@ -451,7 +455,11 @@ func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) (
451455
if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ID, &s.STime, &s.Permissions, &s.ShareType); err != nil {
452456
continue
453457
}
454-
shares = append(shares, conversions.ConvertToCS3Share(s))
458+
gtype, _ := m.getUserType(ctx, s.ShareWith)
459+
// if err != nil {
460+
// failed to resolve grantee's user type, TODO Log
461+
// }
462+
shares = append(shares, conversions.ConvertToCS3Share(s, gtype))
455463
}
456464
if err = rows.Err(); err != nil {
457465
return nil, err
@@ -504,7 +512,11 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
504512
if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil {
505513
continue
506514
}
507-
shares = append(shares, conversions.ConvertToCS3ReceivedShare(s))
515+
gtype, _ := m.getUserType(ctx, s.ShareWith)
516+
// if err != nil {
517+
// failed to resolve grantee's user type, TODO Log
518+
// }
519+
shares = append(shares, conversions.ConvertToCS3ReceivedShare(s, gtype))
508520
}
509521
if err = rows.Err(); err != nil {
510522
return nil, err
@@ -513,7 +525,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
513525
return shares, nil
514526
}
515527

516-
func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId) (*collaboration.ReceivedShare, error) {
528+
func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId, gtype userpb.UserType) (*collaboration.ReceivedShare, error) {
517529
user := appctx.ContextMustGetUser(ctx)
518530
uid := conversions.FormatUserID(user.Id)
519531

@@ -539,10 +551,10 @@ func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId) (*
539551
}
540552
return nil, err
541553
}
542-
return conversions.ConvertToCS3ReceivedShare(s), nil
554+
return conversions.ConvertToCS3ReceivedShare(s, gtype), nil
543555
}
544556

545-
func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey) (*collaboration.ReceivedShare, error) {
557+
func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey, gtype userpb.UserType) (*collaboration.ReceivedShare, error) {
546558
user := appctx.ContextMustGetUser(ctx)
547559
uid := conversions.FormatUserID(user.Id)
548560

@@ -570,17 +582,17 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey)
570582
}
571583
return nil, err
572584
}
573-
return conversions.ConvertToCS3ReceivedShare(s), nil
585+
return conversions.ConvertToCS3ReceivedShare(s, gtype), nil
574586
}
575587

576588
func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.ReceivedShare, error) {
577589
var s *collaboration.ReceivedShare
578590
var err error
579591
switch {
580592
case ref.GetId() != nil:
581-
s, err = m.getReceivedByID(ctx, ref.GetId())
593+
s, err = m.getReceivedByID(ctx, ref.GetId(), userpb.UserType_USER_TYPE_INVALID)
582594
case ref.GetKey() != nil:
583-
s, err = m.getReceivedByKey(ctx, ref.GetKey())
595+
s, err = m.getReceivedByKey(ctx, ref.GetKey(), userpb.UserType_USER_TYPE_INVALID)
584596
default:
585597
err = errtypes.NotFound(ref.String())
586598
}
@@ -589,6 +601,9 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe
589601
return nil, err
590602
}
591603

604+
// resolve grantee's user type
605+
s.Share.Grantee.GetUserId().Type, _ = m.getUserType(ctx, s.Share.Grantee.GetUserId().OpaqueId)
606+
592607
return s, nil
593608
}
594609

@@ -719,3 +734,22 @@ func translateFilters(filters map[collaboration.Filter_Type][]*collaboration.Fil
719734
}
720735
return filterQuery, params, nil
721736
}
737+
738+
func (m *mgr) getUserType(ctx context.Context, username string) (userpb.UserType, error) {
739+
client, err := pool.GetGatewayServiceClient(pool.Endpoint(m.c.GatewaySvc))
740+
if err != nil {
741+
return userpb.UserType_USER_TYPE_PRIMARY, err
742+
}
743+
userRes, err := client.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
744+
Claim: "username",
745+
Value: username,
746+
})
747+
if err != nil {
748+
return userpb.UserType_USER_TYPE_PRIMARY, errors.Wrapf(err, "error getting user by username '%v'", username)
749+
}
750+
if userRes.Status.Code != rpc.Code_CODE_OK {
751+
return userpb.UserType_USER_TYPE_PRIMARY, status.NewErrorFromCode(userRes.Status.Code, "oidc")
752+
}
753+
754+
return userRes.GetUser().Id.Type, nil
755+
}

user/rest.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ type Identity struct {
159159
Upn string `json:"upn"`
160160
DisplayName string `json:"displayName"`
161161
Source string `json:"source,omitempty"`
162+
ActiveUser bool `json:"activeUser,omitempty"`
162163
UID int `json:"uid,omitempty"`
163164
GID int `json:"gid,omitempty"`
164165
}
@@ -184,8 +185,9 @@ func (i *Identity) UserType() userpb.UserType {
184185
case "Secondary":
185186
return userpb.UserType_USER_TYPE_SECONDARY
186187
case "Person":
187-
if i.Source == "cern" && i.UID > 0 {
188-
return userpb.UserType_USER_TYPE_PRIMARY // CERN user
188+
if i.Source == "cern" && i.ActiveUser {
189+
// this is a CERN account; incidentally, also i.UID > 0 qualifies for that
190+
return userpb.UserType_USER_TYPE_PRIMARY
189191
}
190192
return userpb.UserType_USER_TYPE_LIGHTWEIGHT // external user
191193
default:
@@ -194,7 +196,7 @@ func (i *Identity) UserType() userpb.UserType {
194196
}
195197

196198
func (m *manager) fetchAllUserAccounts(ctx context.Context) error {
197-
url := fmt.Sprintf("%s/api/v1.0/Identity?field=upn&field=primaryAccountEmail&field=displayName&field=uid&field=gid&field=type&field=source", m.conf.APIBaseURL)
199+
url := fmt.Sprintf("%s/api/v1.0/Identity?field=upn&field=primaryAccountEmail&field=displayName&field=uid&field=gid&field=type&field=source&field=activeUser", m.conf.APIBaseURL)
198200

199201
for {
200202
var r IdentitiesResponse

0 commit comments

Comments
 (0)