mng_auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. model "go-common/app/admin/main/macross/model/manager"
  6. "go-common/library/log"
  7. )
  8. // GetAuths get auths.
  9. func (s *Service) GetAuths(c context.Context, name string) (res map[string]map[string]*model.Auth, err error) {
  10. res = make(map[string]map[string]*model.Auth)
  11. for system, users := range s.user {
  12. var (
  13. user *model.User
  14. authIDs []int64
  15. auths map[int64]*model.Auth
  16. resTmp map[string]*model.Auth
  17. ok bool
  18. )
  19. if user, ok = users[name]; !ok {
  20. continue
  21. }
  22. if authIDs, ok = s.authRelation[user.RoleID]; !ok {
  23. continue
  24. }
  25. if auths, ok = s.auth[system]; !ok {
  26. continue
  27. }
  28. for _, authID := range authIDs {
  29. if auth, ok := auths[authID]; ok {
  30. if resTmp, ok = res[system]; !ok {
  31. resTmp = make(map[string]*model.Auth)
  32. res[system] = resTmp
  33. }
  34. resTmp[auth.AuthFlag] = auth
  35. }
  36. }
  37. }
  38. return
  39. }
  40. // Auth get auth.
  41. func (s *Service) Auth(c context.Context, system string) (res []*model.Auth) {
  42. for _, auth := range s.auth[system] {
  43. res = append(res, auth)
  44. }
  45. sort.Sort(model.Auths(res))
  46. return
  47. }
  48. // SaveAuth save auth.
  49. func (s *Service) SaveAuth(c context.Context, authID int64, system, authName, authFlag string) (err error) {
  50. var rows int64
  51. if authID == 0 {
  52. if rows, err = s.dao.AddAuth(c, system, authName, authFlag); err != nil {
  53. log.Error("s.dao.AddAuth(%s, %s, %s) error(%v)", system, authName, authFlag, err)
  54. return
  55. }
  56. } else {
  57. if rows, err = s.dao.UpAuth(c, authName, authID); err != nil {
  58. log.Error("s.dao.UpAuth(%s, %d) error(%v)", authName, authID, err)
  59. return
  60. }
  61. }
  62. if rows != 0 {
  63. // update cache
  64. s.loadAuthCache()
  65. }
  66. return
  67. }
  68. // DelAuth del auth.
  69. func (s *Service) DelAuth(c context.Context, authID int64) (err error) {
  70. var rows int64
  71. if rows, err = s.dao.DelAuth(c, authID); err != nil {
  72. log.Error("s.dao.DelAuth(%d) error(%s)", authID, err)
  73. return
  74. } else if rows != 0 {
  75. // update cache
  76. s.loadAuthCache()
  77. if rows, err = s.dao.CleanAuthRelationByAuth(c, authID); err != nil {
  78. log.Error("s.dao.CleanAuthRelationByAuth(%d) error(%v)", authID, err)
  79. return
  80. } else if rows != 0 {
  81. s.loadAuthRelationCache()
  82. }
  83. }
  84. return
  85. }
  86. // AuthRelation get auth relation.
  87. func (s *Service) AuthRelation(c context.Context, roleID, authID int64, state int) (err error) {
  88. var rows int64
  89. if state == 0 {
  90. if rows, err = s.dao.DelAuthRelation(c, roleID, authID); err != nil {
  91. log.Error("s.dao.DelAuthRelation(%d, %d) error(%v)", roleID, authID, err)
  92. return
  93. }
  94. } else {
  95. if rows, err = s.dao.AddAuthRelation(c, roleID, authID); err != nil {
  96. log.Error("s.dao.AddAuthRelation(%d, %d) error(%v)", roleID, authID, err)
  97. return
  98. }
  99. }
  100. if rows != 0 {
  101. s.loadAuthRelationCache()
  102. }
  103. return
  104. }