mirror of https://github.com/Xhofe/alist
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
parent
74e384175b
commit
a9fcd51bc4
|
@ -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
|
||||||
|
|
|
@ -88,12 +88,12 @@ var settingItemHooks = map[string]SettingItemHook{
|
||||||
if v == "" {
|
if v == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
r, err := GetRoleByName(v)
|
id, err := strconv.Atoi(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
item.Value = strconv.Itoa(int(r.ID))
|
_, err = GetRole(uint(id))
|
||||||
return nil
|
return err
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,12 @@ func getRoleOptions() string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
names := make([]string, len(roles))
|
names := make([]string, 0, len(roles))
|
||||||
for i, r := range roles {
|
for _, r := range roles {
|
||||||
names[i] = r.Name
|
if r.Name == "admin" || r.Name == "guest" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
names = append(names, r.Name)
|
||||||
}
|
}
|
||||||
return strings.Join(names, ",")
|
return strings.Join(names, ",")
|
||||||
}
|
}
|
||||||
|
@ -49,6 +52,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 +69,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
|
||||||
}
|
}
|
||||||
|
@ -75,6 +88,22 @@ func SaveSettings(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
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 {
|
if err := op.SaveSettingItems(req); err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
} else {
|
} else {
|
||||||
|
@ -114,6 +143,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