Permissions (untested)

This commit is contained in:
Abhishek Shroff
2024-08-04 00:54:33 +05:30
parent b7e943a8a3
commit 3610418019
5 changed files with 24 additions and 17 deletions

View File

@@ -34,6 +34,6 @@ func CreateBasicAuthHandler(app *app.App) func(c *gin.Context) {
c.Status(http.StatusUnauthorized)
return
}
c.Set(keyUsername, user)
c.Set(keyUsername, user.Username)
}
}

View File

@@ -55,6 +55,7 @@ type resource struct {
parentID *uuid.UUID
name string
size int64
username string
permission int
collection bool
modTime time.Time
@@ -116,7 +117,7 @@ func (r resource) ReadDir(ctx context.Context) ([]Resource, error) {
if r.permission < PermissionReadOnly {
return nil, ErrInsufficientPermissions
}
children, err := r.db.Queries().ReadDir(ctx, sql.ReadDirParams{ID: r.id, IncludeRoot: false, Recursive: false})
children, err := r.db.Queries().ReadDir(ctx, sql.ReadDirParams{ID: r.id, Username: r.username, IncludeRoot: false, Recursive: false})
if err != nil {
return nil, err
}
@@ -132,6 +133,7 @@ func (r resource) ReadDir(ctx context.Context) ([]Resource, error) {
db: r.db,
storage: r.storage,
id: c.ID,
username: r.username,
permission: permission,
parentID: &r.id,
name: c.Name,
@@ -167,6 +169,7 @@ func (r resource) CreateMemberResource(ctx context.Context, id uuid.UUID, name s
storage: r.storage,
id: id,
parentID: &r.id,
username: r.username,
permission: r.permission,
name: result.Name,
size: 0,

View File

@@ -15,7 +15,7 @@ type Silo interface {
Name() string
Owner() string
StorageName() string
ResourceByPath(ctx context.Context, path string) (Resource, error)
ResourceByPath(ctx context.Context, path, username string) (Resource, error)
Move(ctx context.Context, id uuid.UUID, parent uuid.UUID, name string) error
}
@@ -57,7 +57,7 @@ func (s *silo) Move(ctx context.Context, id uuid.UUID, parent uuid.UUID, name st
return s.db.Queries().Rename(ctx, sql.RenameParams{ID: id, Parent: parent, Name: name})
}
func (s *silo) ResourceByPath(ctx context.Context, path string) (Resource, error) {
func (s *silo) ResourceByPath(ctx context.Context, path, username string) (Resource, error) {
path = strings.Trim(path, "/")
segments := strings.Split(path, "/")
if path == "" {
@@ -65,7 +65,7 @@ func (s *silo) ResourceByPath(ctx context.Context, path string) (Resource, error
segments = []string{}
}
res, err := s.db.Queries().ResourceByPath(ctx, sql.ResourceByPathParams{Root: s.root, Search: segments})
res, err := s.db.Queries().ResourceByPath(ctx, sql.ResourceByPathParams{Root: s.root, Search: segments, Username: username})
if err != nil {
return nil, fs.ErrNotExist
}
@@ -78,6 +78,7 @@ func (s *silo) ResourceByPath(ctx context.Context, path string) (Resource, error
db: s.db,
storage: s.storage,
id: res.ID,
username: username,
permission: int(res.Permission.Int32),
parentID: res.Parent,
name: res.Name,

View File

@@ -90,7 +90,7 @@ func (a App) DeleteSilo(ctx context.Context, id uuid.UUID) error {
}
silo := core.NewSilo(a.Db, result.Name, result.Owner, result.ID, storage)
resource, err := silo.ResourceByPath(ctx, "/")
resource, err := silo.ResourceByPath(ctx, "/", "admin")
if err != nil {
return err
}

View File

@@ -65,24 +65,27 @@ func (h *handler) HandleRequest(c *gin.Context) {
return
}
username := auth.GetUsername(c)
webdavHandler := webdav.Handler{
Prefix: h.prefix + "/" + identifier,
FileSystem: adapter{silo},
FileSystem: adapter{silo: silo, username: username},
LockSystem: webdav.NewMemLS(),
}
webdavHandler.ServeHTTP(c.Writer, c.Request)
}
type adapter struct {
silo core.Silo
silo core.Silo
username string
}
func (a adapter) Stat(ctx context.Context, name string) (core.Resource, error) {
return a.silo.ResourceByPath(ctx, name)
return a.silo.ResourceByPath(ctx, name, a.username)
}
func (a adapter) OpenWrite(ctx context.Context, name string) (io.WriteCloser, error) {
resource, err := a.silo.ResourceByPath(ctx, name)
resource, err := a.Stat(ctx, name)
if err != nil {
if err == fs.ErrNotExist {
@@ -90,7 +93,7 @@ func (a adapter) OpenWrite(ctx context.Context, name string) (io.WriteCloser, er
name = strings.TrimRight(name, "/")
index := strings.LastIndex(name, "/")
parentPath := name[0:index]
parent, err := a.silo.ResourceByPath(ctx, parentPath)
parent, err := a.Stat(ctx, parentPath)
if err != nil {
return nil, fs.ErrNotExist
}
@@ -111,7 +114,7 @@ func (a adapter) OpenWrite(ctx context.Context, name string) (io.WriteCloser, er
}
func (a adapter) RemoveAll(ctx context.Context, name string) error {
resource, err := a.silo.ResourceByPath(ctx, name)
resource, err := a.Stat(ctx, name)
if err != nil {
return fs.ErrNotExist
}
@@ -119,13 +122,13 @@ func (a adapter) RemoveAll(ctx context.Context, name string) error {
}
func (a adapter) Mkdir(ctx context.Context, name string) error {
if _, err := a.silo.ResourceByPath(ctx, name); err == nil {
if _, err := a.Stat(ctx, name); err == nil {
return fs.ErrExist
}
name = strings.TrimRight(name, "/")
index := strings.LastIndex(name, "/")
parentPath := name[0:index]
parent, err := a.silo.ResourceByPath(ctx, parentPath)
parent, err := a.Stat(ctx, parentPath)
if err != nil {
return fs.ErrNotExist
}
@@ -135,12 +138,12 @@ func (a adapter) Mkdir(ctx context.Context, name string) error {
}
func (a adapter) Rename(ctx context.Context, oldName, newName string) error {
src, err := a.silo.ResourceByPath(ctx, oldName)
src, err := a.Stat(ctx, oldName)
if err != nil {
return fs.ErrNotExist
}
_, err = a.silo.ResourceByPath(ctx, newName)
_, err = a.Stat(ctx, newName)
if err == nil {
return fs.ErrExist
}
@@ -148,7 +151,7 @@ func (a adapter) Rename(ctx context.Context, oldName, newName string) error {
newName = strings.TrimRight(newName, "/")
index := strings.LastIndex(newName, "/")
parentPath := newName[0:index]
parent, err := a.silo.ResourceByPath(ctx, parentPath)
parent, err := a.Stat(ctx, parentPath)
newName = newName[index+1:]
if err != nil {