You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/api/settings/update.go

60 lines
1.3 KiB
Go

//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//
package settings
import (
"github.com/veypi/vbase/auth"
"github.com/veypi/vbase/models"
"github.com/veypi/vigo"
)
// UpdateItem 更新项
type UpdateItem struct {
Key string `json:"key"`
Value string `json:"value"`
}
// UpdateRequest 更新请求
type UpdateRequest struct {
Settings []UpdateItem `json:"settings"`
}
// UpdateResponse 更新响应
type UpdateResponse struct {
Updated int `json:"updated"`
}
// update 批量更新设置(仅管理员可用)
func update(x *vigo.X, req *UpdateRequest) (*UpdateResponse, error) {
// 获取当前用户ID
userID := ""
if u := x.Get("user_id"); u != nil {
userID = u.(string)
}
// 检查用户是否为管理员(检查 setting:update 权限)
isAdmin, err := auth.VBaseAuth.CheckPermission(x.Context(), userID, "", "setting:update", "")
if err != nil {
return nil, vigo.ErrInternalServer.WithError(err)
}
if !isAdmin {
return nil, vigo.ErrForbidden.WithString("only admin can update settings")
}
updated := 0
for _, item := range req.Settings {
if err := models.SetSetting(item.Key, item.Value, userID); err != nil {
return nil, vigo.ErrInternalServer.WithString("failed to update " + item.Key)
}
updated++
}
return &UpdateResponse{
Updated: updated,
}, nil
}