access.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package view
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-intl/model"
  5. account "go-common/app/service/main/account/model"
  6. "go-common/app/service/main/archive/model/archive"
  7. locmdl "go-common/app/service/main/location/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. // ipLimit ip limit
  13. func (s *Service) ipLimit(c context.Context, mid, aid int64, cdnIP string) (down int64, err error) {
  14. var auth *locmdl.Auth
  15. ip := metadata.String(c, metadata.RemoteIP)
  16. if auth, err = s.locDao.Archive(c, aid, mid, ip, cdnIP); err != nil {
  17. log.Error("%v", err)
  18. err = nil // NOTE: return or ignore err???
  19. }
  20. if auth != nil {
  21. down = auth.Down
  22. switch auth.Play {
  23. case locmdl.Forbidden:
  24. err = ecode.AccessDenied
  25. s.prom.Incr("ip_limit_access")
  26. }
  27. }
  28. return
  29. }
  30. // areaLimit area limit
  31. func (s *Service) areaLimit(c context.Context, plat int8, rid int16) (err error) {
  32. ip := metadata.String(c, metadata.RemoteIP)
  33. if rm, ok := s.region[plat]; !ok {
  34. return
  35. } else if r, ok := rm[rid]; ok && r != nil && r.Area != "" {
  36. var auths map[string]*locmdl.Auth
  37. if auths, err = s.locDao.AuthPIDs(c, r.Area, ip); err != nil {
  38. log.Error("error(%v) area(%v) ip(%v)", err, r.Area, ip)
  39. err = nil
  40. return
  41. }
  42. if auth, ok := auths[r.Area]; ok && auth.Play == locmdl.Forbidden {
  43. log.Error("zlimit region area(%s) ip(%v) forbid", r.Area, ip)
  44. err = ecode.NothingFound
  45. s.prom.Incr("region_limit_access")
  46. }
  47. }
  48. return
  49. }
  50. // checkAceess check user Aceess
  51. func (s *Service) checkAceess(c context.Context, mid, aid int64, state, access int, ak string) (err error) {
  52. if state >= 0 && access == 0 {
  53. return
  54. }
  55. if state < 0 {
  56. if state == archive.StateForbidFixed {
  57. log.Warn("archive(%d) is fixed", aid)
  58. } else if state == archive.StateForbidUpDelete {
  59. log.Warn("archive(%d) is deleted", aid)
  60. } else {
  61. log.Warn("mid(%d) have not access view not pass archive(%d) ", mid, aid)
  62. }
  63. err = ecode.NothingFound
  64. return
  65. }
  66. if mid == 0 {
  67. log.Warn("not login can not view(%d) state(%d) access(%d)", aid, state, access)
  68. err = ecode.AccessDenied
  69. s.prom.Incr("no_login_access")
  70. return
  71. }
  72. card, err := s.accDao.Card3(c, mid)
  73. if err != nil || card == nil {
  74. if err != nil {
  75. log.Error("s.accDao.Info(%d) error(%v)", mid, err)
  76. }
  77. err = ecode.AccessDenied
  78. log.Warn("s.accDao.Info failed can not view(%d) state(%d) access(%d)", aid, state, access)
  79. s.prom.Incr("err_login_access")
  80. return
  81. }
  82. if access > 0 && int(card.Rank) < access && (card.Vip.Type == 0 || card.Vip.Status == 0 || card.Vip.Status == 2 || card.Vip.Status == 3) {
  83. err = ecode.AccessDenied
  84. log.Warn("mid(%d) rank(%d) vip(tp:%d,status:%d) have not access(%d) view archive(%d) ", mid, card.Rank, card.Vip.Type, card.Vip.Status, access, aid)
  85. s.prom.Incr("login_access")
  86. }
  87. return
  88. }
  89. // checkVIP check user is vip or no .
  90. func (s *Service) checkVIP(c context.Context, mid int64) (vip bool) {
  91. var (
  92. card *account.Card
  93. err error
  94. )
  95. if mid > 0 {
  96. if card, err = s.accDao.Card3(c, mid); err != nil || card == nil {
  97. log.Warn("s.acc.Info(%d) error(%v)", mid, err)
  98. return
  99. }
  100. vip = card.Vip.Type > 0 && card.Vip.Status == 1
  101. }
  102. return
  103. }
  104. func (s *Service) overseaCheck(a *archive.Archive3, plat int8) bool {
  105. if a.AttrVal(archive.AttrBitOverseaLock) == archive.AttrYes && model.IsOverseas(plat) {
  106. s.prom.Incr("oversea_access")
  107. return true
  108. }
  109. return false
  110. }