rbac.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "cp-organization-management-gateway/errors"
  6. param_v1 "cp-organization-management-gateway/param/v1"
  7. "cp-organization-management-gateway/pb"
  8. "cp-organization-management-gateway/pb/v1"
  9. "cp-organization-management-gateway/utils"
  10. "cp-organization-management-gateway/consts"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "github.com/jaryhe/gopkgs/tasker/httptasker"
  13. "github.com/jaryhe/gopkgs/util"
  14. "net/http"
  15. "github.com/gin-gonic/gin"
  16. "go.uber.org/zap"
  17. )
  18. func handleRbacList(list []*v1.RbacNodeItem) ([]*v1.RbacNodeItem) {
  19. if len(list) == 0 {
  20. list = make([]*v1.RbacNodeItem, 0)
  21. return list
  22. }
  23. for i, v := range list {
  24. list[i].Childs = handleRbacList(v.Childs)
  25. }
  26. return list
  27. }
  28. //
  29. // @Summary 权限节点列表
  30. // @Description 权限节点列表
  31. // @Tags 系统管理-角色管理
  32. // @Accept json
  33. // @Produce json
  34. // @Param token header string true "token"
  35. // @Param group_id query int64 false "不填返回所有,填了返回该角色下的权限"
  36. // @Param only_select query bool false "填了groupid后,true:仅返回角色下的节点, false:返回所有节点,对属于该角色的节点作标记"
  37. // @Success 200 {object} v1.RbacNodeListResponse
  38. // @Failure 500 {object} base.HTTPError
  39. // @Router /api/v1/system/rbac/node/list [get]
  40. func (c *Controller) RbacNodeList(ctx *gin.Context) {
  41. // 解析参数
  42. req := &param_v1.RbacNodeListRequest{}
  43. parseParamTask := func() error {
  44. err := util.ShouldBind(ctx, &req.Header, nil, &req.RbacNodeListQuery, nil)
  45. if err != nil {
  46. logger.Error("func",
  47. zap.String("call", "util.ShouldBind"),
  48. zap.String("error", err.Error()))
  49. return errors.ParamsError
  50. }
  51. return nil
  52. }
  53. // 业务处理
  54. handleServiceTask := func() error {
  55. tokenInfo, err := utils.GetTokeInfo(ctx)
  56. if err != nil {
  57. return err
  58. }
  59. if req.GroupId > 0 {
  60. resp := param_v1.RbacNodeListByGroupOrUserResponse{}
  61. rpcReq := &v1.RbacNodeListByGroupOrUserRequest{
  62. OrganizationCode:tokenInfo.OrganizationCode,
  63. GroupId:req.GroupId,
  64. Select:req.OnlySelect,
  65. }
  66. rpcRsp, err := pb.Organization.RbacNodeListByGroupOrUser(ctx, rpcReq)
  67. if err != nil {
  68. s, _ := json.MarshalToString(req)
  69. logger.Error("func",
  70. zap.String("call", "pb.Organization.RbacNodeListByGroupOrUser"),
  71. zap.String("params", s),
  72. zap.String("error", err.Error()))
  73. return errors.ErrorTransForm(err)
  74. }
  75. rpcRsp.List = handleRbacList(rpcRsp.List)
  76. resp.Data = *rpcRsp
  77. ctx.JSON(http.StatusOK, resp)
  78. return nil
  79. }
  80. resp := param_v1.RbacNodeListResponse{}
  81. rpcReq := &v1.RbacNodeListRequest{
  82. OrganizationCode:tokenInfo.OrganizationCode,
  83. IsAll:false,
  84. }
  85. rpcRsp, err := pb.Organization.RbacNodeList(ctx, rpcReq)
  86. if err != nil {
  87. s, _ := json.MarshalToString(req)
  88. logger.Error("func",
  89. zap.String("call", "pb.Organization.RbacNodeList"),
  90. zap.String("params", s),
  91. zap.String("error", err.Error()))
  92. return errors.ErrorTransForm(err)
  93. }
  94. rpcRsp.List = handleRbacList(rpcRsp.List)
  95. resp.Data = *rpcRsp
  96. ctx.JSON(http.StatusOK, resp)
  97. return nil
  98. }
  99. // 执行任务
  100. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  101. }
  102. //
  103. // @Summary 角色列表
  104. // @Description 角色列表
  105. // @Tags 系统管理-角色管理
  106. // @Accept json
  107. // @Produce json
  108. // @Param token header string true "token"
  109. // @Success 200 {object} v1.RbacGroupListResponse
  110. // @Failure 500 {object} base.HTTPError
  111. // @Router /api/v1/system/rbac/group/list [get]
  112. func (c *Controller) RbacGroupList(ctx *gin.Context) {
  113. // 解析参数
  114. req := &param_v1.RbacGroupListRequest{}
  115. parseParamTask := func() error {
  116. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  117. if err != nil {
  118. logger.Error("func",
  119. zap.String("call", "util.ShouldBind"),
  120. zap.String("error", err.Error()))
  121. return errors.ParamsError
  122. }
  123. return nil
  124. }
  125. // 业务处理
  126. handleServiceTask := func() error {
  127. // 响应数据
  128. tokenInfo, err := utils.GetTokeInfo(ctx)
  129. if err != nil {
  130. return err
  131. }
  132. resp := param_v1.RbacGroupListResponse{}
  133. rpcReq := &v1.RbacGroupListRequest{
  134. OrganizationCode:tokenInfo.OrganizationCode,
  135. IsSuper:tokenInfo.IsSuper,
  136. }
  137. rpcRsp, err := pb.Organization.RbacGroupList(ctx, rpcReq)
  138. if err != nil {
  139. s, _ := json.MarshalToString(req)
  140. logger.Error("func",
  141. zap.String("call", "pb.Organization.RbacGroupList"),
  142. zap.String("params", s),
  143. zap.String("error", err.Error()))
  144. return errors.ErrorTransForm(err)
  145. }
  146. resp.Data = *rpcRsp
  147. ctx.JSON(http.StatusOK, resp)
  148. return nil
  149. }
  150. // 执行任务
  151. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  152. }
  153. //
  154. // @Summary 添加角色
  155. // @Description 添加角色
  156. // @Tags 系统管理-角色管理
  157. // @Accept json
  158. // @Produce json
  159. // @Param token header string true "token"
  160. // @Param body body v1.RbacGroupAddBody true "token"
  161. // @Success 200 {object} v1.RbacGroupAddResponse
  162. // @Failure 500 {object} base.HTTPError
  163. // @Router /api/v1/system/rbac/group [post]
  164. func (c *Controller) RbacGroupAdd(ctx *gin.Context) {
  165. // 解析参数
  166. req := &param_v1.RbacGroupAddRequest{}
  167. parseParamTask := func() error {
  168. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.RbacGroupAddBody)
  169. if err != nil {
  170. logger.Error("func",
  171. zap.String("call", "util.ShouldBind"),
  172. zap.String("error", err.Error()))
  173. return errors.ParamsError
  174. }
  175. return nil
  176. }
  177. // 业务处理
  178. handleServiceTask := func() error {
  179. // 响应数据
  180. tokenInfo, err := utils.GetTokeInfo(ctx)
  181. if err != nil {
  182. return err
  183. }
  184. resp := param_v1.RbacGroupAddResponse{}
  185. rpcReq := &v1.RbacGroupAddRequest{
  186. OrganizationCode:tokenInfo.OrganizationCode,
  187. NodeList:req.NodeList,
  188. Name:req.Name,
  189. Uid:tokenInfo.Uid,
  190. }
  191. rpcRsp, err := pb.Organization.RbacGroupAdd(ctx, rpcReq)
  192. if err != nil {
  193. s, _ := json.MarshalToString(req)
  194. logger.Error("func",
  195. zap.String("call", "pb.Organization.RbacGroupAdd"),
  196. zap.String("params", s),
  197. zap.String("error", err.Error()))
  198. return errors.ErrorTransForm(err)
  199. }
  200. resp.Data = *rpcRsp
  201. ctx.JSON(http.StatusOK, resp)
  202. logReq := OperationLogRequest{
  203. Module:consts.OperationModuleRbac,
  204. Action:consts.OperationActionGroupAdd,
  205. Origin:nil,
  206. Target:req.RbacGroupAddBody,
  207. UserName:tokenInfo.Username,
  208. Uid:tokenInfo.Uid,
  209. OrganizationCode:tokenInfo.OrganizationCode,
  210. }
  211. OperationLogAdd(&logReq)
  212. return nil
  213. }
  214. // 执行任务
  215. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  216. }
  217. //
  218. // @Summary 修改角色
  219. // @Description 修改角色
  220. // @Tags 系统管理-角色管理
  221. // @Accept json
  222. // @Produce json
  223. // @Param token header string true "token"
  224. // @Param body body v1.RbacGroupUpdateBody true "token"
  225. // @Success 200 {object} v1.RbacGroupUpdateResponse
  226. // @Failure 500 {object} base.HTTPError
  227. // @Router /api/v1/system/rbac/group [put]
  228. func (c *Controller) RbacGroupUpdate(ctx *gin.Context) {
  229. // 解析参数
  230. req := &param_v1.RbacGroupUpdateRequest{}
  231. parseParamTask := func() error {
  232. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.RbacGroupUpdateBody)
  233. if err != nil {
  234. logger.Error("func",
  235. zap.String("call", "util.ShouldBind"),
  236. zap.String("error", err.Error()))
  237. return errors.ParamsError
  238. }
  239. return nil
  240. }
  241. // 业务处理
  242. handleServiceTask := func() error {
  243. // 响应数据
  244. tokenInfo, err := utils.GetTokeInfo(ctx)
  245. if err != nil {
  246. return err
  247. }
  248. resp := param_v1.RbacGroupUpdateResponse{}
  249. rpcReq := &v1.RbacGroupUpdateRequest{
  250. OrganizationCode:tokenInfo.OrganizationCode,
  251. NodeList:req.NodeList,
  252. Name:req.Name,
  253. Id:req.Id,
  254. Uid:tokenInfo.Uid,
  255. }
  256. rpcResp, err := pb.Organization.RbacGroupUpdate(ctx, rpcReq)
  257. if err != nil {
  258. s, _ := json.MarshalToString(req)
  259. logger.Error("func",
  260. zap.String("call", "pb.Organization.RbacGroupUpdate"),
  261. zap.String("params", s),
  262. zap.String("error", err.Error()))
  263. return errors.ErrorTransForm(err)
  264. }
  265. ctx.JSON(http.StatusOK, resp)
  266. logReq := OperationLogRequest{
  267. Module:consts.OperationModuleRbac,
  268. Action:consts.OperationActionGroupUpdate,
  269. Origin:rpcResp.Origin,
  270. Target:req.RbacGroupUpdateBody,
  271. UserName:tokenInfo.Username,
  272. Uid:tokenInfo.Uid,
  273. OrganizationCode:tokenInfo.OrganizationCode,
  274. }
  275. OperationLogAdd(&logReq)
  276. return nil
  277. }
  278. // 执行任务
  279. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  280. }
  281. //
  282. // @Summary 删除角色
  283. // @Description 删除角色
  284. // @Tags 系统管理-角色管理
  285. // @Accept json
  286. // @Produce json
  287. // @Param token header string true "token"
  288. // @Param id path int64 true "token"
  289. // @Success 200 {object} v1.RbacGroupDelResponse
  290. // @Failure 500 {object} base.HTTPError
  291. // @Router /api/v1/system/rbac/group/{id} [delete]
  292. func (c *Controller) RbacGroupDel(ctx *gin.Context) {
  293. // 解析参数
  294. req := &param_v1.RbacGroupDelRequest{}
  295. parseParamTask := func() error {
  296. err := util.ShouldBind(ctx, &req.Header, &req.RbacGroupDelPath, nil, nil)
  297. if err != nil {
  298. logger.Error("func",
  299. zap.String("call", "util.ShouldBind"),
  300. zap.String("error", err.Error()))
  301. return errors.ParamsError
  302. }
  303. return nil
  304. }
  305. // 业务处理
  306. handleServiceTask := func() error {
  307. // 响应数据
  308. tokenInfo, err := utils.GetTokeInfo(ctx)
  309. if err != nil {
  310. return err
  311. }
  312. resp := param_v1.RbacGroupDelResponse{}
  313. rpcReq := &v1.RbacGroupDelRequest{
  314. OrganizationCode:tokenInfo.OrganizationCode,
  315. Id:req.Id,
  316. Uid:tokenInfo.Uid,
  317. }
  318. rpcResp, err := pb.Organization.RbacGroupDel(ctx, rpcReq)
  319. if err != nil {
  320. s, _ := json.MarshalToString(req)
  321. logger.Error("func",
  322. zap.String("call", "pb.Organization.RbacGroupDel"),
  323. zap.String("params", s),
  324. zap.String("error", err.Error()))
  325. return errors.ErrorTransForm(err)
  326. }
  327. ctx.JSON(http.StatusOK, resp)
  328. logReq := OperationLogRequest{
  329. Module:consts.OperationModuleRbac,
  330. Action:consts.OperationActionGroupDel,
  331. Origin:rpcResp.Origin,
  332. Target:req.RbacGroupDelPath,
  333. UserName:tokenInfo.Username,
  334. Uid:tokenInfo.Uid,
  335. OrganizationCode:tokenInfo.OrganizationCode,
  336. }
  337. OperationLogAdd(&logReq)
  338. return nil
  339. }
  340. // 执行任务
  341. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  342. }