sys_authority.go 5.6 KB

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