// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package v1 import ( "cp-organization-management-gateway/errors" param_v1 "cp-organization-management-gateway/param/v1" "cp-organization-management-gateway/pb" "cp-organization-management-gateway/pb/v1" "cp-organization-management-gateway/utils" "cp-organization-management-gateway/consts" "github.com/jaryhe/gopkgs/logger" "github.com/jaryhe/gopkgs/tasker/httptasker" "github.com/jaryhe/gopkgs/util" "net/http" "github.com/gin-gonic/gin" "go.uber.org/zap" ) func handleRbacList(list []*v1.RbacNodeItem) ([]*v1.RbacNodeItem) { if len(list) == 0 { list = make([]*v1.RbacNodeItem, 0) return list } for i, v := range list { list[i].Childs = handleRbacList(v.Childs) } return list } // // @Summary 权限节点列表 // @Description 权限节点列表 // @Tags 系统管理-角色管理 // @Accept json // @Produce json // @Param token header string true "token" // @Param group_id query int64 false "不填返回所有,填了返回该角色下的权限" // @Param only_select query bool false "填了groupid后,true:仅返回角色下的节点, false:返回所有节点,对属于该角色的节点作标记" // @Success 200 {object} v1.RbacNodeListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/system/rbac/node/list [get] func (c *Controller) RbacNodeList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.RbacNodeListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.RbacNodeListQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetTokeInfo(ctx) if err != nil { return err } if req.GroupId > 0 { resp := param_v1.RbacNodeListByGroupOrUserResponse{} rpcReq := &v1.RbacNodeListByGroupOrUserRequest{ OrganizationCode:tokenInfo.OrganizationCode, GroupId:req.GroupId, Select:req.OnlySelect, } rpcRsp, err := pb.Organization.RbacNodeListByGroupOrUser(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.RbacNodeListByGroupOrUser"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } rpcRsp.List = handleRbacList(rpcRsp.List) resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } resp := param_v1.RbacNodeListResponse{} rpcReq := &v1.RbacNodeListRequest{ OrganizationCode:tokenInfo.OrganizationCode, IsAll:false, } rpcRsp, err := pb.Organization.RbacNodeList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.RbacNodeList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } rpcRsp.List = handleRbacList(rpcRsp.List) resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 角色列表 // @Description 角色列表 // @Tags 系统管理-角色管理 // @Accept json // @Produce json // @Param token header string true "token" // @Success 200 {object} v1.RbacGroupListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/system/rbac/group/list [get] func (c *Controller) RbacGroupList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.RbacGroupListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { // 响应数据 tokenInfo, err := utils.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.RbacGroupListResponse{} rpcReq := &v1.RbacGroupListRequest{ OrganizationCode:tokenInfo.OrganizationCode, IsSuper:tokenInfo.IsSuper, } rpcRsp, err := pb.Organization.RbacGroupList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.RbacGroupList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 添加角色 // @Description 添加角色 // @Tags 系统管理-角色管理 // @Accept json // @Produce json // @Param token header string true "token" // @Param body body v1.RbacGroupAddBody true "token" // @Success 200 {object} v1.RbacGroupAddResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/system/rbac/group [post] func (c *Controller) RbacGroupAdd(ctx *gin.Context) { // 解析参数 req := ¶m_v1.RbacGroupAddRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.RbacGroupAddBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { // 响应数据 tokenInfo, err := utils.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.RbacGroupAddResponse{} rpcReq := &v1.RbacGroupAddRequest{ OrganizationCode:tokenInfo.OrganizationCode, NodeList:req.NodeList, Name:req.Name, Uid:tokenInfo.Uid, } rpcRsp, err := pb.Organization.RbacGroupAdd(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.RbacGroupAdd"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:consts.OperationModuleRbac, Action:consts.OperationActionGroupAdd, Origin:nil, Target:req.RbacGroupAddBody, UserName:tokenInfo.Username, Uid:tokenInfo.Uid, OrganizationCode:tokenInfo.OrganizationCode, } OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 修改角色 // @Description 修改角色 // @Tags 系统管理-角色管理 // @Accept json // @Produce json // @Param token header string true "token" // @Param body body v1.RbacGroupUpdateBody true "token" // @Success 200 {object} v1.RbacGroupUpdateResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/system/rbac/group [put] func (c *Controller) RbacGroupUpdate(ctx *gin.Context) { // 解析参数 req := ¶m_v1.RbacGroupUpdateRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.RbacGroupUpdateBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { // 响应数据 tokenInfo, err := utils.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.RbacGroupUpdateResponse{} rpcReq := &v1.RbacGroupUpdateRequest{ OrganizationCode:tokenInfo.OrganizationCode, NodeList:req.NodeList, Name:req.Name, Id:req.Id, Uid:tokenInfo.Uid, } rpcResp, err := pb.Organization.RbacGroupUpdate(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.RbacGroupUpdate"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:consts.OperationModuleRbac, Action:consts.OperationActionGroupUpdate, Origin:rpcResp.Origin, Target:req.RbacGroupUpdateBody, UserName:tokenInfo.Username, Uid:tokenInfo.Uid, OrganizationCode:tokenInfo.OrganizationCode, } OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 删除角色 // @Description 删除角色 // @Tags 系统管理-角色管理 // @Accept json // @Produce json // @Param token header string true "token" // @Param id path int64 true "token" // @Success 200 {object} v1.RbacGroupDelResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/system/rbac/group/{id} [delete] func (c *Controller) RbacGroupDel(ctx *gin.Context) { // 解析参数 req := ¶m_v1.RbacGroupDelRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, &req.RbacGroupDelPath, nil, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { // 响应数据 tokenInfo, err := utils.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.RbacGroupDelResponse{} rpcReq := &v1.RbacGroupDelRequest{ OrganizationCode:tokenInfo.OrganizationCode, Id:req.Id, Uid:tokenInfo.Uid, } rpcResp, err := pb.Organization.RbacGroupDel(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.RbacGroupDel"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:consts.OperationModuleRbac, Action:consts.OperationActionGroupDel, Origin:rpcResp.Origin, Target:req.RbacGroupDelPath, UserName:tokenInfo.Username, Uid:tokenInfo.Uid, OrganizationCode:tokenInfo.OrganizationCode, } OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }