Skip to content

Commit

Permalink
add option to hide public shares
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Oct 4, 2022
1 parent 041edc2 commit 192974e
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 62 deletions.
2 changes: 2 additions & 0 deletions cmd/reva/public-share-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func publicShareCreateCommand() *command {
cmd.Usage = func() string { return "Usage: public-share-create [-flags] <path>" }
rol := cmd.String("rol", "viewer", "the permission for the share (viewer or editor)")
description := cmd.String("description", "", "the description for the share")
internal := cmd.Bool("internal", false, "mark the public share as internal")

cmd.ResetFlags = func() {
*rol, *description = "viewer", ""
Expand Down Expand Up @@ -80,6 +81,7 @@ func publicShareCreateCommand() *command {
ResourceInfo: res.Info,
Grant: grant,
Description: *description,
Internal: *internal,
}

shareRes, err := client.CreatePublicShare(ctx, shareRequest)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
go 1.16

replace (
github.com/cs3org/go-cs3apis => github.com/gmgigi96/go-cs3apis v0.0.0-20221004074314-b2292f6a794c
github.com/eventials/go-tus => github.com/andrewmostello/go-tus v0.0.0-20200314041820-904a9904af9a
github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e h1:tqSPWQeueWTKnJVMJffz4pz0o1WuQxJ28+5x5JgaHD8=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e/go.mod h1:XJEZ3/EQuI3BXTp/6DUzFr850vlxq11I6satRtz0YQ4=
github.com/cs3org/go-cs3apis v0.0.0-20220930140901-5777bc1ccfaa h1:MDbQLEHxlgEB9Xca1NKMqKR4c1r5S94SoP5hEkWLPC8=
github.com/cs3org/go-cs3apis v0.0.0-20220930140901-5777bc1ccfaa/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8 h1:Z9lwXumT5ACSmJ7WGnFl+OMLLjpz5uR2fyz7dC255FI=
github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8/go.mod h1:4abs/jPXcmJzYoYGF91JF9Uq9s/KL5n1jvFDix8KcqY=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
Expand Down Expand Up @@ -283,6 +281,8 @@ github.com/gdexlab/go-render v1.0.1/go.mod h1:wRi5nW2qfjiGj4mPukH4UV0IknS1cHD4Vg
github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/gmgigi96/go-cs3apis v0.0.0-20221004074314-b2292f6a794c h1:mC9jwnDK3weg0S3md2euO0iIvlD33JE/MsFQGaZ/zX0=
github.com/gmgigi96/go-cs3apis v0.0.0-20221004074314-b2292f6a794c/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/go-acme/lego/v4 v4.4.0/go.mod h1:l3+tFUFZb590dWcqhWZegynUthtaHJbG2fevUpoOOE0=
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *service) CreatePublicShare(ctx context.Context, req *link.CreatePublicS
log.Error().Msg("error getting user from context")
}

share, err := s.sm.CreatePublicShare(ctx, u, req.ResourceInfo, req.Grant, req.Description)
share, err := s.sm.CreatePublicShare(ctx, u, req.ResourceInfo, req.Grant, req.Description, req.Internal)
if err != nil {
log.Debug().Err(err).Str("createShare", "shares").Msg("error connecting to storage provider")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (h *Handler) createPublicLinkShare(w http.ResponseWriter, r *http.Request,
newPermissions = conversions.RoleFromOCSPermissions(permissions).CS3ResourcePermissions()
}

internal, _ := strconv.ParseBool(r.FormValue("internal"))

req := link.CreatePublicShareRequest{
ResourceInfo: statInfo,
Grant: &link.Grant{
Expand All @@ -119,6 +121,7 @@ func (h *Handler) createPublicLinkShare(w http.ResponseWriter, r *http.Request,
Password: r.FormValue("password"),
},
Description: r.FormValue("description"),
Internal: internal,
}

expireTimeString, ok := r.Form["expireDate"]
Expand Down
71 changes: 15 additions & 56 deletions pkg/cbox/publicshare/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,20 @@ func init() {
}

type config struct {
SharePasswordHashCost int `mapstructure:"password_hash_cost"`
JanitorRunInterval int `mapstructure:"janitor_run_interval"`
EnableExpiredSharesCleanup bool `mapstructure:"enable_expired_shares_cleanup"`
DbUsername string `mapstructure:"db_username"`
DbPassword string `mapstructure:"db_password"`
DbHost string `mapstructure:"db_host"`
DbPort int `mapstructure:"db_port"`
DbName string `mapstructure:"db_name"`
GatewaySvc string `mapstructure:"gatewaysvc"`
HiddenTags []string `mapstructure:"hidden_tags"`
SharePasswordHashCost int `mapstructure:"password_hash_cost"`
JanitorRunInterval int `mapstructure:"janitor_run_interval"`
EnableExpiredSharesCleanup bool `mapstructure:"enable_expired_shares_cleanup"`
DbUsername string `mapstructure:"db_username"`
DbPassword string `mapstructure:"db_password"`
DbHost string `mapstructure:"db_host"`
DbPort int `mapstructure:"db_port"`
DbName string `mapstructure:"db_name"`
GatewaySvc string `mapstructure:"gatewaysvc"`
}

type manager struct {
c *config
db *sql.DB
hiddenTags struct {
query string
params []interface{}
}
c *config
db *sql.DB
}

func (c *config) init() {
Expand Down Expand Up @@ -124,43 +119,16 @@ func New(m map[string]interface{}) (publicshare.Manager, error) {
return nil, err
}

hdnQuery, hdnParams := hiddenTagsQuery(c.HiddenTags)

mgr := manager{
c: c,
db: db,
hiddenTags: struct {
query string
params []interface{}
}{
query: hdnQuery,
params: hdnParams,
},
}
go mgr.startJanitorRun()

return &mgr, nil
}

func hiddenTagsQuery(hiddenTags []string) (string, []interface{}) {
query := ""
for range hiddenTags {
query += "?,"
}
if query != "" {
// remove last ,
query = query[:len(query)-1]
}

params := make([]interface{}, 0, len(hiddenTags))
for _, t := range hiddenTags {
params = append(params, t)
}
query = fmt.Sprintf(" AND description NOT IN (%s)", query)
return query, params
}

func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant, description string) (*link.PublicShare, error) {
func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant, description string, internal bool) (*link.PublicShare, error) {

tkn := utils.RandString(15)
now := time.Now().Unix()
Expand Down Expand Up @@ -188,8 +156,8 @@ func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *pr
fileSource = 0
}

query := "insert into oc_share set share_type=?,uid_owner=?,uid_initiator=?,item_type=?,fileid_prefix=?,item_source=?,file_source=?,permissions=?,stime=?,token=?,share_name=?,quicklink=?,description=?"
params := []interface{}{publicShareType, owner, creator, itemType, prefix, itemSource, fileSource, permissions, now, tkn, displayName, quicklink, description}
query := "insert into oc_share set share_type=?,uid_owner=?,uid_initiator=?,item_type=?,fileid_prefix=?,item_source=?,file_source=?,permissions=?,stime=?,token=?,share_name=?,quicklink=?,description=?,internal=?"
params := []interface{}{publicShareType, owner, creator, itemType, prefix, itemSource, fileSource, permissions, now, tkn, displayName, quicklink, description, internal}

var passwordProtected bool
password := g.Password
Expand Down Expand Up @@ -358,15 +326,8 @@ func (m *manager) GetPublicShare(ctx context.Context, u *user.User, ref *link.Pu
return s, nil
}

func (m *manager) filterHiddenTagsQuery(query *string, params *[]interface{}) {
if len(m.hiddenTags.params) != 0 {
*query += m.hiddenTags.query
*params = append(*params, m.hiddenTags.params...)
}
}

func (m *manager) ListPublicShares(ctx context.Context, u *user.User, filters []*link.ListPublicSharesRequest_Filter, md *provider.ResourceInfo, sign bool) ([]*link.PublicShare, error) {
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, coalesce(token,'') as token, coalesce(expiration, '') as expiration, coalesce(share_name, '') as share_name, id, stime, permissions, quicklink, description FROM oc_share WHERE (orphan = 0 or orphan IS NULL) AND (share_type=?)"
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, coalesce(token,'') as token, coalesce(expiration, '') as expiration, coalesce(share_name, '') as share_name, id, stime, permissions, quicklink, description FROM oc_share WHERE (orphan = 0 or orphan IS NULL) AND (share_type=?) AND internal=false"
var resourceFilters, ownerFilters, creatorFilters string
var resourceParams, ownerParams, creatorParams []interface{}
params := []interface{}{publicShareType}
Expand Down Expand Up @@ -415,8 +376,6 @@ func (m *manager) ListPublicShares(ctx context.Context, u *user.User, filters []
query = fmt.Sprintf("%s AND (%s)", query, uidOwnersQuery)
}

m.filterHiddenTagsQuery(&query, &params)

rows, err := m.db.Query(query, params...)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/publicshare/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (m *manager) startJanitorRun() {
}

// CreatePublicShare adds a new entry to manager.shares
func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant, description string) (*link.PublicShare, error) {
func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant, description string, internal bool) (*link.PublicShare, error) {
id := &link.PublicShareId{
OpaqueId: utils.RandString(15),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/publicshare/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
)

// CreatePublicShare adds a new entry to manager.shares
func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant, description string) (*link.PublicShare, error) {
func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant, description string, internal bool) (*link.PublicShare, error) {
id := &link.PublicShareId{
OpaqueId: randString(15),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/publicshare/publicshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

// Manager manipulates public shares.
type Manager interface {
CreatePublicShare(ctx context.Context, u *user.User, md *provider.ResourceInfo, g *link.Grant, description string) (*link.PublicShare, error)
CreatePublicShare(ctx context.Context, u *user.User, md *provider.ResourceInfo, g *link.Grant, description string, internal bool) (*link.PublicShare, error)
UpdatePublicShare(ctx context.Context, u *user.User, req *link.UpdatePublicShareRequest, g *link.Grant) (*link.PublicShare, error)
GetPublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference, sign bool) (*link.PublicShare, error)
ListPublicShares(ctx context.Context, u *user.User, filters []*link.ListPublicSharesRequest_Filter, md *provider.ResourceInfo, sign bool) ([]*link.PublicShare, error)
Expand Down

0 comments on commit 192974e

Please sign in to comment.