sys_authority.go 7.0 KB

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