123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package rbac
- import (
- "context"
- "gd_admin/apis"
- "gd_admin/common.in/jsonrpc2"
- "gd_admin/common.in/utils"
- "gd_admin/errors"
- "gd_admin/impl/dbmodel"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- "strings"
- "time"
- )
- // 获取所有的权限列表
- func GetGroupList(ctx context.Context, req *apis.GetRbacGroupListReq, reply *apis.GetRbacGroupListReply) error {
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err := fmt.Errorf("%+v", r)
- e := &jsonrpc2.Error{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- l.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // where
- filter := map[string]interface{}{
- "custom": 0,
- }
- p := dbmodel.TGdAdminRbacGroup{}
- // 获取权限分组信息
- list, err := p.FetchAll(orm.NewOrm(), filter, []string{"id", "name", "rbac_node_list"})
- if err != nil {
- if err == orm.ErrNoRows {
- reply.List = make([]apis.RbacGroupList, 0)
- return nil
- }
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
- zap.String("fields", utils.MarshalJsonString(filter)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- for k := range list {
- reply.List = append(reply.List, apis.RbacGroupList{
- Id: int(list[k].Id),
- Name: list[k].Name,
- Node: strings.Split(list[k].RbacNodeList, ","),
- })
- }
- return nil
- }
- // 修改分组权限
- func UpdateGroup(ctx context.Context, req *apis.UpdateRbacGroupReq, reply *apis.UpdateRbacGroupReply) error {
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err := fmt.Errorf("%+v", r)
- e := &jsonrpc2.Error{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- l.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // 参数验证
- if req.Id <= 0 || req.NodeId == "" || req.Name == "" {
- return errors.ArgsError
- }
- // 去除重复的id
- ids := strings.Split(req.NodeId, ",")
- newIds := utils.StrArrRemoveDuplicates(ids)
- // where
- filter := map[string]interface{}{
- "id": req.Id,
- }
- // value
- value := map[string]interface{}{
- "name": req.Name,
- "rbac_node_list": strings.Join(newIds, ","),
- "updated_at": time.Now().Format("2006-01-02 15:04:05"),
- }
- p := dbmodel.TGdAdminRbacGroup{}
- if _, err := p.Save(orm.NewOrm(), filter, value); err != nil {
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("Update %s", p.TableName())),
- zap.String("fields", utils.MarshalJsonString(filter, value)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return nil
- }
- // 删除分组
- func DeleteGroup(ctx context.Context, req *apis.DeleteRbacGroupReq, reply *apis.DeleteRbacGroupReply) error {
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err := fmt.Errorf("%+v", r)
- e := &jsonrpc2.Error{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- l.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // 参数验证
- if req.Id <= 0 {
- return errors.ArgsError
- }
- // where
- where := map[string]interface{}{
- "group_id": req.Id,
- }
- n := dbmodel.TGdAdminRbacAccess{}
- if err := n.Fetch(orm.NewOrm(), where); err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("SELECT * FROM %s", n.TableName())),
- zap.String("fields", utils.MarshalJsonString(where)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- // 已存在使用用户,不允许删除
- if n.Uid != 0 {
- return errors.DelGroupErr
- }
- // where
- filter := map[string]interface{}{
- "id": req.Id,
- }
- p := dbmodel.TGdAdminRbacGroup{}
- if _, err := p.Delete(orm.NewOrm(), filter); err != nil {
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("Delete %s", p.TableName())),
- zap.String("fields", utils.MarshalJsonString(filter)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return nil
- }
- // 新增分组
- func AddGroup(ctx context.Context, req *apis.AddRbacGroupReq, reply *apis.AddRbacGroupReply) error {
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err := fmt.Errorf("%+v", r)
- e := &jsonrpc2.Error{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- l.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // 参数验证
- if req.Name == "" || req.NodeId == "" {
- return errors.ArgsError
- }
- // 去除重复的id
- ids := strings.Split(req.NodeId, ",")
- newIds := utils.StrArrRemoveDuplicates(ids)
- p := dbmodel.TGdAdminRbacGroup{
- Name: req.Name,
- RbacNodeList: strings.Join(newIds, ","),
- Custom: 0,
- CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdatedAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- if _, err := p.Create(orm.NewOrm()); err != nil {
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("Insert %s", p.TableName())),
- zap.String("fields", utils.MarshalJsonString(p)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return nil
- }
- // 新增自定义分组
- func AddCostomGroup(db orm.Ormer, nodeIds string) (int64, error) {
- p := dbmodel.TGdAdminRbacGroup{}
- // 去除重复的id
- ids := strings.Split(nodeIds, ",")
- newIds := utils.StrArrRemoveDuplicates(ids)
- // where
- filter := map[string]interface{}{
- "rbac_node_list": strings.Join(newIds, ","),
- }
- err := p.Fetch(db, filter)
- switch {
- case err != nil && err != orm.ErrNoRows:
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
- zap.String("fields", utils.MarshalJsonString(filter)),
- zap.String("error", err.Error()))
- return 0, errors.DataBaseError
- case err == orm.ErrNoRows:
- // 不存在分组,新增分组
- p = dbmodel.TGdAdminRbacGroup{
- Name: "自定义",
- RbacNodeList: nodeIds,
- Custom: 1,
- CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdatedAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- id, err := p.Create(db)
- if err != nil {
- l.Error("mysql",
- zap.String("sql", fmt.Sprintf("INSERT %s", p.TableName())),
- zap.String("fields", utils.MarshalJsonString(p)),
- zap.String("error", err.Error()))
- return 0, errors.DataBaseError
- }
- return id, nil
- }
- return p.Id, nil
- }
|