fix(setting): ensure DefaultRole stores role ID while exposing role name in APIs

- Simplified initial settings to use `model.GUEST` as the default role ID instead of querying roles at startup.
- Updated `GetSetting`, `ListSettings` handlers to:
  - Convert stored role ID into the corresponding role name when returning data.
  - Preserve dynamic role options for selection.
- Removed unused `strings` import and role preloading logic from `InitialSettings`.
- This change avoids DB dependency during initialization while keeping consistent role display for frontend clients.
pull/9279/head
okatu-loli 2025-08-19 13:51:57 +08:00
parent 74e384175b
commit b14d2b438b
2 changed files with 17 additions and 17 deletions

View File

@ -2,7 +2,6 @@ package data
import (
"strconv"
"strings"
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
@ -92,21 +91,7 @@ func InitialSettings() []model.SettingItem {
} else {
token = random.Token()
}
roles, _, err := op.GetRoles(1, model.MaxInt)
if err != nil {
utils.Log.Fatalf("failed get roles: %+v", err)
}
roleNames := make([]string, len(roles))
defaultRoleID := ""
for i, role := range roles {
roleNames[i] = role.Name
if role.Name == "guest" {
defaultRoleID = strconv.Itoa(int(role.ID))
}
}
if defaultRoleID == "" && len(roles) > 0 {
defaultRoleID = strconv.Itoa(int(roles[0].ID))
}
defaultRoleID := strconv.Itoa(model.GUEST)
initialSettingItems = []model.SettingItem{
// site settings
{Key: conf.VERSION, Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
@ -120,7 +105,7 @@ func InitialSettings() []model.SettingItem {
{Key: conf.AllowMounted, Value: "true", Type: conf.TypeBool, Group: model.SITE},
{Key: conf.RobotsTxt, Value: "User-agent: *\nAllow: /", Type: conf.TypeText, Group: model.SITE},
{Key: conf.AllowRegister, Value: "false", Type: conf.TypeBool, Group: model.SITE},
{Key: conf.DefaultRole, Value: defaultRoleID, Type: conf.TypeSelect, Options: strings.Join(roleNames, ","), Group: model.SITE},
{Key: conf.DefaultRole, Value: defaultRoleID, Type: conf.TypeSelect, Group: model.SITE},
// newui settings
{Key: conf.UseNewui, Value: "false", Type: conf.TypeBool, Group: model.SITE},
// style settings

View File

@ -49,6 +49,11 @@ func GetSetting(c *gin.Context) {
if item.Key == conf.DefaultRole {
copy := *item
copy.Options = getRoleOptions()
if id, err := strconv.Atoi(copy.Value); err == nil {
if r, err := op.GetRole(uint(id)); err == nil {
copy.Value = r.Name
}
}
common.SuccessResp(c, copy)
return
}
@ -61,6 +66,11 @@ func GetSetting(c *gin.Context) {
}
for i := range items {
if items[i].Key == conf.DefaultRole {
if id, err := strconv.Atoi(items[i].Value); err == nil {
if r, err := op.GetRole(uint(id)); err == nil {
items[i].Value = r.Name
}
}
items[i].Options = getRoleOptions()
break
}
@ -114,6 +124,11 @@ func ListSettings(c *gin.Context) {
}
for i := range settings {
if settings[i].Key == conf.DefaultRole {
if id, err := strconv.Atoi(settings[i].Value); err == nil {
if r, err := op.GetRole(uint(id)); err == nil {
settings[i].Value = r.Name
}
}
settings[i].Options = getRoleOptions()
break
}