sys_authority.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  8. systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
  9. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  10. "github.com/gin-gonic/gin"
  11. "go.uber.org/zap"
  12. )
  13. type AuthorityApi struct {
  14. }
  15. // @Tags Authority
  16. // @Summary 创建角色
  17. // @Security ApiKeyAuth
  18. // @accept application/json
  19. // @Produce application/json
  20. // @Param data body system.SysAuthority true "权限id, 权限名, 父角色id"
  21. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  22. // @Router /authority/createAuthority [post]
  23. func (a *AuthorityApi) CreateAuthority(c *gin.Context) {
  24. var authority system.SysAuthority
  25. _ = c.ShouldBindJSON(&authority)
  26. if err := utils.Verify(authority, utils.AuthorityVerify); err != nil {
  27. response.FailWithMessage(err.Error(), c)
  28. return
  29. }
  30. if err, authBack := authorityService.CreateAuthority(authority); err != nil {
  31. global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
  32. response.FailWithMessage("创建失败"+err.Error(), c)
  33. } else {
  34. _ = menuService.AddMenuAuthority(systemReq.DefaultMenu(), authority.AuthorityId)
  35. _ = casbinService.UpdateCasbin(authority.AuthorityId, systemReq.DefaultCasbin())
  36. response.OkWithDetailed(systemRes.SysAuthorityResponse{Authority: authBack}, "创建成功", c)
  37. }
  38. }
  39. // @Tags Authority
  40. // @Summary 拷贝角色
  41. // @Security ApiKeyAuth
  42. // @accept application/json
  43. // @Produce application/json
  44. // @Param data body response.SysAuthorityCopyResponse true "旧角色id, 新权限id, 新权限名, 新父角色id"
  45. // @Success 200 {string} string "{"success":true,"data":{},"msg":"拷贝成功"}"
  46. // @Router /authority/copyAuthority [post]
  47. func (a *AuthorityApi) CopyAuthority(c *gin.Context) {
  48. var copyInfo systemRes.SysAuthorityCopyResponse
  49. _ = c.ShouldBindJSON(&copyInfo)
  50. if err := utils.Verify(copyInfo, utils.OldAuthorityVerify); err != nil {
  51. response.FailWithMessage(err.Error(), c)
  52. return
  53. }
  54. if err := utils.Verify(copyInfo.Authority, utils.AuthorityVerify); err != nil {
  55. response.FailWithMessage(err.Error(), c)
  56. return
  57. }
  58. if err, authBack := authorityService.CopyAuthority(copyInfo); err != nil {
  59. global.GVA_LOG.Error("拷贝失败!", zap.Any("err", err))
  60. response.FailWithMessage("拷贝失败"+err.Error(), c)
  61. } else {
  62. response.OkWithDetailed(systemRes.SysAuthorityResponse{Authority: authBack}, "拷贝成功", c)
  63. }
  64. }
  65. // @Tags Authority
  66. // @Summary 删除角色
  67. // @Security ApiKeyAuth
  68. // @accept application/json
  69. // @Produce application/json
  70. // @Param data body system.SysAuthority true "删除角色"
  71. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  72. // @Router /authority/deleteAuthority [post]
  73. func (a *AuthorityApi) DeleteAuthority(c *gin.Context) {
  74. var authority system.SysAuthority
  75. _ = c.ShouldBindJSON(&authority)
  76. if err := utils.Verify(authority, utils.AuthorityIdVerify); err != nil {
  77. response.FailWithMessage(err.Error(), c)
  78. return
  79. }
  80. if err := authorityService.DeleteAuthority(&authority); err != nil { // 删除角色之前需要判断是否有用户正在使用此角色
  81. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  82. response.FailWithMessage("删除失败"+err.Error(), c)
  83. } else {
  84. response.OkWithMessage("删除成功", c)
  85. }
  86. }
  87. // @Tags Authority
  88. // @Summary 更新角色信息
  89. // @Security ApiKeyAuth
  90. // @accept application/json
  91. // @Produce application/json
  92. // @Param data body system.SysAuthority true "权限id, 权限名, 父角色id"
  93. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  94. // @Router /authority/updateAuthority [post]
  95. func (a *AuthorityApi) UpdateAuthority(c *gin.Context) {
  96. var auth system.SysAuthority
  97. _ = c.ShouldBindJSON(&auth)
  98. if err := utils.Verify(auth, utils.AuthorityVerify); err != nil {
  99. response.FailWithMessage(err.Error(), c)
  100. return
  101. }
  102. if err, authority := authorityService.UpdateAuthority(auth); err != nil {
  103. global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
  104. response.FailWithMessage("更新失败"+err.Error(), c)
  105. } else {
  106. response.OkWithDetailed(systemRes.SysAuthorityResponse{Authority: authority}, "更新成功", c)
  107. }
  108. }
  109. // @Tags Authority
  110. // @Summary 分页获取角色列表
  111. // @Security ApiKeyAuth
  112. // @accept application/json
  113. // @Produce application/json
  114. // @Param data body request.PageInfo true "页码, 每页大小"
  115. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  116. // @Router /authority/getAuthorityList [post]
  117. func (a *AuthorityApi) GetAuthorityList(c *gin.Context) {
  118. var pageInfo request.PageInfo
  119. _ = c.ShouldBindJSON(&pageInfo)
  120. if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
  121. response.FailWithMessage(err.Error(), c)
  122. return
  123. }
  124. if err, list, total := authorityService.GetAuthorityInfoList(pageInfo); err != nil {
  125. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  126. response.FailWithMessage("获取失败"+err.Error(), c)
  127. } else {
  128. response.OkWithDetailed(response.PageResult{
  129. List: list,
  130. Total: total,
  131. Page: pageInfo.Page,
  132. PageSize: pageInfo.PageSize,
  133. }, "获取成功", c)
  134. }
  135. }
  136. // @Tags Authority
  137. // @Summary 设置角色资源权限
  138. // @Security ApiKeyAuth
  139. // @accept application/json
  140. // @Produce application/json
  141. // @Param data body system.SysAuthority true "设置角色资源权限"
  142. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  143. // @Router /authority/setDataAuthority [post]
  144. func (a *AuthorityApi) SetDataAuthority(c *gin.Context) {
  145. var auth system.SysAuthority
  146. _ = c.ShouldBindJSON(&auth)
  147. if err := utils.Verify(auth, utils.AuthorityIdVerify); err != nil {
  148. response.FailWithMessage(err.Error(), c)
  149. return
  150. }
  151. if err := authorityService.SetDataAuthority(auth); err != nil {
  152. global.GVA_LOG.Error("设置失败!", zap.Any("err", err))
  153. response.FailWithMessage("设置失败"+err.Error(), c)
  154. } else {
  155. response.OkWithMessage("设置成功", c)
  156. }
  157. }