feat: file permission.

This commit is contained in:
2026-04-28 21:09:27 +08:00
parent f6c06bd7ad
commit 641703ca69
5 changed files with 53 additions and 1 deletions
+8
View File
@@ -74,8 +74,11 @@ func (lc *LessonPlanController) Upload(c *gin.Context) {
return
}
tempPath := tempFile.Name()
tempClosed := false
defer func() {
if !tempClosed {
_ = tempFile.Close()
}
if _, statErr := os.Stat(tempPath); statErr == nil {
_ = os.Remove(tempPath)
}
@@ -104,6 +107,11 @@ func (lc *LessonPlanController) Upload(c *gin.Context) {
storedFilename := buildStoredLessonPlanFilename(md5Value, fileHeader.Filename)
finalPath := filepath.Join(lessonPlanStorageDir, storedFilename)
if err := tempFile.Close(); err != nil {
writeError(c, http.StatusInternalServerError, "failed to finalize upload")
return
}
tempClosed = true
if err := os.Rename(tempPath, finalPath); err != nil {
writeError(c, http.StatusInternalServerError, "failed to finalize upload")
return
+3
View File
@@ -38,6 +38,9 @@ func main() {
if err := models.BackfillLegacyUserPermissions(config.DB); err != nil {
log.Printf("legacy user permission backfill failed: %v", err)
}
if err := models.EnsureDefaultAdmin(config.DB); err != nil {
log.Printf("default admin init failed: %v", err)
}
if err := mqtt.Start(config.DB, config.App.MQTT); err != nil {
log.Printf("mqtt listener start failed: %v", err)
+4
View File
@@ -28,6 +28,10 @@ func RequireHeartRateOperatorOrHigher() gin.HandlerFunc {
c.Abort()
return
}
if role == models.UserRoleSuperAdmin {
c.Next()
return
}
flavorValue, exists := c.Get("flavorType")
if !exists {
+4
View File
@@ -29,6 +29,10 @@ func RequireStepTrainingAccess() gin.HandlerFunc {
c.Abort()
return
}
if role == models.UserRoleSuperAdmin {
c.Next()
return
}
flavorValue, exists := c.Get("flavorType")
if !exists {
+33
View File
@@ -0,0 +1,33 @@
package models
import (
"errors"
"gorm.io/gorm"
)
const (
defaultAdminUsername = "admin"
defaultAdminPassword = "123456"
)
func EnsureDefaultAdmin(db *gorm.DB) error {
var user User
err := db.Where("username = ?", defaultAdminUsername).First(&user).Error
if err == nil {
return nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
admin := User{
Username: defaultAdminUsername,
Password: defaultAdminPassword,
Role: UserRoleSuperAdmin,
FlavorType: UserFlavorAll,
IsActive: true,
}
return db.Create(&admin).Error
}