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 7, 2021
1 parent c367f2a commit 18d2416
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 27 additions & 5 deletions internal/http/services/owncloud/ocdav/ocdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,47 @@ 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)
var requestUsernameOrId string
requestUsernameOrId, requestPath = router.ShiftPath(requestPath)

gatewayClient, err := s.getClient()
if err != nil {
return "", "", err
}

// Check if this is a Userid
userRes, err := gatewayClient.GetUser(ctx, &userpb.GetUserRequest{
UserId: &userpb.UserId{OpaqueId: requestUserID},
UserId: &userpb.UserId{OpaqueId: 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 userid try if it is a user name
if userRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
res, err := gatewayClient.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{
Claim: "username",
Value: requestUsernameOrId,
})
if err != nil {
return "", "", err
} else {
userRes.Status = res.Status
userRes.User = res.User
}
}

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

u = userRes.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 @@ -96,7 +96,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 18d2416

Please sign in to comment.