rbac_node.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package rbac
  2. import (
  3. "gd_admin/apis"
  4. "gd_admin/errors"
  5. "gd_admin/impl/dbmodel"
  6. "encoding/json"
  7. "fmt"
  8. "gd_admin/common.in/jsonrpc2"
  9. "gd_admin/common.in/utils"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. "golang.org/x/net/context"
  13. )
  14. // 获取用户权限节点列表
  15. func getUserRbacNode(id int) (string, error) {
  16. // 捕获各个task中的异常并返回给调用者
  17. defer func() {
  18. if r := recover(); r != nil {
  19. err := fmt.Errorf("%+v", r)
  20. e := &jsonrpc2.Error{}
  21. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  22. l.Error("err",
  23. zap.String("system_err", err.Error()),
  24. zap.Stack("stacktrace"))
  25. }
  26. }
  27. }()
  28. // where
  29. filter := map[string]interface{}{
  30. "id": id,
  31. }
  32. p := dbmodel.TGdAdminRbacGroup{}
  33. err := p.Fetch(orm.NewOrm(), filter)
  34. if err != nil {
  35. if err == orm.ErrNoRows {
  36. return "", errors.AccessNotAllow
  37. }
  38. l.Error("mysql",
  39. zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
  40. zap.String("fields", utils.MarshalJsonString(filter)),
  41. zap.String("error", err.Error()))
  42. return "", errors.DataBaseError
  43. }
  44. return p.RbacNodeList, nil
  45. }
  46. // 获取所有的节点列表
  47. func GetList(ctx context.Context, req *apis.GetNodeListReq, reply *apis.GetNodeListReply) error {
  48. // 捕获各个task中的异常并返回给调用者
  49. defer func() {
  50. if r := recover(); r != nil {
  51. err := fmt.Errorf("%+v", r)
  52. e := &jsonrpc2.Error{}
  53. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  54. l.Error("err",
  55. zap.String("system_err", err.Error()),
  56. zap.Stack("stacktrace"))
  57. }
  58. }
  59. }()
  60. // where
  61. filter := map[string]interface{}{}
  62. p := dbmodel.TGdAdminRbacNode{}
  63. list, err := p.FetchAll(orm.NewOrm(), filter, []string{"id", "name", "pid"})
  64. if err != nil {
  65. l.Error("mysql",
  66. zap.String("sql", fmt.Sprintf("SELECT * FROM %s", p.TableName())),
  67. zap.String("fields", utils.MarshalJsonString(filter)),
  68. zap.String("error", err.Error()))
  69. return errors.DataBaseError
  70. }
  71. for k := range list {
  72. reply.List = append(reply.List, apis.RbacNodeList{
  73. Id: list[k].Id,
  74. Name: list[k].Name,
  75. Pid: list[k].Pid,
  76. })
  77. }
  78. return nil
  79. }