170 lines
4.5 KiB
Go
170 lines
4.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"kra/internal/server/httpx"
|
|
"kra/internal/service"
|
|
"kra/internal/service/dto"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Authority struct{ service *service.AuthorityService }
|
|
|
|
func NewAuthority(service *service.AuthorityService) *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, "获取失败"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
|
|
}
|
|
func (h *Authority) Create(c *gin.Context) {
|
|
var req dto.AuthorityRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.AuthorityID == 0 {
|
|
httpx.Fail(c, "AuthorityId值不能为空")
|
|
return
|
|
}
|
|
if req.AuthorityName == "" {
|
|
httpx.Fail(c, "AuthorityName值不能为空")
|
|
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 err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.OldAuthorityID == 0 {
|
|
httpx.Fail(c, "OldAuthorityId值不能为空")
|
|
return
|
|
}
|
|
if req.Authority.AuthorityID == 0 {
|
|
httpx.Fail(c, "AuthorityId值不能为空")
|
|
return
|
|
}
|
|
if req.Authority.AuthorityName == "" {
|
|
httpx.Fail(c, "AuthorityName值不能为空")
|
|
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 err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.AuthorityID == 0 {
|
|
httpx.Fail(c, "AuthorityId值不能为空")
|
|
return
|
|
}
|
|
if req.AuthorityName == "" {
|
|
httpx.Fail(c, "AuthorityName值不能为空")
|
|
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 err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.AuthorityID == 0 {
|
|
httpx.Fail(c, "AuthorityId值不能为空")
|
|
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 err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.AuthorityID == 0 {
|
|
httpx.Fail(c, "角色ID不能为空")
|
|
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) {
|
|
var req dto.SetRoleUsersRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
ids, err := h.service.AuthorityUserIDs(c.Request.Context(), req.AuthorityID)
|
|
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 err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.AuthorityID == 0 {
|
|
httpx.Fail(c, "角色ID不能为空")
|
|
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) {
|
|
var req dto.SetDataScopeRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
ids, err := h.service.DataScopeDepartmentIDs(c.Request.Context(), req.AuthorityID)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败"+err.Error())
|
|
return
|
|
}
|
|
if ids == nil {
|
|
ids = []uint{}
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
|
|
}
|