Skip to content

Commit

Permalink
ocdav: Use GetUserByClaim to lookup user by name
Browse files Browse the repository at this point in the history
GetUser() is for getting users by ID only. Also try to handle the "User does not
exist" case with a more helpful error message.
  • Loading branch information
rhafer committed Oct 14, 2021
1 parent 580eef6 commit 76c6b42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
39 changes: 32 additions & 7 deletions internal/http/services/owncloud/ocdav/ocdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,50 @@ func (s *svc) ApplyLayout(ctx context.Context, ns string, useLoggedInUserNS bool
// namespace template.
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok || !useLoggedInUserNS {
var requestUserID string
requestUserID, requestPath = router.ShiftPath(requestPath)
// take a shortcut if "ns" is not a template
if !strings.Contains(ns, "{{") {
return ns, requestPath, nil
}
var requestUsernameOrID string
requestUsernameOrID, requestPath = router.ShiftPath(requestPath)

gatewayClient, err := s.getClient()
if err != nil {
return "", "", err
}
userRes, err := gatewayClient.GetUser(ctx, &userpb.GetUserRequest{
UserId: &userpb.UserId{OpaqueId: requestUserID},

// Check if we got a Username
res, err := gatewayClient.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
Claim: "username",
Value: requestUsernameOrID,
})
if err != nil {
return "", "", err
}
if userRes.Status.Code != rpc.Code_CODE_OK {
return "", "", errors.New(userRes.Status.Message)

// If it's not a Username, try if it is a valid userid
if res.Status.Code == rpc.Code_CODE_NOT_FOUND {
userRes, err := gatewayClient.GetUser(ctx, &userpb.GetUserRequest{
UserId: &userpb.UserId{OpaqueId: requestUsernameOrID},
})
if err != nil {
return "", "", err
}
res.Status = userRes.Status
res.User = userRes.User
}

// If still didn't find a user, fallback
if res.Status.Code == rpc.Code_CODE_NOT_FOUND {
res.User = &userpb.User{
Username: requestUsernameOrID,
Id: &userpb.UserId{OpaqueId: requestUsernameOrID},
}
}

u = userRes.User
u = res.User
}

return templates.WithUser(u, ns), requestPath, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (h *WebDavHandler) Handler(s *svc) http.Handler {
w.WriteHeader(http.StatusNotFound)
b, err := Marshal(exception{
code: SabredavNotFound,
message: fmt.Sprintf("could not get storage for %s", r.RemoteAddr),
message: fmt.Sprintf("could not get storage for %s", r.URL.Path),
})
HandleWebdavError(appctx.GetLogger(r.Context()), w, b, err)
}
Expand Down

0 comments on commit 76c6b42

Please sign in to comment.