sys_authority.go 7.2 KB

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