Skip to content

Commit

Permalink
map user id to username in share ocs share responses
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Dec 15, 2020
1 parent e791b55 commit 1af72bb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/ocs-share-api-userid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Change: replace the user uuid with the username in ocs share responses

The ocs api should not send the users uuid. Replaced the uuid with the username.

https://github.com/owncloud/ocis/issues/990
https://github.com/cs3org/reva/pull/1370
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ type Handler struct {
publicURL string
sharePrefix string
displayNameCache *ttlmap.TTLMap
userNameCache *ttlmap.TTLMap
}

// Init initializes this and any contained handlers
func (h *Handler) Init(c *config.Config) error {
h.gatewayAddr = c.GatewaySvc
h.publicURL = c.Config.Host
h.displayNameCache = ttlmap.New(1000, 60)
h.userNameCache = ttlmap.New(1000, 60)
h.sharePrefix = c.SharePrefix
return nil
}
Expand Down Expand Up @@ -313,6 +315,7 @@ func (h *Handler) createUserShare(w http.ResponseWriter, r *http.Request) {
return
}
h.addDisplaynames(ctx, c, s)
h.mapUserIds(ctx, c, s)

response.WriteOCSSuccess(w, r, s)
}
Expand Down Expand Up @@ -528,6 +531,7 @@ func (h *Handler) getShare(w http.ResponseWriter, r *http.Request, shareID strin
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error mapping share data", err)
}
h.addDisplaynames(ctx, client, share)
h.mapUserIds(ctx, client, share)

response.WriteOCSSuccess(w, r, []*conversions.ShareData{share})
}
Expand Down Expand Up @@ -651,6 +655,7 @@ func (h *Handler) updateShare(w http.ResponseWriter, r *http.Request, shareID st
return
}
h.addDisplaynames(ctx, uClient, share)
h.mapUserIds(ctx, uClient, share)

response.WriteOCSSuccess(w, r, share)
}
Expand Down Expand Up @@ -815,6 +820,7 @@ func (h *Handler) listSharesWithMe(w http.ResponseWriter, r *http.Request) {
continue
}
h.addDisplaynames(r.Context(), gwc, data)
h.mapUserIds(r.Context(), gwc, data)

if data.State == ocsStateAccepted {
// Needed because received shares can be jailed in a folder in the users home
Expand Down Expand Up @@ -1050,10 +1056,63 @@ func (h *Handler) getDisplayname(ctx context.Context, c gateway.GatewayAPIClient
}

h.displayNameCache.Put(userid, res.User.DisplayName)
h.userNameCache.Put(userid, res.User.Username)
log.Debug().Str("userid", userid).Msg("cache update")
return res.User.DisplayName
}

func (h *Handler) getUsername(ctx context.Context, c gateway.GatewayAPIClient, userid string) string {
log := appctx.GetLogger(ctx)
if userid == "" {
return ""
}
if un := h.userNameCache.Get(userid); un != "" {
log.Debug().Str("userid", userid).Msg("cache hit")
return un
}
log.Debug().Str("userid", userid).Msg("cache miss")
res, err := c.GetUser(ctx, &userpb.GetUserRequest{
UserId: &userpb.UserId{
OpaqueId: userid,
},
})
if err != nil {
log.Err(err).
Str("userid", userid).
Msg("could not look up user")
return ""
}
if res.GetStatus().GetCode() != rpc.Code_CODE_OK {
log.Err(err).
Str("opaque_id", userid).
Int32("code", int32(res.GetStatus().GetCode())).
Str("message", res.GetStatus().GetMessage()).
Msg("get user call failed")
return ""
}
if res.User == nil {
log.Debug().
Str("opaque_id", userid).
Int32("code", int32(res.GetStatus().GetCode())).
Str("message", res.GetStatus().GetMessage()).
Msg("user not found")
return ""
}
if res.User.Username == "" {
log.Debug().
Str("opaque_id", userid).
Int32("code", int32(res.GetStatus().GetCode())).
Str("message", res.GetStatus().GetMessage()).
Msg("Username empty")
return ""
}

h.userNameCache.Put(userid, res.User.Username)
h.displayNameCache.Put(userid, res.User.DisplayName)
log.Debug().Str("userid", userid).Msg("cache update")
return res.User.Username
}

func (h *Handler) addDisplaynames(ctx context.Context, c gateway.GatewayAPIClient, s *conversions.ShareData) {
if s.DisplaynameOwner == "" {
s.DisplaynameOwner = h.getDisplayname(ctx, c, s.UIDOwner)
Expand All @@ -1066,6 +1125,12 @@ func (h *Handler) addDisplaynames(ctx context.Context, c gateway.GatewayAPIClien
}
}

func (h *Handler) mapUserIds(ctx context.Context, c gateway.GatewayAPIClient, s *conversions.ShareData) {
s.UIDOwner = h.getUsername(ctx, c, s.UIDOwner)
s.UIDFileOwner = h.getUsername(ctx, c, s.UIDFileOwner)
s.ShareWith = h.getUsername(ctx, c, s.ShareWith)
}

func parseTimestamp(timestampString string) (*types.Timestamp, error) {
parsedTime, err := time.Parse("2006-01-02T15:04:05Z0700", timestampString)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (h *Handler) listUserShares(r *http.Request, filters []*collaboration.ListS
continue
}
h.addDisplaynames(ctx, c, data)
h.mapUserIds(ctx, c, data)

log.Debug().Interface("share", s).Interface("info", rInfo).Interface("shareData", data).Msg("mapped")
ocsDataPayload = append(ocsDataPayload, data)
Expand Down

0 comments on commit 1af72bb

Please sign in to comment.