123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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"
- "strconv"
- "strings"
- )
- func nodeSelect(m map[int64]bool, all []*pb_v1.RbacNodeItem) []*pb_v1.RbacNodeItem {
- for i, v := range all {
- if _, ok := m[v.Id]; ok {
- all[i].Select = true
- if len(all[i].Childs) > 0 {
- all[i].Childs = nodeSelect(m, all[i].Childs)
- }
- }
- }
- return all
- }
- func getNodeListByGroupId(id int64, code string)(reply *pb_v1.RbacNodeListByGroupOrUserReply, err error) {
- reply = &pb_v1.RbacNodeListByGroupOrUserReply{}
- dbname := utils.GetDbName(code)
- // 取id列表
- group := model.NewRbacGroup(dbname)
- where := map[string]interface{}{
- "id":id,
- }
- err = group.Find(database.DB(), where)
- if err != nil {
- return nil, status.Error(10012, "角色不存在")
- }
- if group.NodeList == "" {
- return reply, nil
- }
- idStrs := strings.Split(group.NodeList, ",")
- ids := make([]int64, len(idStrs))
- for i, v := range idStrs {
- ids[i], _ = strconv.ParseInt(v, 10, 64)
- }
- // 取目标group节点
- p := model.NewRbacNode(dbname)
- where = map[string]interface{}{
- "id in":ids,
- }
- list, err := p.ListAll(database.DB(), where, nil)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- return nil, errors.DataBaseError
- }
- // 如果目标角色是超管,获取所有节点
- // 否则获取除super 外的所有节点
- mreq := pb_v1.RbacNodeListRequest{OrganizationCode:code, IsAll:false}
- if group.IsSuperGroup {
- mreq.IsAll = true
- }
- mreply, err := RbacNodeList(context.Background(), &mreq)
- if err != nil {
- return nil, err
- }
- m := map[int64]bool{}
- for _, v := range list {
- m[v.Id] = true
- }
- reply.List = nodeSelect(m, mreply.List)
- return reply, nil
- }
- func getNodeListByGroupIdOnlySelect(id int64, code string)(reply *pb_v1.RbacNodeListByGroupOrUserReply, err error) {
- reply = &pb_v1.RbacNodeListByGroupOrUserReply{}
- dbname := utils.GetDbName(code)
- // 取id列表
- group := model.NewRbacGroup(dbname)
- where := map[string]interface{}{
- "id":id,
- }
- err = group.Find(database.DB(), where)
- if err != nil {
- return nil, status.Error(10012, "角色不存在")
- }
- if group.NodeList == "" {
- return reply, nil
- }
- idStrs := strings.Split(group.NodeList, ",")
- ids := make([]int64, len(idStrs))
- for i, v := range idStrs {
- ids[i], _ = strconv.ParseInt(v, 10, 64)
- }
- // 取目标group节点
- p := model.NewRbacNode(dbname)
- where = map[string]interface{}{
- "id in":ids,
- }
- list, err := p.ListAll(database.DB(), where, nil)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- return nil, errors.DataBaseError
- }
- reply.List = NodeTreeSelect(list)
- return reply, nil
- }
- func RbacNodeListByGroupOrUser(ctx context.Context, req *pb_v1.RbacNodeListByGroupOrUserRequest) (reply *pb_v1.RbacNodeListByGroupOrUserReply, err error) {
- reply = &pb_v1.RbacNodeListByGroupOrUserReply{}
- // 捕获各个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
- }
- if req.Uid < 1 && req.GroupId < 1 {
- return nil, errors.ParamsError
- }
- if req.GroupId > 0 {
- if req.Select {
- return getNodeListByGroupIdOnlySelect(req.GroupId, req.OrganizationCode)
- }
- return getNodeListByGroupId(req.GroupId, req.OrganizationCode)
- }
- dbname := utils.GetDbName(req.OrganizationCode)
- p := model.NewRbacUser(dbname)
- where := map[string]interface{}{
- "id":req.Uid,
- }
- err = p.Find(database.DB(), where)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, status.Error(30202, "用户不存在")
- }
- return nil, errors.DataBaseError
- }
- if req.Select {
- return getNodeListByGroupIdOnlySelect(p.GroupId, req.OrganizationCode)
- }
- return getNodeListByGroupId(p.GroupId, req.OrganizationCode)
- }
|