banned.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package auth
  2. import (
  3. "strconv"
  4. "time"
  5. xsql "go-common/library/database/sql"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. xauth "go-common/library/net/http/blademaster/middleware/auth"
  10. )
  11. const (
  12. _queryBannedUser = "select `expire_time`,`forbid_status` from `forbid_user` where `mid` = ?;"
  13. )
  14. // BannedAuth .
  15. type BannedAuth struct {
  16. *xauth.Auth
  17. db *xsql.DB
  18. }
  19. // NewBannedAuth .
  20. func NewBannedAuth(authConf *xauth.Config, dbConf *xsql.Config) *BannedAuth {
  21. return &BannedAuth{
  22. Auth: xauth.New(authConf),
  23. db: xsql.NewMySQL(dbConf),
  24. }
  25. }
  26. // User .
  27. func (ba *BannedAuth) User(ctx *bm.Context) {
  28. ba.Auth.User(ctx)
  29. // 只过滤写接口
  30. if ctx.Request.Method != "POST" {
  31. return
  32. }
  33. // 获取MID
  34. var mid int64
  35. tmp, _ := ctx.Get("mid")
  36. switch tmp.(type) {
  37. case int64:
  38. mid = tmp.(int64)
  39. }
  40. if mid == 0 {
  41. return
  42. }
  43. // 过滤封禁用户
  44. var expired, status int
  45. row := ba.db.QueryRow(ctx, _queryBannedUser, mid)
  46. row.Scan(&expired, &status)
  47. if status == 1 && expired > 0 && expired > int(time.Now().Unix()) {
  48. // 封禁用户
  49. log.Infov(ctx, log.KV("log", "user banned "+strconv.Itoa(int(mid))), log.KV("err", ecode.BBQUserBanned))
  50. ctx.Error = ecode.BBQUserBanned
  51. ctx.JSON(nil, ecode.BBQUserBanned)
  52. ctx.Abort()
  53. }
  54. }