Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Populate owner data in the ocs and ocdav services (cs3org#2233)
Browse files Browse the repository at this point in the history
* Populate owner data in the ocs and ocdav services

* Handle the case when owner is nil

* Only fetch owner info when needed
  • Loading branch information
ishank011 committed Feb 7, 2022
1 parent 882add6 commit 8dc78c6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 23 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/ocs-user-data.md
@@ -0,0 +1,3 @@
Enhancement: Populate owner data in the ocs and ocdav services

https://github.com/cs3org/reva/pull/2233
17 changes: 11 additions & 6 deletions internal/http/services/owncloud/ocdav/ocdav.go
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

"github.com/ReneKroon/ttlcache/v2"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
Expand Down Expand Up @@ -114,11 +115,12 @@ func (c *Config) init() {
}

type svc struct {
c *Config
webDavHandler *WebDavHandler
davHandler *DavHandler
favoritesManager favorite.Manager
client *http.Client
c *Config
webDavHandler *WebDavHandler
davHandler *DavHandler
favoritesManager favorite.Manager
client *http.Client
userIdentifierCache *ttlcache.Cache
}

func getFavoritesManager(c *Config) (favorite.Manager, error) {
Expand Down Expand Up @@ -150,8 +152,11 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error)
rhttp.Timeout(time.Duration(conf.Timeout*int64(time.Second))),
rhttp.Insecure(conf.Insecure),
),
favoritesManager: fm,
favoritesManager: fm,
userIdentifierCache: ttlcache.NewCache(),
}
_ = s.userIdentifierCache.SetTTL(60 * time.Second)

// initialize handlers and set default configs
if err := s.webDavHandler.init(conf.WebdavNamespace, true); err != nil {
return nil, err
Expand Down
66 changes: 50 additions & 16 deletions internal/http/services/owncloud/ocdav/propfind.go
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"
"github.com/cs3org/reva/pkg/appctx"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/publicshare"
rtrace "github.com/cs3org/reva/pkg/trace"
"github.com/cs3org/reva/pkg/utils"
Expand Down Expand Up @@ -642,6 +643,13 @@ func (s *svc) mdToPropResponse(ctx context.Context, pf *propfindXML, md *provide
// TODO return other properties ... but how do we put them in a namespace?
} else {
// otherwise return only the requested properties
var ownerUsername, ownerDisplayName string
owner, err := s.getOwnerInfo(ctx, md.Owner)
if err == nil {
ownerUsername = owner.Username
ownerDisplayName = owner.DisplayName
}

for i := range pf.Prop {
switch pf.Prop[i].Space {
case _nsOwncloud:
Expand Down Expand Up @@ -731,14 +739,8 @@ func (s *svc) mdToPropResponse(ctx context.Context, pf *propfindXML, md *provide
propstatNotFound.Prop = append(propstatNotFound.Prop, s.newProp("oc:size", ""))
}
case "owner-id": // phoenix only
if md.Owner != nil {
if isCurrentUserOwner(ctx, md.Owner) {
u := ctxpkg.ContextMustGetUser(ctx)
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:owner-id", u.Username))
} else {
sublog.Debug().Msg("TODO fetch user username")
propstatNotFound.Prop = append(propstatNotFound.Prop, s.newProp("oc:owner-id", ""))
}
if ownerUsername != "" {
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:owner-id", ownerUsername))
} else {
propstatNotFound.Prop = append(propstatNotFound.Prop, s.newProp("oc:owner-id", ""))
}
Expand Down Expand Up @@ -816,14 +818,8 @@ func (s *svc) mdToPropResponse(ctx context.Context, pf *propfindXML, md *provide
propstatNotFound.Prop = append(propstatNotFound.Prop, s.newProp("oc:"+pf.Prop[i].Local, ""))
}
case "owner-display-name": // phoenix only
if md.Owner != nil {
if isCurrentUserOwner(ctx, md.Owner) {
u := ctxpkg.ContextMustGetUser(ctx)
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:owner-display-name", u.DisplayName))
} else {
sublog.Debug().Msg("TODO fetch user displayname")
propstatNotFound.Prop = append(propstatNotFound.Prop, s.newProp("oc:owner-display-name", ""))
}
if ownerDisplayName != "" {
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:owner-display-name", ownerDisplayName))
} else {
propstatNotFound.Prop = append(propstatNotFound.Prop, s.newProp("oc:owner-display-name", ""))
}
Expand Down Expand Up @@ -997,6 +993,44 @@ func quoteEtag(etag string) string {
return `"` + strings.Trim(etag, `"`) + `"`
}

func (s *svc) getOwnerInfo(ctx context.Context, owner *userv1beta1.UserId) (*userv1beta1.User, error) {
if owner == nil {
return nil, errtypes.NotFound("owner is nil")
}

if isCurrentUserOwner(ctx, owner) {
return ctxpkg.ContextMustGetUser(ctx), nil
}

log := appctx.GetLogger(ctx)
if idIf, err := s.userIdentifierCache.Get(owner.OpaqueId); err == nil {
log.Debug().Msg("cache hit")
return idIf.(*userv1beta1.User), nil
}

client, err := s.getClient()
if err != nil {
log.Error().Err(err).Msg("error getting grpc client")
return nil, err
}

res, err := client.GetUser(ctx, &userv1beta1.GetUserRequest{UserId: owner})
if err != nil {
log.Err(err).Msg("could not look up user")
return nil, err
}
if res.GetStatus().GetCode() != rpc.Code_CODE_OK {
log.Err(err).Msg("get user call failed")
return nil, err
}
if res.User == nil {
log.Debug().Msg("user not found")
return nil, err
}
_ = s.userIdentifierCache.Set(owner.OpaqueId, res.User)
return res.User, nil
}

// a file is only yours if you are the owner
func isCurrentUserOwner(ctx context.Context, owner *userv1beta1.UserId) bool {
contextUser, ok := ctxpkg.ContextGetUser(ctx)
Expand Down
Expand Up @@ -49,7 +49,22 @@ func (h *Handler) Init(c *config.Config) {
// GetGroups handles GET requests on /cloud/users/groups
// TODO: implement
func (h *Handler) GetGroups(w http.ResponseWriter, r *http.Request) {
response.WriteOCSSuccess(w, r, &Groups{})
ctx := r.Context()

user := chi.URLParam(r, "userid")
// FIXME use ldap to fetch user info
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "missing user in context", fmt.Errorf("missing user in context"))
return
}
if user != u.Username {
// FIXME allow fetching other users info? only for admins
response.WriteOCSError(w, r, http.StatusForbidden, "user id mismatch", fmt.Errorf("%s tried to access %s user info endpoint", u.Id.OpaqueId, user))
return
}

response.WriteOCSSuccess(w, r, &Groups{Groups: u.Groups})
}

// Quota holds quota information
Expand Down

0 comments on commit 8dc78c6

Please sign in to comment.