124 lines
3.5 KiB
Go
124 lines
3.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/server/httpx"
|
|
"kra/internal/service"
|
|
"kra/internal/service/dto"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Authority struct{ service *service.AccessService }
|
|
|
|
func NewAuthority(service *service.AccessService) *Authority { return &Authority{service: service} }
|
|
|
|
func (h *Authority) List(c *gin.Context) {
|
|
items, err := h.service.Authorities(c.Request.Context())
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
|
|
}
|
|
func (h *Authority) Create(c *gin.Context) {
|
|
var req dto.AuthorityRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.AuthorityID == 0 {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
value, err := h.service.CreateAuthorityRequest(c.Request.Context(), &req)
|
|
if err != nil {
|
|
httpx.Fail(c, "创建失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{"authority": value}, "创建成功")
|
|
}
|
|
func (h *Authority) Copy(c *gin.Context) {
|
|
var req dto.CopyAuthorityRequest
|
|
if c.ShouldBindJSON(&req) != nil || req.Authority.AuthorityID == 0 || req.OldAuthorityID == 0 {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
value, err := h.service.CopyAuthorityRequest(c.Request.Context(), &req)
|
|
if err != nil {
|
|
httpx.Fail(c, "拷贝失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{"authority": value}, "拷贝成功")
|
|
}
|
|
func (h *Authority) Update(c *gin.Context) {
|
|
var req dto.AuthorityRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
value, err := h.service.UpdateAuthorityRequest(c.Request.Context(), &req)
|
|
if err != nil {
|
|
httpx.Fail(c, "更新失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{"authority": value}, "更新成功")
|
|
}
|
|
func (h *Authority) Delete(c *gin.Context) {
|
|
var req dto.DeleteAuthorityRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.DeleteAuthority(c.Request.Context(), req.AuthorityID); err != nil {
|
|
httpx.Fail(c, "删除失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
|
|
}
|
|
func (h *Authority) SetUsers(c *gin.Context) {
|
|
var req dto.SetRoleUsersRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.SetAuthorityUsers(c.Request.Context(), req.AuthorityID, req.UserIDs); err != nil {
|
|
httpx.Fail(c, "设置失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|
|
func (h *Authority) Users(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
|
|
ids, err := h.service.AuthorityUserIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败"+err.Error())
|
|
return
|
|
}
|
|
if ids == nil {
|
|
ids = []uint{}
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
|
|
}
|
|
func (h *Authority) SetDataScope(c *gin.Context) {
|
|
var req dto.SetDataScopeRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.SetDataScope(c.Request.Context(), req.AuthorityID, req.DataScope, req.DepartmentIDs); err != nil {
|
|
httpx.Fail(c, "设置失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|
|
func (h *Authority) DataScopeDepartments(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("authorityId"), 10, 64)
|
|
ids, err := h.service.DataScopeDepartmentIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败"+err.Error())
|
|
return
|
|
}
|
|
if ids == nil {
|
|
ids = []uint{}
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
|
|
}
|