123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- // 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)
- }
|