sys_authority.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package system
  2. import (
  3. "server/global"
  4. "server/model/common/request"
  5. "server/model/common/response"
  6. "server/model/system"
  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 system.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. // CopyAuthority
  47. // @Tags Authority
  48. // @Summary 拷贝角色
  49. // @Security ApiKeyAuth
  50. // @accept application/json
  51. // @Produce application/json
  52. // @Param data body response.SysAuthorityCopyResponse true "旧角色id, 新权限id, 新权限名, 新父角色id"
  53. // @Success 200 {object} response.Response{data=systemRes.SysAuthorityResponse,msg=string} "拷贝角色,返回包括系统角色详情"
  54. // @Router /authority/copyAuthority [post]
  55. func (a *AuthorityApi) CopyAuthority(c *gin.Context) {
  56. var copyInfo systemRes.SysAuthorityCopyResponse
  57. err := c.ShouldBindJSON(&copyInfo)
  58. if err != nil {
  59. response.FailWithMessage(err.Error(), c)
  60. return
  61. }
  62. err = utils.Verify(copyInfo, utils.OldAuthorityVerify)
  63. if err != nil {
  64. response.FailWithMessage(err.Error(), c)
  65. return
  66. }
  67. err = utils.Verify(copyInfo.Authority, utils.AuthorityVerify)
  68. if err != nil {
  69. response.FailWithMessage(err.Error(), c)
  70. return
  71. }
  72. authBack, err := authorityService.CopyAuthority(copyInfo)
  73. if err != nil {
  74. global.GVA_LOG.Error("拷贝失败!", zap.Error(err))
  75. response.FailWithMessage("拷贝失败"+err.Error(), c)
  76. return
  77. }
  78. response.OkWithDetailed(systemRes.SysAuthorityResponse{Authority: authBack}, "拷贝成功", c)
  79. }
  80. // DeleteAuthority
  81. // @Tags Authority
  82. // @Summary 删除角色
  83. // @Security ApiKeyAuth
  84. // @accept application/json
  85. // @Produce application/json
  86. // @Param data body system.SysAuthority true "删除角色"
  87. // @Success 200 {object} response.Response{msg=string} "删除角色"
  88. // @Router /authority/deleteAuthority [post]
  89. func (a *AuthorityApi) DeleteAuthority(c *gin.Context) {
  90. var authority system.SysAuthority
  91. var err error
  92. if err = c.ShouldBindJSON(&authority); err != nil {
  93. response.FailWithMessage(err.Error(), c)
  94. return
  95. }
  96. if err = utils.Verify(authority, utils.AuthorityIdVerify); err != nil {
  97. response.FailWithMessage(err.Error(), c)
  98. return
  99. }
  100. // 删除角色之前需要判断是否有用户正在使用此角色
  101. if err = authorityService.DeleteAuthority(&authority); err != nil {
  102. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  103. response.FailWithMessage("删除失败"+err.Error(), c)
  104. return
  105. }
  106. _ = casbinService.FreshCasbin()
  107. response.OkWithMessage("删除成功", c)
  108. }
  109. // UpdateAuthority
  110. // @Tags Authority
  111. // @Summary 更新角色信息
  112. // @Security ApiKeyAuth
  113. // @accept application/json
  114. // @Produce application/json
  115. // @Param data body system.SysAuthority true "权限id, 权限名, 父角色id"
  116. // @Success 200 {object} response.Response{data=systemRes.SysAuthorityResponse,msg=string} "更新角色信息,返回包括系统角色详情"
  117. // @Router /authority/updateAuthority [post]
  118. func (a *AuthorityApi) UpdateAuthority(c *gin.Context) {
  119. var auth system.SysAuthority
  120. err := c.ShouldBindJSON(&auth)
  121. if err != nil {
  122. response.FailWithMessage(err.Error(), c)
  123. return
  124. }
  125. err = utils.Verify(auth, utils.AuthorityVerify)
  126. if err != nil {
  127. response.FailWithMessage(err.Error(), c)
  128. return
  129. }
  130. authority, err := authorityService.UpdateAuthority(auth)
  131. if err != nil {
  132. global.GVA_LOG.Error("更新失败!", zap.Error(err))
  133. response.FailWithMessage("更新失败"+err.Error(), c)
  134. return
  135. }
  136. response.OkWithDetailed(systemRes.SysAuthorityResponse{Authority: authority}, "更新成功", c)
  137. }
  138. // GetAuthorityList
  139. // @Tags Authority
  140. // @Summary 分页获取角色列表
  141. // @Security ApiKeyAuth
  142. // @accept application/json
  143. // @Produce application/json
  144. // @Param data body request.PageInfo true "页码, 每页大小"
  145. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页获取角色列表,返回包括列表,总数,页码,每页数量"
  146. // @Router /authority/getAuthorityList [post]
  147. func (a *AuthorityApi) GetAuthorityList(c *gin.Context) {
  148. var pageInfo request.PageInfo
  149. err := c.ShouldBindJSON(&pageInfo)
  150. if err != nil {
  151. response.FailWithMessage(err.Error(), c)
  152. return
  153. }
  154. err = utils.Verify(pageInfo, utils.PageInfoVerify)
  155. if err != nil {
  156. response.FailWithMessage(err.Error(), c)
  157. return
  158. }
  159. list, total, err := authorityService.GetAuthorityInfoList(pageInfo)
  160. if err != nil {
  161. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  162. response.FailWithMessage("获取失败"+err.Error(), c)
  163. return
  164. }
  165. response.OkWithDetailed(response.PageResult{
  166. List: list,
  167. Total: total,
  168. Page: pageInfo.Page,
  169. PageSize: pageInfo.PageSize,
  170. }, "获取成功", c)
  171. }
  172. // SetDataAuthority
  173. // @Tags Authority
  174. // @Summary 设置角色资源权限
  175. // @Security ApiKeyAuth
  176. // @accept application/json
  177. // @Produce application/json
  178. // @Param data body system.SysAuthority true "设置角色资源权限"
  179. // @Success 200 {object} response.Response{msg=string} "设置角色资源权限"
  180. // @Router /authority/setDataAuthority [post]
  181. func (a *AuthorityApi) SetDataAuthority(c *gin.Context) {
  182. var auth system.SysAuthority
  183. err := c.ShouldBindJSON(&auth)
  184. if err != nil {
  185. response.FailWithMessage(err.Error(), c)
  186. return
  187. }
  188. err = utils.Verify(auth, utils.AuthorityIdVerify)
  189. if err != nil {
  190. response.FailWithMessage(err.Error(), c)
  191. return
  192. }
  193. err = authorityService.SetDataAuthority(auth)
  194. if err != nil {
  195. global.GVA_LOG.Error("设置失败!", zap.Error(err))
  196. response.FailWithMessage("设置失败"+err.Error(), c)
  197. return
  198. }
  199. response.OkWithMessage("设置成功", c)
  200. }