mirror of
https://github.com/AlistGo/alist.git
synced 2025-11-25 19:37:41 +08:00
* feat(setting): add role-based default and registration settings (closed #feat/register-and-statistics) - Added `AllowRegister` and `DefaultRole` settings to site configuration. - Integrated dynamic role options for `DefaultRole` using `op.GetRoles`. - Updated `setting.go` handlers to manage `DefaultRole` options dynamically. - Modified `const.go` to include new site settings constants. - Updated dependencies in `go.mod` and `go.sum` to support new functionality. * feat(register-and-statistics): add user registration endpoint - Added `POST /auth/register` endpoint to support user registration. - Implemented registration logic in `auth.go` with dynamic role assignment. - Integrated settings `AllowRegister` and `DefaultRole` for registration flow. - Updated imports to include new modules: `conf`, `setting`. - Adjusted user creation logic to use `DefaultRole` setting dynamically. * feat(register-and-statistics): add user registration endpoint (#register-and-statistics) - Added `POST /auth/register` endpoint to support user registration. - Implemented registration logic in `auth.go` with dynamic role assignment. - Integrated `AllowRegister` and `DefaultRole` settings for registration flow. - Updated imports to include new modules: `conf`, `setting`. - Adjusted user creation logic to use `DefaultRole` dynamically. * feat(register-and-statistics): enhance role management logic (#register-and-statistics) - Refactored CreateRole and UpdateRole functions to handle default role. - Added dynamic role assignment logic in 'role.go' using conf settings. - Improved request handling in 'handles/role.go' with structured data. - Implemented default role logic in 'db/role.go' to update non-default roles. - Modified 'model/role.go' to include a 'Default' field for role management. * feat(register-and-statistics): enhance role management logic - Refactor CreateRole and UpdateRole to handle default roles. - Add dynamic role assignment using conf settings in 'role.go'. - Improve request handling with structured data in 'handles/role.go'. - Implement default role logic in 'db/role.go' for non-default roles. - Modify 'model/role.go' to include 'Default' field for role management. * feat(register-and-statistics): improve role handling logic - Switch from role names to role IDs for better consistency. - Update logic to prioritize "guest" for default role ID. - Adjust `DefaultRole` setting to use role IDs. - Refactor `getRoleOptions` to return role IDs as a comma-separated string. * feat(register-and-statistics): improve role handling logic
136 lines
3.0 KiB
Go
136 lines
3.0 KiB
Go
package handles
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/alist-org/alist/v3/internal/op"
|
|
"github.com/alist-org/alist/v3/internal/sign"
|
|
"github.com/alist-org/alist/v3/pkg/utils/random"
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
"github.com/alist-org/alist/v3/server/static"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func getRoleOptions() string {
|
|
roles, _, err := op.GetRoles(1, model.MaxInt)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
names := make([]string, len(roles))
|
|
for i, r := range roles {
|
|
names[i] = r.Name
|
|
}
|
|
return strings.Join(names, ",")
|
|
}
|
|
|
|
func ResetToken(c *gin.Context) {
|
|
token := random.Token()
|
|
item := model.SettingItem{Key: "token", Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}
|
|
if err := op.SaveSettingItem(&item); err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
return
|
|
}
|
|
sign.Instance()
|
|
common.SuccessResp(c, token)
|
|
}
|
|
|
|
func GetSetting(c *gin.Context) {
|
|
key := c.Query("key")
|
|
keys := c.Query("keys")
|
|
if key != "" {
|
|
item, err := op.GetSettingItemByKey(key)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
if item.Key == conf.DefaultRole {
|
|
copy := *item
|
|
copy.Options = getRoleOptions()
|
|
common.SuccessResp(c, copy)
|
|
return
|
|
}
|
|
common.SuccessResp(c, item)
|
|
} else {
|
|
items, err := op.GetSettingItemInKeys(strings.Split(keys, ","))
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
for i := range items {
|
|
if items[i].Key == conf.DefaultRole {
|
|
items[i].Options = getRoleOptions()
|
|
break
|
|
}
|
|
}
|
|
common.SuccessResp(c, items)
|
|
}
|
|
}
|
|
|
|
func SaveSettings(c *gin.Context) {
|
|
var req []model.SettingItem
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
if err := op.SaveSettingItems(req); err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
} else {
|
|
common.SuccessResp(c)
|
|
static.UpdateIndex()
|
|
}
|
|
}
|
|
|
|
func ListSettings(c *gin.Context) {
|
|
groupStr := c.Query("group")
|
|
groupsStr := c.Query("groups")
|
|
var settings []model.SettingItem
|
|
var err error
|
|
if groupsStr == "" && groupStr == "" {
|
|
settings, err = op.GetSettingItems()
|
|
} else {
|
|
var groupStrings []string
|
|
if groupsStr != "" {
|
|
groupStrings = strings.Split(groupsStr, ",")
|
|
} else {
|
|
groupStrings = append(groupStrings, groupStr)
|
|
}
|
|
var groups []int
|
|
for _, str := range groupStrings {
|
|
group, err := strconv.Atoi(str)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
groups = append(groups, group)
|
|
}
|
|
settings, err = op.GetSettingItemsInGroups(groups)
|
|
}
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
for i := range settings {
|
|
if settings[i].Key == conf.DefaultRole {
|
|
settings[i].Options = getRoleOptions()
|
|
break
|
|
}
|
|
}
|
|
common.SuccessResp(c, settings)
|
|
}
|
|
|
|
func DeleteSetting(c *gin.Context) {
|
|
key := c.Query("key")
|
|
if err := op.DeleteSettingItemByKey(key); err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
return
|
|
}
|
|
common.SuccessResp(c)
|
|
}
|
|
|
|
func PublicSettings(c *gin.Context) {
|
|
common.SuccessResp(c, op.GetPublicSettingsMap())
|
|
}
|