Skip to content

Commit

Permalink
use configurable templates for space alias
Browse files Browse the repository at this point in the history
  • Loading branch information
micbar committed Mar 9, 2022
1 parent 34d2f49 commit 338dfff
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/storage/utils/decomposedfs/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Options struct {

// permissions service to use when checking permissions
PermissionsSVC string `mapstructure:"permissionssvc"`

PersonalSpaceAliasTemplate string `mapstructure:"personalspacealias_template"`
GeneralSpaceAliasTemplate string `mapstructure:"generalspacealias_template"`
}

// New returns a new Options instance for the given configuration
Expand All @@ -73,5 +76,13 @@ func New(m map[string]interface{}) (*Options, error) {
// c.DataDirectory should never end in / unless it is the root
o.Root = filepath.Clean(o.Root)

if o.PersonalSpaceAliasTemplate == "" {
o.PersonalSpaceAliasTemplate = "{{.SpaceType}}/{{.User.Username}}"
}

if o.GeneralSpaceAliasTemplate == "" {
o.GeneralSpaceAliasTemplate = "{{.SpaceType}}/{{.SpaceName | replace \" \" \"-\" | lower}}"
}

return o, nil
}
7 changes: 6 additions & 1 deletion pkg/storage/utils/decomposedfs/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/lookup"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/xattrs"
"github.com/cs3org/reva/v2/pkg/storage/utils/templates"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/cs3org/reva/v2/pkg/utils/resourceid"
"github.com/google/uuid"
Expand Down Expand Up @@ -69,11 +70,15 @@ func (fs *Decomposedfs) CreateStorageSpace(ctx context.Context, req *provider.Cr
description := utils.ReadPlainFromOpaque(req.Opaque, "description")
// allow sending a spaceAlias
alias := utils.ReadPlainFromOpaque(req.Opaque, "spaceAlias")
u := ctxpkg.ContextMustGetUser(ctx)
if alias == "" {
alias = templates.WithSpacePropertiesAndUser(u, req.Type, req.Name, fs.o.GeneralSpaceAliasTemplate)
}
// TODO enforce a uuid?
// TODO clarify if we want to enforce a single personal storage space or if we want to allow sending the spaceid
if req.Type == spaceTypePersonal {
spaceID = req.GetOwner().GetId().GetOpaqueId()
alias = spaceTypePersonal + "/" + req.GetOwner().GetUsername()
alias = templates.WithSpacePropertiesAndUser(u, req.Type, req.Name, fs.o.PersonalSpaceAliasTemplate)
}

root, err := node.ReadNode(ctx, fs.lu, spaceID, spaceID)
Expand Down
38 changes: 38 additions & 0 deletions pkg/storage/utils/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type UserData struct {
Email EmailData
}

type SpaceData struct {
*UserData
SpaceType string
SpaceName string
}

// EmailData contains mail data
// split into local and domain part.
// It is extracted from splitting the username by @.
Expand All @@ -69,8 +75,30 @@ func WithUser(u *userpb.User, tpl string) string {
return b.String()
}

// WithSpacePropertiesAndUser generates a layout based on user data and a space type.
func WithSpacePropertiesAndUser(u *userpb.User, spaceType string, spaceName string, tpl string) string {
tpl = clean(tpl)
sd := newSpaceData(u, spaceType, spaceName)
// compile given template tpl
t, err := template.New("tpl").Funcs(sprig.TxtFuncMap()).Parse(tpl)
if err != nil {
err := errors.Wrap(err, fmt.Sprintf("error parsing template: spaceanduser_template:%+v tpl:%s", sd, tpl))
panic(err)
}
b := bytes.Buffer{}
if err := t.Execute(&b, sd); err != nil {
err := errors.Wrap(err, fmt.Sprintf("error executing template: spaceanduser_template:%+v tpl:%s", sd, tpl))
panic(err)
}
return b.String()
}

func newUserData(u *userpb.User) *UserData {
usernameSplit := strings.Split(u.Username, "@")
if u.Mail != "" {
usernameSplit = strings.Split(u.Mail, "@")
}

if len(usernameSplit) == 1 {
usernameSplit = append(usernameSplit, "_unknown")
}
Expand All @@ -88,6 +116,16 @@ func newUserData(u *userpb.User) *UserData {
return ut
}

func newSpaceData(u *userpb.User, st string, n string) *SpaceData {
userData := newUserData(u)
sd := &SpaceData{
userData,
st,
n,
}
return sd
}

func clean(a string) string {
return path.Clean(a)
}

0 comments on commit 338dfff

Please sign in to comment.