mirror of https://github.com/Xhofe/alist
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package handles
|
|
|
|
import (
|
|
"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"
|
|
"strconv"
|
|
)
|
|
|
|
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 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)
|
|
permissions, total, err := op.GetRoles(req.Page, req.PerPage)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500, true)
|
|
return
|
|
}
|
|
common.SuccessResp(c, common.PageResp{
|
|
Content: permissions,
|
|
Total: total,
|
|
})
|
|
}
|
|
|
|
func UpdateRole(c *gin.Context) {
|
|
var req model.Role
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
if err := op.UpdateRole(&req); err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
} 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
|
|
}
|
|
if err := op.DeleteRoleById(uint(id)); err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
return
|
|
}
|
|
common.SuccessResp(c)
|
|
}
|
|
|
|
type GetPermissionByRoleIdsReq struct {
|
|
Ids []uint `json:"ids"`
|
|
}
|
|
|
|
func GetPermissionByRoleIds(c *gin.Context) {
|
|
var req GetPermissionByRoleIdsReq
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
common.ErrorResp(c, err, 400)
|
|
return
|
|
}
|
|
permissions, err := op.GetPermissionByRoleIds(req.Ids)
|
|
if err != nil {
|
|
common.ErrorResp(c, err, 500)
|
|
return
|
|
}
|
|
common.SuccessResp(c, permissions)
|
|
}
|