user.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/xstr"
  10. )
  11. const _logout = "http://dashboard-mng.bilibili.co/logout?caller=manager-admin"
  12. func authUser(c *bm.Context) {
  13. var (
  14. username string
  15. )
  16. if un, ok := c.Get("username"); ok {
  17. username = un.(string)
  18. } else {
  19. c.JSON(nil, ecode.Unauthorized)
  20. return
  21. }
  22. c.JSON(mngSvc.Auth(c, username))
  23. }
  24. func logout(c *bm.Context) {
  25. // purge mid cache
  26. c.Redirect(http.StatusFound, _logout)
  27. }
  28. func permissions(c *bm.Context) {
  29. var username string
  30. if username = c.Request.Form.Get("username"); username == "" {
  31. c.JSON(nil, ecode.RequestErr)
  32. return
  33. }
  34. c.JSON(mngSvc.Permissions(c, username))
  35. }
  36. func users(c *bm.Context) {
  37. var (
  38. err error
  39. pn, ps int
  40. params = c.Request.Form
  41. pnStr = params.Get("pn")
  42. psStr = params.Get("ps")
  43. )
  44. if pn, err = strconv.Atoi(pnStr); err != nil || pn <= 0 {
  45. pn = 1
  46. }
  47. if ps, err = strconv.Atoi(psStr); err != nil || ps <= 0 {
  48. ps = 20
  49. }
  50. c.JSON(mngSvc.Users(c, pn, ps))
  51. }
  52. func usersTotal(c *bm.Context) {
  53. var (
  54. err error
  55. total int64
  56. )
  57. if total, err = mngSvc.UsersTotal(c); err != nil {
  58. log.Error("mngSvc.UsersTotal error(%v)", err)
  59. c.JSON(nil, err)
  60. return
  61. }
  62. c.JSON(struct {
  63. Total int64 `json:"total"`
  64. }{total}, nil)
  65. }
  66. func heartbeat(c *bm.Context) {
  67. un, ok := c.Get("username")
  68. if !ok {
  69. log.Error("username not found in context")
  70. return
  71. }
  72. c.JSON(nil, mngSvc.Heartbeat(c, un.(string)))
  73. }
  74. // batch check unames
  75. func usersNames(c *bm.Context) {
  76. var (
  77. err error
  78. params = c.Request.Form
  79. uids string
  80. uidsV []int64
  81. items map[int64]string
  82. )
  83. if uids = params.Get("uids"); uids == "" {
  84. c.JSON(nil, ecode.RequestErr)
  85. return
  86. }
  87. if uidsV, err = xstr.SplitInts(uids); err != nil {
  88. log.Error("mngSvc.Unames(%s) error(%v)", uids, err)
  89. c.JSON(nil, err)
  90. return
  91. }
  92. items = mngSvc.Unames(c, uidsV)
  93. c.JSON(items, nil)
  94. }
  95. // batch check users' departments
  96. func usersDepts(c *bm.Context) {
  97. var (
  98. err error
  99. params = c.Request.Form
  100. uids string
  101. uidsV []int64
  102. items map[int64]string
  103. )
  104. if uids = params.Get("uids"); uids == "" {
  105. c.JSON(nil, ecode.RequestErr)
  106. return
  107. }
  108. if uidsV, err = xstr.SplitInts(uids); err != nil {
  109. log.Error("mngSvc.Udepts(%s) error(%v)", uids, err)
  110. c.JSON(nil, err)
  111. return
  112. }
  113. items = mngSvc.Udepts(c, uidsV)
  114. c.JSON(items, nil)
  115. }
  116. func userIds(c *bm.Context) {
  117. var (
  118. params = c.Request.Form
  119. unames string
  120. unamesV []string
  121. items map[string]int64
  122. )
  123. if unames = params.Get("unames"); unames == "" {
  124. c.JSON(nil, ecode.RequestErr)
  125. return
  126. }
  127. unamesV = strings.Split(unames, ",")
  128. items = mngSvc.UIds(c, unamesV)
  129. c.JSON(items, nil)
  130. }