87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func groupRatioTarget(c *gin.Context) (int, bool) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil || id <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "Invalid user"})
|
|
return 0, false
|
|
}
|
|
user, err := model.GetUserById(id, false)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return 0, false
|
|
}
|
|
if c.GetInt("role") < common.RoleAdminUser || !canManageTargetRole(c.GetInt("role"), user.Role) {
|
|
c.JSON(http.StatusForbidden, gin.H{"success": false, "message": "Permission denied"})
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
func GetUserGroupRatios(c *gin.Context) {
|
|
id, ok := groupRatioTarget(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
ratios, err := model.GetPersonalGroupRatios(c.Request.Context(), id)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": ratios})
|
|
}
|
|
|
|
func UpdateUserGroupRatios(c *gin.Context) {
|
|
id, ok := groupRatioTarget(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var input struct {
|
|
Ratios map[string]float64 `json:"ratios"`
|
|
}
|
|
if err := c.ShouldBindJSON(&input); err != nil || input.Ratios == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "Invalid ratios"})
|
|
return
|
|
}
|
|
if err := model.SavePersonalGroupRatios(c.Request.Context(), id, input.Ratios); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
recordManageAuditFor(c, id, "user.group_ratios.update", map[string]any{"ratios": input.Ratios})
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
func GetGroupMetadata(c *gin.Context) {
|
|
groups, err := model.GetGroupMetadata(c.Request.Context())
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": groups})
|
|
}
|
|
|
|
func UpdateGroupMetadata(c *gin.Context) {
|
|
var input struct {
|
|
Groups map[string]model.GroupMetadataValue `json:"groups"`
|
|
}
|
|
if err := c.ShouldBindJSON(&input); err != nil || input.Groups == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "Invalid group metadata"})
|
|
return
|
|
}
|
|
if err := model.SaveGroupMetadata(c.Request.Context(), input.Groups); err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
recordManageAuditFor(c, 0, "group.metadata.update", map[string]any{"groups": input.Groups})
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|