role.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/aegis/model/business"
  5. "go-common/app/admin/main/aegis/model/common"
  6. "go-common/app/admin/main/aegis/model/task"
  7. "go-common/library/log"
  8. )
  9. // GetRole .
  10. func (s *Service) GetRole(c context.Context, opt *common.BaseOptions) (uname string, role int8, err error) {
  11. role, uname, err = s.mc.GetRole(c, opt)
  12. if err == nil && role > 0 {
  13. return
  14. }
  15. var (
  16. cfgs map[int64][]int64 //bid:flowids
  17. mngid int64
  18. )
  19. if cfgs = s.getTaskRole(c, opt.BusinessID); err != nil {
  20. return
  21. }
  22. for bid, flows := range cfgs {
  23. exist := false
  24. for _, flowID := range flows {
  25. if flowID == opt.FlowID {
  26. exist = true
  27. break
  28. }
  29. }
  30. if exist {
  31. mngid = bid
  32. break
  33. }
  34. }
  35. log.Info("GetRole %d %d", mngid, opt.UID)
  36. if mngid <= 0 {
  37. return
  38. }
  39. roles, err := s.http.GetRole(c, mngid, opt.UID)
  40. if err != nil {
  41. return
  42. }
  43. for _, item := range roles {
  44. log.Info("GetRole %d %d %+v", mngid, opt.UID, item)
  45. if int8(item.RID) == task.TaskRoleLeader {
  46. role = task.TaskRoleLeader
  47. break
  48. }
  49. if int8(item.RID) == task.TaskRoleMember {
  50. role = task.TaskRoleMember
  51. }
  52. }
  53. log.Info("GetRole %d %d %d", mngid, opt.UID, role)
  54. if opt.Uname == "" {
  55. unames, _ := s.http.GetUnames(c, []int64{opt.UID})
  56. opt.Uname = unames[opt.UID]
  57. }
  58. uname = opt.Uname
  59. s.mc.SetRole(c, opt, role)
  60. return
  61. }
  62. //GetTaskBizFlows uid能查看哪些任务节点
  63. func (s *Service) GetTaskBizFlows(c context.Context, uid int64) (businessID []int64, flows []int64, err error) {
  64. var (
  65. uroles []*task.Role
  66. )
  67. businessID = []int64{}
  68. flows = []int64{}
  69. //用户角色
  70. if uroles, err = s.http.GetUserRoles(c, uid); err != nil {
  71. log.Error("GetTaskBizFlows s.http.GetUserRoles(%d) error(%v)", uid, err)
  72. return
  73. }
  74. //业务与用户角色的映射关系
  75. bizMap := map[int64]int{}
  76. bizs := []int64{}
  77. for _, item := range uroles {
  78. if item == nil || item.BID <= 0 {
  79. continue
  80. }
  81. bizMap[item.BID] = 1
  82. bizs = append(bizs, item.BID)
  83. }
  84. businessID, flows = s.getTaskBiz(c, bizs)
  85. log.Info("checkAccessTask uid(%d) can see business(%+v)", uid, businessID)
  86. return
  87. }
  88. //GetRoleBiz uid能查看哪些业务
  89. func (s *Service) GetRoleBiz(c context.Context, uid int64, role string, noAdmin bool) (businessID []int64, err error) {
  90. var (
  91. uroles []*task.Role
  92. )
  93. businessID = []int64{}
  94. //用户角色
  95. if uroles, err = s.http.GetUserRoles(c, uid); err != nil {
  96. log.Error("GetRoleBiz s.http.GetUserRoles(%d) error(%v)", uid, err)
  97. return
  98. }
  99. //业务与用户角色的映射关系
  100. bizMap := map[int64]int{}
  101. for _, item := range uroles {
  102. if item == nil || item.BID <= 0 || item.RID <= 0 {
  103. continue
  104. }
  105. roles, bizID := s.getBizRole(c, item.BID)
  106. log.Info("GetRoleBiz s.getBizRole roles(%+v) bizID(%d)", roles, bizID)
  107. if len(roles) == 0 || bizID <= 0 {
  108. continue
  109. }
  110. //没有role, 走bid, 有role,走role过滤; 然后在走noadmin过滤
  111. if (role != "" && roles[role] != item.RID) || (noAdmin && roles[business.BizBIDAdmin] == item.RID) || bizMap[bizID] > 0 {
  112. continue
  113. }
  114. businessID = append(businessID, bizID)
  115. bizMap[bizID] = 1
  116. }
  117. log.Info("uid(%d) can see biz(%v) as role(%s) noadmin(%v)", uid, businessID, role, noAdmin)
  118. return
  119. }
  120. //FlushRole .
  121. func (s *Service) FlushRole(c context.Context, BizID, flowID int64, uids []int64) (err error) {
  122. return s.mc.DelRole(c, BizID, flowID, uids)
  123. }