user_info.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package user
  2. import (
  3. "context"
  4. "cp-organization-management/errors"
  5. "cp-organization-management/impl/v1/common"
  6. "cp-organization-management/impl/v1/rbac"
  7. "cp-organization-management/impl/v1/zone"
  8. "cp-organization-management/model"
  9. pb_v1 "cp-organization-management/pb/v1"
  10. "cp-organization-management/utils"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/jaryhe/gopkgs/database"
  14. "github.com/jaryhe/gopkgs/logger"
  15. "github.com/jinzhu/gorm"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func getUserBaseInfo(user *model.RbacUser, reply *pb_v1.UserInfoReply) error {
  20. reply.Id = user.Id
  21. reply.GroupId = user.GroupId
  22. reply.Name = user.Name
  23. reply.Username = user.Username
  24. reply.Email = user.Email
  25. reply.Phone = user.Phone
  26. return nil
  27. }
  28. func getUserRbacInfo(req *pb_v1.UserInfoRequest, reply *pb_v1.UserInfoReply, dbname string, groupId int64) error {
  29. if groupId < 1 {
  30. return nil
  31. }
  32. group := model.NewRbacGroup(dbname)
  33. where := map[string]interface{}{
  34. "id":groupId,
  35. }
  36. err := group.Find(database.DB(), where)
  37. if err != nil {
  38. if err == gorm.ErrRecordNotFound {
  39. return nil
  40. }
  41. return errors.DataBaseError
  42. }
  43. mreq := pb_v1.RbacNodeListByGroupOrUserRequest{OrganizationCode:req.OrganizationCode, GroupId:groupId, Select:true}
  44. mreply, err := rbac.RbacNodeListByGroupOrUser(context.Background(), &mreq)
  45. if err != nil {
  46. return err
  47. }
  48. reply.NodeList = mreply.List
  49. return nil
  50. }
  51. func selectUserZone(zones map[string]string, loginUserZones []*pb_v1.ZoneItem) ([]*pb_v1.ZoneItemSelect) {
  52. list := make([]*pb_v1.ZoneItemSelect, len(loginUserZones))
  53. for i, v := range loginUserZones {
  54. list[i] = &pb_v1.ZoneItemSelect{
  55. ZoneName:v.ZoneName,
  56. ZoneCode:v.ZoneCode,
  57. ParentZoneCode:v.ParentZoneCode,
  58. }
  59. if _, ok := zones[v.ZoneCode]; ok {
  60. list[i].Select = true
  61. }
  62. if len(v.Childs) > 0 {
  63. list[i].Childs = selectUserZone(zones, v.Childs)
  64. }
  65. }
  66. return list
  67. }
  68. func selectUserZoneAll(loginUserZones []*pb_v1.ZoneItem) ([]*pb_v1.ZoneItemSelect) {
  69. list := make([]*pb_v1.ZoneItemSelect, len(loginUserZones))
  70. for i, v := range loginUserZones {
  71. list[i] = &pb_v1.ZoneItemSelect{
  72. ZoneName:v.ZoneName,
  73. ZoneCode:v.ZoneCode,
  74. ParentZoneCode:v.ParentZoneCode,
  75. Select:true,
  76. }
  77. if len(v.Childs) > 0 {
  78. list[i].Childs = selectUserZoneAll(v.Childs)
  79. }
  80. }
  81. return list
  82. }
  83. func getUserZoneInfo(req *pb_v1.UserInfoRequest, reply *pb_v1.UserInfoReply, dbname string) error {
  84. // 获取登录用户的区域
  85. loginUserZoneReq := pb_v1.ZoneListRequest{OrganizationCode:req.OrganizationCode, Uid:req.Uid}
  86. loginUserZoneReply, err := zone.ZoneList(context.Background(), &loginUserZoneReq)
  87. if err != nil {
  88. return err
  89. }
  90. if len(loginUserZoneReply.List) == 0 {
  91. return nil
  92. }
  93. // 获取指定用户的区域
  94. super, zones, err := common.GetUserZone(req.Id, dbname)
  95. if err != nil {
  96. return err
  97. }
  98. // 不能对超级用户进行操作
  99. if super {
  100. reply.ZoneList = selectUserZoneAll(loginUserZoneReply.List)
  101. return nil
  102. }
  103. // 在登录用户的区域中,勾选指定用户的区域
  104. reply.ZoneList = selectUserZone(zones, loginUserZoneReply.List)
  105. return nil
  106. }
  107. func UserInfo(ctx context.Context, req *pb_v1.UserInfoRequest)(reply *pb_v1.UserInfoReply, err error) {
  108. reply = &pb_v1.UserInfoReply{}
  109. defer func() {
  110. if r := recover(); r != nil {
  111. err = fmt.Errorf("%+v", r)
  112. e := &status.Status{}
  113. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  114. logger.Error("err",
  115. zap.String("system_err", err.Error()),
  116. zap.Stack("stacktrace"))
  117. }
  118. }
  119. }()
  120. dbname := utils.GetDbName(req.OrganizationCode)
  121. loginUserInfo, err := common.GetUserBaseInfo(req.Uid, dbname)
  122. if err != nil {
  123. return nil, err
  124. }
  125. targetUserInfo, err := common.GetUserBaseInfo(req.Id, dbname)
  126. if err != nil {
  127. return nil, err
  128. }
  129. err = UserPermissionCheck(loginUserInfo, targetUserInfo, dbname)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if err = getUserBaseInfo(targetUserInfo, reply); err != nil {
  134. return nil, err
  135. }
  136. if err = getUserRbacInfo(req, reply, dbname, reply.GroupId); err != nil {
  137. return nil, err
  138. }
  139. if err = getUserZoneInfo(req, reply, dbname); err != nil {
  140. return nil, err
  141. }
  142. return reply, nil
  143. }