123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package rbac
- import (
- "context"
- "cp-organization-management/errors"
- "cp-organization-management/model"
- pb_v1 "cp-organization-management/pb/v1"
- "cp-organization-management/utils"
- "encoding/json"
- "fmt"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func getChild(pid int64, m map[int64][]*pb_v1.RbacNodeItem) (ret []*pb_v1.RbacNodeItem) {
- if _, ok := m[pid]; !ok {
- return ret
- }
- ret = append(ret, m[pid]...)
- for k, v := range ret {
- ret[k].Childs = getChild(v.Id, m)
- }
- return ret
- }
- func NodeTree(array []model.RbacNode) (ret []*pb_v1.RbacNodeItem) {
- m := map[int64][]*pb_v1.RbacNodeItem{}
- for _, v := range array {
- item := pb_v1.RbacNodeItem{
- Id:v.Id,
- Pid:v.Pid,
- Method:v.Method,
- Resource:v.Resource,
- Object:v.Object,
- Router:v.Router,
- Name:v.Name,
- }
- m[v.Pid] = append(m[v.Pid], &item)
- }
- return getChild(0, m)
- }
- func NodeTreeSelect(array []model.RbacNode) (ret []*pb_v1.RbacNodeItem) {
- m := map[int64][]*pb_v1.RbacNodeItem{}
- for _, v := range array {
- item := pb_v1.RbacNodeItem{
- Id:v.Id,
- Pid:v.Pid,
- Method:v.Method,
- Resource:v.Resource,
- Object:v.Object,
- Router:v.Router,
- Name:v.Name,
- Select:true,
- }
- m[v.Pid] = append(m[v.Pid], &item)
- }
- return getChild(0, m)
- }
- func RbacNodeList(ctx context.Context, req *pb_v1.RbacNodeListRequest) (reply *pb_v1.RbacNodeListReply, err error) {
- reply = &pb_v1.RbacNodeListReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- if req.OrganizationCode == "" {
- return nil, errors.ParamsError
- }
- dbname := utils.GetDbName(req.OrganizationCode)
- p := model.NewRbacNode(dbname)
- list := []model.RbacNode{}
- where := map[string]interface{}{
- "is_super":false,
- }
- if req.IsAll {
- where = nil
- }
- err = p.QueryAll(database.DB(), where, &list)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- return nil, errors.DataBaseError
- }
- reply.List = NodeTree(list)
- return reply, nil
- }
|