feat: assign per-user WebDAV paths
This commit is contained in:
+17
-3
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -14,6 +15,8 @@ func (a *App) audit(operator uint, action, targetType, targetID, metadata string
|
||||
_ = a.db.Create(&AuditLog{OperatorID: operator, Action: action, TargetType: targetType, TargetID: targetID, Metadata: metadata}).Error
|
||||
}
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[A-Za-z]{4,10}$`)
|
||||
|
||||
func defaultPermissions(role string) []string {
|
||||
if role == RolePrivileged {
|
||||
return []string{PermDownload, PermRedeem, PermWebDAV, PermWebDAVConfig}
|
||||
@@ -59,8 +62,13 @@ func (a *App) adminCreateUser(c *gin.Context) {
|
||||
Role string `json:"role"`
|
||||
Credits int64 `json:"credits"`
|
||||
}
|
||||
if c.BindJSON(&in) != nil || strings.TrimSpace(in.Username) == "" || len(in.Password) < 8 {
|
||||
c.JSON(400, gin.H{"error": "username and password(8+) required"})
|
||||
if c.BindJSON(&in) != nil {
|
||||
c.JSON(400, gin.H{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
in.Username = strings.TrimSpace(in.Username)
|
||||
if !usernamePattern.MatchString(in.Username) || len(in.Password) < 8 {
|
||||
c.JSON(400, gin.H{"error": "username must be 4-10 English letters and password must be 8+ characters"})
|
||||
return
|
||||
}
|
||||
if in.Role == "" {
|
||||
@@ -78,10 +86,16 @@ func (a *App) adminCreateUser(c *gin.Context) {
|
||||
admin := c.MustGet("user").(*User)
|
||||
var u User
|
||||
err = a.db.Transaction(func(tx *gorm.DB) error {
|
||||
u = User{Username: strings.TrimSpace(in.Username), PasswordHash: hash, Role: in.Role, Status: UserActive}
|
||||
u = User{Username: in.Username, PasswordHash: hash, Role: in.Role, Status: UserActive}
|
||||
if err := tx.Create(&u).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if in.Role != RoleAdmin {
|
||||
root := cleanWebDAVRoot(strings.TrimRight(a.cfg.WebDAVPath, "/") + "/" + u.Username)
|
||||
if err := tx.Create(&WebDAVConfig{UserID: u.ID, Enabled: true, RootPath: root}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Create(&CreditAccount{UserID: u.ID, Balance: in.Credits}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user