Skip to content

Commit b14d2b4

Browse files
committed
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.
1 parent 74e3841 commit b14d2b4

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

internal/bootstrap/data/setting.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package data
22

33
import (
44
"strconv"
5-
"strings"
65

76
"github.com/alist-org/alist/v3/cmd/flags"
87
"github.com/alist-org/alist/v3/internal/conf"
@@ -92,21 +91,7 @@ func InitialSettings() []model.SettingItem {
9291
} else {
9392
token = random.Token()
9493
}
95-
roles, _, err := op.GetRoles(1, model.MaxInt)
96-
if err != nil {
97-
utils.Log.Fatalf("failed get roles: %+v", err)
98-
}
99-
roleNames := make([]string, len(roles))
100-
defaultRoleID := ""
101-
for i, role := range roles {
102-
roleNames[i] = role.Name
103-
if role.Name == "guest" {
104-
defaultRoleID = strconv.Itoa(int(role.ID))
105-
}
106-
}
107-
if defaultRoleID == "" && len(roles) > 0 {
108-
defaultRoleID = strconv.Itoa(int(roles[0].ID))
109-
}
94+
defaultRoleID := strconv.Itoa(model.GUEST)
11095
initialSettingItems = []model.SettingItem{
11196
// site settings
11297
{Key: conf.VERSION, Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
@@ -120,7 +105,7 @@ func InitialSettings() []model.SettingItem {
120105
{Key: conf.AllowMounted, Value: "true", Type: conf.TypeBool, Group: model.SITE},
121106
{Key: conf.RobotsTxt, Value: "User-agent: *\nAllow: /", Type: conf.TypeText, Group: model.SITE},
122107
{Key: conf.AllowRegister, Value: "false", Type: conf.TypeBool, Group: model.SITE},
123-
{Key: conf.DefaultRole, Value: defaultRoleID, Type: conf.TypeSelect, Options: strings.Join(roleNames, ","), Group: model.SITE},
108+
{Key: conf.DefaultRole, Value: defaultRoleID, Type: conf.TypeSelect, Group: model.SITE},
124109
// newui settings
125110
{Key: conf.UseNewui, Value: "false", Type: conf.TypeBool, Group: model.SITE},
126111
// style settings

server/handles/setting.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func GetSetting(c *gin.Context) {
4949
if item.Key == conf.DefaultRole {
5050
copy := *item
5151
copy.Options = getRoleOptions()
52+
if id, err := strconv.Atoi(copy.Value); err == nil {
53+
if r, err := op.GetRole(uint(id)); err == nil {
54+
copy.Value = r.Name
55+
}
56+
}
5257
common.SuccessResp(c, copy)
5358
return
5459
}
@@ -61,6 +66,11 @@ func GetSetting(c *gin.Context) {
6166
}
6267
for i := range items {
6368
if items[i].Key == conf.DefaultRole {
69+
if id, err := strconv.Atoi(items[i].Value); err == nil {
70+
if r, err := op.GetRole(uint(id)); err == nil {
71+
items[i].Value = r.Name
72+
}
73+
}
6474
items[i].Options = getRoleOptions()
6575
break
6676
}
@@ -114,6 +124,11 @@ func ListSettings(c *gin.Context) {
114124
}
115125
for i := range settings {
116126
if settings[i].Key == conf.DefaultRole {
127+
if id, err := strconv.Atoi(settings[i].Value); err == nil {
128+
if r, err := op.GetRole(uint(id)); err == nil {
129+
settings[i].Value = r.Name
130+
}
131+
}
117132
settings[i].Options = getRoleOptions()
118133
break
119134
}

0 commit comments

Comments
 (0)