feat: file permission.
This commit is contained in:
@@ -74,8 +74,11 @@ func (lc *LessonPlanController) Upload(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
tempPath := tempFile.Name()
|
tempPath := tempFile.Name()
|
||||||
|
tempClosed := false
|
||||||
defer func() {
|
defer func() {
|
||||||
|
if !tempClosed {
|
||||||
_ = tempFile.Close()
|
_ = tempFile.Close()
|
||||||
|
}
|
||||||
if _, statErr := os.Stat(tempPath); statErr == nil {
|
if _, statErr := os.Stat(tempPath); statErr == nil {
|
||||||
_ = os.Remove(tempPath)
|
_ = os.Remove(tempPath)
|
||||||
}
|
}
|
||||||
@@ -104,6 +107,11 @@ func (lc *LessonPlanController) Upload(c *gin.Context) {
|
|||||||
|
|
||||||
storedFilename := buildStoredLessonPlanFilename(md5Value, fileHeader.Filename)
|
storedFilename := buildStoredLessonPlanFilename(md5Value, fileHeader.Filename)
|
||||||
finalPath := filepath.Join(lessonPlanStorageDir, storedFilename)
|
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 {
|
if err := os.Rename(tempPath, finalPath); err != nil {
|
||||||
writeError(c, http.StatusInternalServerError, "failed to finalize upload")
|
writeError(c, http.StatusInternalServerError, "failed to finalize upload")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -38,6 +38,9 @@ func main() {
|
|||||||
if err := models.BackfillLegacyUserPermissions(config.DB); err != nil {
|
if err := models.BackfillLegacyUserPermissions(config.DB); err != nil {
|
||||||
log.Printf("legacy user permission backfill failed: %v", err)
|
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 {
|
if err := mqtt.Start(config.DB, config.App.MQTT); err != nil {
|
||||||
log.Printf("mqtt listener start failed: %v", err)
|
log.Printf("mqtt listener start failed: %v", err)
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ func RequireHeartRateOperatorOrHigher() gin.HandlerFunc {
|
|||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if role == models.UserRoleSuperAdmin {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
flavorValue, exists := c.Get("flavorType")
|
flavorValue, exists := c.Get("flavorType")
|
||||||
if !exists {
|
if !exists {
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ func RequireStepTrainingAccess() gin.HandlerFunc {
|
|||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if role == models.UserRoleSuperAdmin {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
flavorValue, exists := c.Get("flavorType")
|
flavorValue, exists := c.Get("flavorType")
|
||||||
if !exists {
|
if !exists {
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user