auth.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package service
  2. import (
  3. "go-common/app/admin/main/manager/model"
  4. "go-common/library/log"
  5. )
  6. func (s *Service) authItems(typ int) (items []*model.AuthItem, err error) {
  7. if err = s.dao.DB().Where("type = ?", typ).Find(&items).Error; err != nil {
  8. log.Error("s.authItem(%d) error(%v)", typ, err)
  9. }
  10. return
  11. }
  12. func (s *Service) assigns(id int) (assigns []*model.AuthAssign, err error) {
  13. if err = s.dao.DB().Where("item_id = ?", id).Find(&assigns).Error; err != nil {
  14. log.Error("s.assigns(%d) error(%v)", id, err)
  15. }
  16. return
  17. }
  18. // admins
  19. func (s *Service) adms() (as map[int64]bool, err error) {
  20. assigns, err := s.assigns(model.Admin)
  21. if err != nil {
  22. return
  23. }
  24. as = make(map[int64]bool)
  25. for _, v := range assigns {
  26. as[v.UserID] = true
  27. }
  28. return
  29. }
  30. // points
  31. func (s *Service) ptrs() (ps []*model.AuthItem, mps map[int64]*model.AuthItem, err error) {
  32. ps, err = s.authItems(model.TypePointer)
  33. if err != nil {
  34. return
  35. }
  36. mps = make(map[int64]*model.AuthItem)
  37. for _, v := range ps {
  38. mps[v.ID] = v
  39. }
  40. return
  41. }
  42. func (s *Service) roleAuths() (ra map[int64][]int64, err error) {
  43. var aic []*model.AuthItemChild
  44. if err = s.dao.DB().Joins("left join auth_item on auth_item_child.parent=auth_item.id").Where("auth_item.type=?", model.TypeRole).Find(&aic).Error; err != nil {
  45. log.Error("s.roleAuths() error(%v)", err)
  46. return
  47. }
  48. ra = make(map[int64][]int64)
  49. for _, v := range aic {
  50. ra[v.Parent] = append(ra[v.Parent], v.Child)
  51. }
  52. return
  53. }
  54. func (s *Service) groupAuths() (ga map[int64][]int64, err error) {
  55. var aic []*model.AuthItemChild
  56. if err = s.dao.DB().Joins("left join auth_item on auth_item_child.parent=auth_item.id").Where("auth_item.type=?", model.TypeGroup).Find(&aic).Error; err != nil {
  57. log.Error("s.groupAuths() error(%v)", err)
  58. return
  59. }
  60. ga = make(map[int64][]int64)
  61. for _, v := range aic {
  62. ga[v.Parent] = append(ga[v.Parent], v.Child)
  63. }
  64. return
  65. }
  66. func (s *Service) orgAuths() (gsInfo map[int64]*model.AuthOrg, err error) {
  67. var aic []*model.AuthOrg
  68. if err = s.dao.DB().Where("auth_item.type IN (?,?)", model.TypeGroup, model.TypeRole).Find(&aic).Error; err != nil {
  69. log.Error("s.groups() error(%v)", err)
  70. return
  71. }
  72. gsInfo = make(map[int64]*model.AuthOrg)
  73. for _, v := range aic {
  74. gsInfo[v.ID] = v
  75. }
  76. return
  77. }