137 lines
3.2 KiB
Go
137 lines
3.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"filestoragesystem/internal/utils"
|
|
)
|
|
|
|
// RoleController 角色权限控制器
|
|
type RoleController struct {
|
|
*Base
|
|
}
|
|
|
|
// NewRoleController 创建角色控制器
|
|
func NewRoleController(b *Base) *RoleController { return &RoleController{Base: b} }
|
|
|
|
// ListRoles 角色列表
|
|
func (ctl *RoleController) ListRoles(c *gin.Context) {
|
|
roles, err := ctl.svc.Role.ListRoles()
|
|
if err != nil {
|
|
utils.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
utils.OK(c, roles)
|
|
}
|
|
|
|
// GetRole 角色详情
|
|
func (ctl *RoleController) GetRole(c *gin.Context) {
|
|
id, ok := paramID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
role, err := ctl.svc.Role.GetRole(id)
|
|
if err != nil {
|
|
utils.NotFound(c, "角色不存在")
|
|
return
|
|
}
|
|
utils.OK(c, role)
|
|
}
|
|
|
|
type createRoleReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Code string `json:"code" binding:"required"`
|
|
Description string `json:"description"`
|
|
Permissions []uint `json:"permissions"`
|
|
}
|
|
|
|
// CreateRole 创建角色
|
|
func (ctl *RoleController) CreateRole(c *gin.Context) {
|
|
var req createRoleReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
role, err := ctl.svc.Role.CreateRole(req.Name, req.Code, req.Description, req.Permissions)
|
|
if err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "create", "role", role.ID, role.Name, true, "")
|
|
utils.OK(c, role)
|
|
}
|
|
|
|
type updateRoleReq struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Permissions []uint `json:"permissions"`
|
|
}
|
|
|
|
// UpdateRole 编辑角色
|
|
func (ctl *RoleController) UpdateRole(c *gin.Context) {
|
|
id, ok := paramID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req updateRoleReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
role, err := ctl.svc.Role.UpdateRole(id, req.Name, req.Description, req.Permissions)
|
|
if err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "role", id, role.Name, true, "")
|
|
utils.OK(c, role)
|
|
}
|
|
|
|
// DeleteRole 删除角色
|
|
func (ctl *RoleController) DeleteRole(c *gin.Context) {
|
|
id, ok := paramID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := ctl.svc.Role.DeleteRole(id); err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "delete", "role", id, "", true, "")
|
|
utils.OKMsg(c, "角色已删除")
|
|
}
|
|
|
|
type setPermissionsReq struct {
|
|
Permissions []uint `json:"permissions"`
|
|
}
|
|
|
|
// SetPermissions 配置角色权限
|
|
func (ctl *RoleController) SetPermissions(c *gin.Context) {
|
|
id, ok := paramID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req setPermissionsReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
if err := ctl.svc.Role.SetPermissions(id, req.Permissions); err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "role", id, "permissions", true, "")
|
|
utils.OKMsg(c, "角色权限已更新")
|
|
}
|
|
|
|
// ListPermissions 权限列表(按模块分组)
|
|
func (ctl *RoleController) ListPermissions(c *gin.Context) {
|
|
groups, order, err := ctl.svc.Role.PermissionsGrouped()
|
|
if err != nil {
|
|
utils.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
result := make([]gin.H, 0, len(order))
|
|
for _, module := range order {
|
|
result = append(result, gin.H{"module": module, "permissions": groups[module]})
|
|
}
|
|
utils.OK(c, result)
|
|
}
|