token.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/aegis/model"
  6. "go-common/app/admin/main/aegis/model/net"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. //GetTokenList .
  11. func (s *Service) GetTokenList(c context.Context, pm *net.ListTokenParam) (result *net.ListTokenRes, err error) {
  12. var (
  13. unames map[int64]string
  14. )
  15. if result, err = s.gorm.TokenListWithPager(c, pm); err != nil {
  16. return
  17. }
  18. if len(result.Result) == 0 {
  19. return
  20. }
  21. //get username
  22. uid := []int64{}
  23. for _, item := range result.Result {
  24. if item.UID > 0 {
  25. uid = append(uid, item.UID)
  26. }
  27. }
  28. if len(uid) == 0 {
  29. return
  30. }
  31. if unames, err = s.http.GetUnames(c, uid); err != nil || len(unames) == 0 {
  32. log.Error("GetTokenList s.http.GetUnames error(%v) or empty uid(%+v)", err, uid)
  33. err = nil
  34. }
  35. for _, item := range result.Result {
  36. item.Username = unames[item.UID]
  37. }
  38. return
  39. }
  40. //TokenGroupByType .
  41. func (s *Service) TokenGroupByType(c context.Context, netID int64) (result map[string][]*net.Token, err error) {
  42. var (
  43. list []*net.Token
  44. )
  45. result = make(map[string][]*net.Token)
  46. if list, err = s.gorm.TokenList(c, []int64{netID}, nil, "", true); err != nil || len(list) == 0 {
  47. return
  48. }
  49. for _, item := range list {
  50. typeDesc := net.TokenValueTypeDesc[item.Type]
  51. if _, exist := result[typeDesc]; !exist {
  52. result[typeDesc] = []*net.Token{item}
  53. continue
  54. }
  55. result[typeDesc] = append(result[typeDesc], item)
  56. }
  57. return
  58. }
  59. //TokenByName .
  60. func (s *Service) TokenByName(c context.Context, businessID int64, name string) (result map[string]string, err error) {
  61. var (
  62. netID []int64
  63. tokenList []*net.Token
  64. )
  65. result = map[string]string{}
  66. if netID, err = s.netIDByBusiness(c, businessID); err != nil {
  67. log.Error("TokenByName s.netIDByBusiness(%d) error(%v)", businessID, err)
  68. return
  69. }
  70. if len(netID) == 0 {
  71. return
  72. }
  73. if tokenList, err = s.gorm.TokenList(c, netID, nil, name, true); err != nil {
  74. log.Error("TokenByName s.gorm.TokenList(%v, %s) error(%v) businessid(%d)", netID, name, err, businessID)
  75. return
  76. }
  77. for _, item := range tokenList {
  78. result[item.Value] = item.ChName
  79. }
  80. return
  81. }
  82. //ShowToken .
  83. func (s *Service) ShowToken(c context.Context, id int64) (n *net.Token, err error) {
  84. return s.gorm.TokenByID(c, id)
  85. }
  86. func (s *Service) checkTokenUnique(c context.Context, netID int64, name string, compare int8, value string) (err error, msg string) {
  87. var exist *net.Token
  88. if exist, err = s.gorm.TokenByUnique(c, netID, name, compare, value); err != nil {
  89. log.Error("checkTokenUnique s.gorm.TokenByUnique(%d,%s,%d,%s) error(%v)", netID, name, compare, value, err)
  90. return
  91. }
  92. if exist != nil {
  93. err = ecode.AegisUniqueAlreadyExist
  94. msg = fmt.Sprintf(ecode.AegisUniqueAlreadyExist.Message(), "令牌",
  95. fmt.Sprintf("%s %s %s", name, net.GetTokenCompare(compare), value))
  96. }
  97. return
  98. }
  99. //AddToken .
  100. func (s *Service) AddToken(c context.Context, t *net.Token) (id int64, err error, msg string) {
  101. if err, msg = s.checkTokenUnique(c, t.NetID, t.Name, t.Compare, t.Value); err != nil {
  102. return
  103. }
  104. if err = s.gorm.AddItem(c, nil, t); err != nil {
  105. return
  106. }
  107. id = t.ID
  108. //日志
  109. oper := &model.NetConfOper{
  110. OID: t.ID,
  111. Action: model.LogNetActionNew,
  112. UID: t.UID,
  113. NetID: t.NetID,
  114. ChName: t.ChName,
  115. Diff: []string{t.FormatLog()},
  116. }
  117. s.sendNetConfLog(c, model.LogTypeTokenConf, oper)
  118. return
  119. }