group_list.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package rbac
  2. import (
  3. "context"
  4. "cp-organization-management/errors"
  5. "cp-organization-management/model"
  6. pb_v1 "cp-organization-management/pb/v1"
  7. "cp-organization-management/utils"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/jaryhe/gopkgs/database"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "github.com/jinzhu/gorm"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. )
  16. func RbacGroupList(ctx context.Context, req *pb_v1.RbacGroupListRequest) (reply *pb_v1.RbacGroupListReply, err error) {
  17. reply = &pb_v1.RbacGroupListReply{}
  18. // 捕获各个task中的异常并返回给调用者
  19. defer func() {
  20. if r := recover(); r != nil {
  21. err = fmt.Errorf("%+v", r)
  22. e := &status.Status{}
  23. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  24. logger.Error("err",
  25. zap.String("system_err", err.Error()),
  26. zap.Stack("stacktrace"))
  27. }
  28. }
  29. }()
  30. if req.OrganizationCode == "" {
  31. return nil, errors.ParamsError
  32. }
  33. dbname := utils.GetDbName(req.OrganizationCode)
  34. p := model.NewRbacGroup(dbname)
  35. list := []model.RbacGroup{}
  36. where := map[string]interface{}{
  37. "is_super_group":false,
  38. }
  39. // 超级管理员可以看见所有
  40. if req.IsSuper {
  41. where = nil
  42. }
  43. err = p.QueryAll(database.DB(), where, &list)
  44. if err != nil {
  45. if err == gorm.ErrRecordNotFound {
  46. return reply, nil
  47. }
  48. return nil, errors.DataBaseError
  49. }
  50. reply.List = make([]*pb_v1.RbacGroupItem, len(list))
  51. for i, v := range list {
  52. reply.List[i] = &pb_v1.RbacGroupItem{
  53. Name: v.Name,
  54. Id: v.Id,
  55. }
  56. }
  57. return reply, nil
  58. }