feat: user migration

This commit is contained in:
2026-04-28 19:06:42 +08:00
parent aa90b10f06
commit f9077dafcf
6 changed files with 114 additions and 4 deletions
+4
View File
@@ -19,6 +19,7 @@ type UserFlavorType string
const (
UserFlavorAll UserFlavorType = "all"
UserFlavorFlink UserFlavorType = "flink"
UserFlavorFull UserFlavorType = "full"
UserFlavorLight UserFlavorType = "light"
UserFlavorHeartRate UserFlavorType = "heartrate"
@@ -87,6 +88,9 @@ func (u *User) SupportsFlavor(flavor string) bool {
if u.FlavorType == UserFlavorAll {
return true
}
if u.FlavorType == UserFlavorFlink {
return flavor == string(UserFlavorFlink) || flavor == string(UserFlavorFull) || flavor == string(UserFlavorLight)
}
return string(u.FlavorType) == flavor
}
+25
View File
@@ -0,0 +1,25 @@
package models
import "gorm.io/gorm"
func BackfillLegacyUserPermissions(db *gorm.DB) error {
if err := db.Model(&User{}).
Where("role IS NULL OR role = '' OR role = ?", UserRoleViewer).
Update("role", UserRoleOperator).Error; err != nil {
return err
}
if err := db.Model(&User{}).
Where("flavor_type IS NULL OR flavor_type = '' OR flavor_type = ?", UserFlavorAll).
Update("flavor_type", UserFlavorFlink).Error; err != nil {
return err
}
if err := db.Model(&User{}).
Where("is_active IS NULL").
Update("is_active", true).Error; err != nil {
return err
}
return nil
}