mirror of https://github.com/Xhofe/alist
102 lines
2.1 KiB
Go
102 lines
2.1 KiB
Go
package handles
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/alist-org/alist/v3/internal/op"
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func ListRoles(c *gin.Context) {
|
|
var req model.PageReq
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
req.Validate()
|
|
log.Debugf("%+v", req)
|
|
roles, total, err := op.GetRoles(req.Page, req.PerPage)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
common.SuccessResp(c, common.PageResp{Content: roles, Total: total})
|
|
}
|
|
|
|
func GetRole(c *gin.Context) {
|
|
idStr := c.Query("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
role, err := op.GetRole(uint(id))
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
common.SuccessResp(c, role)
|
|
}
|
|
|
|
func CreateRole(c *gin.Context) {
|
|
var req model.Role
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
if err := op.CreateRole(&req); err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
} else {
|
|
common.SuccessResp(c)
|
|
}
|
|
}
|
|
|
|
func UpdateRole(c *gin.Context) {
|
|
var req model.Role
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
role, err := op.GetRole(req.ID)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
if role.Name == "admin" || role.Name == "guest" {
|
|
common.ErrorResp(c, errs.ErrChangeDefaultRole, 403)
|
|
return
|
|
}
|
|
if err := op.UpdateRole(&req); err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
} else {
|
|
common.SuccessResp(c)
|
|
}
|
|
}
|
|
|
|
func DeleteRole(c *gin.Context) {
|
|
idStr := c.Query("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
role, err := op.GetRole(uint(id))
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
if role.Name == "admin" || role.Name == "guest" {
|
|
common.ErrorResp(c, errs.ErrChangeDefaultRole, 403)
|
|
return
|
|
}
|
|
if err := op.DeleteRole(uint(id)); err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
common.SuccessResp(c)
|
|
}
|