sys_authority.go 5.8 KB

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