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.

165 lines
6.1 KiB

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