feat: assign per-user WebDAV paths
This commit is contained in:
+40
-2
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/studio-b12/gowebdav"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func cleanWebDAVRoot(root string) string {
|
||||
@@ -18,6 +19,28 @@ func cleanWebDAVRoot(root string) string {
|
||||
return strings.Trim(root, "/")
|
||||
}
|
||||
|
||||
func (a *App) initializeUserWebDAVDefaults() error {
|
||||
var users []User
|
||||
if err := a.db.Where("role <> ?", RoleAdmin).Find(&users).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, user := range users {
|
||||
var cfg WebDAVConfig
|
||||
err := a.db.Where("user_id = ?", user.ID).First(&cfg).Error
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
root := cleanWebDAVRoot(strings.TrimRight(a.cfg.WebDAVPath, "/") + "/" + user.Username)
|
||||
if err := a.db.Create(&WebDAVConfig{UserID: user.ID, Enabled: true, RootPath: root}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) getUserWebDAV(c *gin.Context) {
|
||||
user := c.MustGet("user").(*User)
|
||||
var cfg WebDAVConfig
|
||||
@@ -98,6 +121,10 @@ func (a *App) storageForTask(t *Task) (RemoteStorage, string, bool) {
|
||||
if a.cfg.WebDAVURL == "" || a.storage == nil {
|
||||
return nil, "", false
|
||||
}
|
||||
var cfg WebDAVConfig
|
||||
if a.db.Where("user_id = ?", user.ID).First(&cfg).Error == nil && cfg.RootPath != "" {
|
||||
return a.storage, cfg.RootPath, true
|
||||
}
|
||||
return a.storage, a.cfg.WebDAVPath, true
|
||||
}
|
||||
if user.Role == RoleAdmin {
|
||||
@@ -107,13 +134,24 @@ func (a *App) storageForTask(t *Task) (RemoteStorage, string, bool) {
|
||||
return a.storage, a.cfg.WebDAVPath, true
|
||||
}
|
||||
var cfg WebDAVConfig
|
||||
if a.db.Where("user_id = ? AND enabled = ?", user.ID, true).First(&cfg).Error != nil || cfg.URL == "" {
|
||||
if a.db.Where("user_id = ? AND enabled = ?", user.ID, true).First(&cfg).Error != nil {
|
||||
// Privileged users fall back to the global WebDAV until a custom
|
||||
// WebDAV configuration is enabled and tested successfully.
|
||||
// WebDAV configuration is enabled.
|
||||
if a.cfg.WebDAVURL == "" || a.storage == nil {
|
||||
return nil, "", false
|
||||
}
|
||||
return a.storage, a.cfg.WebDAVPath, true
|
||||
}
|
||||
if cfg.URL == "" {
|
||||
// An empty URL means this user inherits the global WebDAV connection,
|
||||
// while keeping the user's own destination path.
|
||||
if a.cfg.WebDAVURL == "" || a.storage == nil {
|
||||
return nil, "", false
|
||||
}
|
||||
if cfg.RootPath != "" {
|
||||
return a.storage, cfg.RootPath, true
|
||||
}
|
||||
return a.storage, a.cfg.WebDAVPath, true
|
||||
}
|
||||
return NewWebDAVStorage(Config{WebDAVURL: cfg.URL, WebDAVUser: cfg.Username, WebDAVPass: cfg.Password}), cfg.RootPath, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user