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

* 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.

* fix(setting): ensure DefaultRole stores role ID while exposing role
name in APIs (fix/settings-get-role)

- Simplify initial settings to use `model.GUEST` as the default role ID
  instead of querying roles at startup.
- Update `GetSetting`, `ListSettings` handlers to:
  - Convert stored role ID into the corresponding role name when
    returning data.
  - Preserve dynamic role options for selection.
- Remove unused `strings` import and role preloading logic from
  `InitialSettings`.
- Avoid DB dependency during initialization while keeping consistent
  role display for frontend clients.
main beta
千石 2025-08-19 15:01:32 +08:00 committed by GitHub
parent 74e384175b
commit a9fcd51bc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 24 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

@ -88,12 +88,12 @@ var settingItemHooks = map[string]SettingItemHook{
if v == "" {
return nil
}
r, err := GetRoleByName(v)
id, err := strconv.Atoi(v)
if err != nil {
return err
return errors.WithStack(err)
}
item.Value = strconv.Itoa(int(r.ID))
return nil
_, err = GetRole(uint(id))
return err
},
}

View File

@ -19,9 +19,12 @@ func getRoleOptions() string {
if err != nil {
return ""
}
names := make([]string, len(roles))
for i, r := range roles {
names[i] = r.Name
names := make([]string, 0, len(roles))
for _, r := range roles {
if r.Name == "admin" || r.Name == "guest" {
continue
}
names = append(names, r.Name)
}
return strings.Join(names, ",")
}
@ -49,6 +52,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 +69,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
}
@ -75,6 +88,22 @@ func SaveSettings(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
for i := range req {
if req[i].Key == conf.DefaultRole {
role, err := op.GetRoleByName(req[i].Value)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if role.Name == "admin" || role.Name == "guest" {
common.ErrorStrResp(c, "cannot set admin or guest as default role", 400)
return
}
req[i].Value = strconv.Itoa(int(role.ID))
}
}
if err := op.SaveSettingItems(req); err != nil {
common.ErrorResp(c, err, 500)
} else {
@ -114,6 +143,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
}