Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 9m20s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 55m7s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/i18n"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func manageUserQuota(c *gin.Context, req ManageRequest) {
|
|
action := "generic"
|
|
params := model.AuditFields{
|
|
"target_user_id": req.Id,
|
|
"mode": req.Mode,
|
|
"requested_quota": req.Value,
|
|
}
|
|
switch req.Mode {
|
|
case "add":
|
|
action = "user.quota_add"
|
|
case "subtract":
|
|
action = "user.quota_subtract"
|
|
case "override":
|
|
action = "user.quota_override"
|
|
default:
|
|
params["action"] = "add_quota"
|
|
params["method"] = c.Request.Method
|
|
params["route"] = c.FullPath()
|
|
}
|
|
success := false
|
|
defer func() {
|
|
content := auditContentEN(action, params)
|
|
if !success {
|
|
// Failed requests have no committed balance changes to render.
|
|
content = "Failed user quota adjustment"
|
|
}
|
|
model.RecordOperationAuditLog(c.GetInt("id"), c.GetInt("role"), content, c.ClientIP(), action, params,
|
|
auditOperatorInfo(c), &model.AuditRequestInfo{
|
|
Method: c.Request.Method, Route: c.FullPath(), Status: c.Writer.Status(), Success: success,
|
|
}, c)
|
|
markAuditLogged(c)
|
|
}()
|
|
|
|
adjustment, err := model.AdjustUserQuota(req.Id, c.GetInt("role"), req.Mode, req.Value)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, model.ErrInvalidUserQuotaAdjustment):
|
|
params["failure_reason"] = "invalid_parameters"
|
|
if (req.Mode == "add" || req.Mode == "subtract") && req.Value <= 0 {
|
|
common.ApiErrorI18n(c, i18n.MsgUserQuotaChangeZero)
|
|
} else {
|
|
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
|
|
}
|
|
case errors.Is(err, model.ErrUserQuotaPermission):
|
|
params["failure_reason"] = "permission_denied"
|
|
common.ApiErrorI18n(c, i18n.MsgUserNoPermissionHigherLevel)
|
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
|
params["failure_reason"] = "target_not_found"
|
|
common.ApiErrorI18n(c, i18n.MsgUserNotExists)
|
|
case errors.Is(err, model.ErrWalletQuotaLimitExceeded):
|
|
params["failure_reason"] = "quota_limit_exceeded"
|
|
common.ApiError(c, err)
|
|
default:
|
|
params["failure_reason"] = "database_error"
|
|
common.ApiError(c, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
params["target_username"] = adjustment.Username
|
|
params["from"] = adjustment.Before
|
|
params["to"] = adjustment.After
|
|
if req.Mode != "override" {
|
|
params["quota"] = req.Value
|
|
}
|
|
success = true
|
|
operation := model.AuditOperation{Action: action, Params: params}
|
|
model.RecordLogWithAdminInfo(adjustment.UserID, model.LogTypeTopup,
|
|
auditContentEN(action, params), auditOperatorInfo(c), &operation, c)
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": ""})
|
|
}
|