You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.5 KiB

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