mirror of https://github.com/Xhofe/alist
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
parent
74e384175b
commit
b14d2b438b
|
@ -2,7 +2,6 @@ package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/cmd/flags"
|
"github.com/alist-org/alist/v3/cmd/flags"
|
||||||
"github.com/alist-org/alist/v3/internal/conf"
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
@ -92,21 +91,7 @@ func InitialSettings() []model.SettingItem {
|
||||||
} else {
|
} else {
|
||||||
token = random.Token()
|
token = random.Token()
|
||||||
}
|
}
|
||||||
roles, _, err := op.GetRoles(1, model.MaxInt)
|
defaultRoleID := strconv.Itoa(model.GUEST)
|
||||||
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))
|
|
||||||
}
|
|
||||||
initialSettingItems = []model.SettingItem{
|
initialSettingItems = []model.SettingItem{
|
||||||
// site settings
|
// site settings
|
||||||
{Key: conf.VERSION, Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
|
{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.AllowMounted, Value: "true", Type: conf.TypeBool, Group: model.SITE},
|
||||||
{Key: conf.RobotsTxt, Value: "User-agent: *\nAllow: /", Type: conf.TypeText, 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.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
|
// newui settings
|
||||||
{Key: conf.UseNewui, Value: "false", Type: conf.TypeBool, Group: model.SITE},
|
{Key: conf.UseNewui, Value: "false", Type: conf.TypeBool, Group: model.SITE},
|
||||||
// style settings
|
// style settings
|
||||||
|
|
|
@ -49,6 +49,11 @@ func GetSetting(c *gin.Context) {
|
||||||
if item.Key == conf.DefaultRole {
|
if item.Key == conf.DefaultRole {
|
||||||
copy := *item
|
copy := *item
|
||||||
copy.Options = getRoleOptions()
|
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)
|
common.SuccessResp(c, copy)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -61,6 +66,11 @@ func GetSetting(c *gin.Context) {
|
||||||
}
|
}
|
||||||
for i := range items {
|
for i := range items {
|
||||||
if items[i].Key == conf.DefaultRole {
|
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()
|
items[i].Options = getRoleOptions()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -114,6 +124,11 @@ func ListSettings(c *gin.Context) {
|
||||||
}
|
}
|
||||||
for i := range settings {
|
for i := range settings {
|
||||||
if settings[i].Key == conf.DefaultRole {
|
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()
|
settings[i].Options = getRoleOptions()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue