service.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/manager/conf"
  6. "go-common/app/admin/main/manager/dao"
  7. "go-common/app/admin/main/manager/model"
  8. )
  9. // Service biz service def.
  10. type Service struct {
  11. c *conf.Config
  12. dao *dao.Dao
  13. // rbac may not change frequent, can update every few seconds. only assignment must get from db.
  14. points map[int64]*model.AuthItem
  15. pointList []*model.AuthItem
  16. groupAuth map[int64][]int64
  17. orgAuth map[int64]*model.AuthOrg // group + role info
  18. roleAuth map[int64][]int64
  19. admins map[int64]bool
  20. userNames map[int64]string // users' name
  21. userNicknames map[int64]string // user's nickname
  22. userDeps map[int64]string // users' department
  23. userIds map[string]int64 // users' ids
  24. }
  25. // New new a Service and return.
  26. func New(c *conf.Config) (s *Service) {
  27. s = &Service{
  28. c: c,
  29. dao: dao.New(c),
  30. }
  31. s.syncRbac()
  32. s.loadUnames()
  33. go s.syncRbacproc()
  34. go s.loadUnamesproc()
  35. return s
  36. }
  37. func (s *Service) syncRbacproc() {
  38. for {
  39. time.Sleep(time.Second * 10)
  40. s.syncRbac()
  41. }
  42. }
  43. func (s *Service) syncRbac() {
  44. if points, mpoints, err := s.ptrs(); err != nil {
  45. return
  46. } else if len(mpoints) > 0 {
  47. s.pointList = points
  48. s.points = mpoints
  49. }
  50. if admins, err := s.adms(); err != nil {
  51. return
  52. } else if len(admins) > 0 {
  53. s.admins = admins
  54. }
  55. if ra, err := s.roleAuths(); err != nil {
  56. return
  57. } else if len(ra) > 0 {
  58. s.roleAuth = ra
  59. }
  60. if ga, err := s.groupAuths(); err != nil {
  61. return
  62. } else if len(ga) > 0 {
  63. s.groupAuth = ga
  64. }
  65. if oa, err := s.orgAuths(); err != nil {
  66. return
  67. } else if len(oa) > 0 {
  68. s.orgAuth = oa
  69. }
  70. }
  71. // Ping check dao health.
  72. func (s *Service) Ping(c context.Context) (err error) {
  73. return s.dao.Ping(c)
  74. }
  75. // Wait wait all closed.
  76. func (s *Service) Wait() {
  77. }
  78. // Close close all dao.
  79. func (s *Service) Close() {
  80. s.dao.Close()
  81. }