|
| 1 | +// Copyright 2018-2019 CERN |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// In applying this license, CERN does not waive the privileges and immunities |
| 16 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 17 | +// or submit itself to any jurisdiction. |
| 18 | + |
| 19 | +package context |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "path" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/cs3org/reva/pkg/errtypes" |
| 28 | + "github.com/cs3org/reva/pkg/storage" |
| 29 | + "github.com/cs3org/reva/pkg/storage/helper" |
| 30 | + "github.com/cs3org/reva/pkg/storage/pw/registry" |
| 31 | + "github.com/cs3org/reva/pkg/user" |
| 32 | + "github.com/mitchellh/mapstructure" |
| 33 | + "github.com/pkg/errors" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + registry.Register("context", New) |
| 38 | +} |
| 39 | + |
| 40 | +type config struct { |
| 41 | + Prefix string `mapstructure:"prefix"` |
| 42 | + Layout string `mapstructure:"layout"` |
| 43 | +} |
| 44 | + |
| 45 | +func parseConfig(m map[string]interface{}) (*config, error) { |
| 46 | + c := &config{Layout: "{{.Username}}"} |
| 47 | + if err := mapstructure.Decode(m, c); err != nil { |
| 48 | + err = errors.Wrap(err, "error decoding conf") |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return c, nil |
| 52 | +} |
| 53 | + |
| 54 | +// New returns an implementation to of the storage.PathWrapper interface that |
| 55 | +// is used to wrap and unwrap storage paths |
| 56 | +func New(m map[string]interface{}) (storage.PathWrapper, error) { |
| 57 | + c, err := parseConfig(m) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + return &pw{prefix: c.Prefix, layout: c.Layout}, nil |
| 62 | +} |
| 63 | + |
| 64 | +type pw struct { |
| 65 | + prefix string |
| 66 | + layout string |
| 67 | +} |
| 68 | + |
| 69 | +// Only works when a user is in context |
| 70 | +func (pw *pw) Unwrap(ctx context.Context, rp string) (string, error) { |
| 71 | + |
| 72 | + u, ok := user.ContextGetUser(ctx) |
| 73 | + if !ok { |
| 74 | + return "", errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx") |
| 75 | + } |
| 76 | + if u.Username == "" { |
| 77 | + return "", errors.Wrap(errtypes.UserRequired("userrequired"), "user has no username") |
| 78 | + } |
| 79 | + userhome, err := helper.GetUserHomePath(u, pw.layout) |
| 80 | + if err != nil { |
| 81 | + return "", errors.Wrap(errtypes.UserRequired("userrequired"), fmt.Sprintf("template error: %s", err.Error())) |
| 82 | + } |
| 83 | + return path.Join("/", pw.prefix, userhome, rp), nil |
| 84 | +} |
| 85 | + |
| 86 | +func (pw *pw) Wrap(ctx context.Context, rp string) (string, error) { |
| 87 | + u, ok := user.ContextGetUser(ctx) |
| 88 | + if !ok { |
| 89 | + return "", errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx") |
| 90 | + } |
| 91 | + if u.Username == "" { |
| 92 | + return "", errors.Wrap(errtypes.UserRequired("userrequired"), "user has no username") |
| 93 | + } |
| 94 | + userhome, err := helper.GetUserHomePath(u, pw.layout) |
| 95 | + if err != nil { |
| 96 | + return "", errors.Wrap(errtypes.UserRequired("userrequired"), fmt.Sprintf("template error: %s", err.Error())) |
| 97 | + } |
| 98 | + return strings.TrimPrefix(rp, path.Join("/", userhome)), nil |
| 99 | +} |
0 commit comments