sso

purview_service.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package service
  2. import (
  3. "SSO/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func getAllOriginPurviews(appType int) ([]*models.Purview, error) {
  8. var purviews []*models.Purview
  9. getPurviewErr := readUserDb.Model(models.Purview{}).Where("module = ? AND status = 1", appType).Order("listorder asc").Order("id asc").Find(&purviews).Error
  10. if getPurviewErr != nil {
  11. if getPurviewErr == gorm.ErrRecordNotFound {
  12. return nil, nil
  13. } else {
  14. return nil, getPurviewErr
  15. }
  16. }
  17. return purviews, nil
  18. }
  19. func GetSuperAdminUsersPurviewTreeAndUrlfors(appType int) ([]string, []*models.Purview, error) {
  20. originPurviews, getPurviewErr := getAllOriginPurviews(appType)
  21. if getPurviewErr != nil {
  22. return nil, nil, getPurviewErr
  23. }
  24. urlfors, processedPurviews := getUrlforsAndProcessPurviews2Tree(originPurviews)
  25. return urlfors, processedPurviews, nil
  26. }
  27. func GetGeneralAdminUsersPurviewTreeAndUrlfors(adminUserID int, appId int) ([]string, []*models.Purview, error) {
  28. //查出该用户所有角色的权限全集
  29. rows, err := readUserDb.Raw("SELECT p.purview_ids AS pids FROM sgj_user_admin_role AS a_r, sgj_user_role_purview AS p WHERE a_r.admin_user_id = ? AND a_r.app_id = ? AND a_r.role_id = p.role_id AND a_r.status = 1 AND p.`status` = 1;", adminUserID, appId).Rows()
  30. defer rows.Close()
  31. if err != nil {
  32. if err == gorm.ErrRecordNotFound {
  33. return make([]string, 0), make([]*models.Purview, 0), nil
  34. } else {
  35. return nil, nil, err
  36. }
  37. }
  38. pids := ""
  39. if rows.Next() {
  40. rows.Scan(&pids)
  41. }
  42. if len(pids) == 0 {
  43. return make([]string, 0), make([]*models.Purview, 0), nil
  44. } else {
  45. var originPurviews []*models.Purview
  46. getPurviewErr := readUserDb.Model(&models.Purview{}).Where(fmt.Sprintf("id in (%v) and status = 1", pids)).Order("listorder asc").Order("id asc").Find(&originPurviews).Error
  47. if getPurviewErr != nil {
  48. if err == gorm.ErrRecordNotFound {
  49. return make([]string, 0), make([]*models.Purview, 0), nil
  50. } else {
  51. return nil, nil, err
  52. }
  53. } else {
  54. urlfors, processedPurviews := getUrlforsAndProcessPurviews2Tree(originPurviews)
  55. return urlfors, processedPurviews, nil
  56. }
  57. }
  58. }
  59. // 加工这些规则:树形化;以及从中取出不为空的 urlfor
  60. // 正确结果的前提是 originPurviews 以 parentid asc 排好序了的
  61. func getUrlforsAndProcessPurviews2Tree(originPurviews []*models.Purview) ([]string, []*models.Purview) {
  62. processedPurviews := make([]*models.Purview, 0)
  63. pid_childs := make(map[int][]*models.Purview)
  64. urlfors := make([]string, 0, len(originPurviews))
  65. for _, purview := range originPurviews {
  66. if len(purview.Urlfor) != 0 {
  67. urlfors = append(urlfors, purview.Urlfor)
  68. }
  69. // warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
  70. // 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
  71. if purview.Parentid == 0 {
  72. processedPurviews = append(processedPurviews, purview)
  73. } else {
  74. childs := pid_childs[purview.Parentid]
  75. if pid_childs[purview.Parentid] == nil {
  76. childs = make([]*models.Purview, 0)
  77. }
  78. childs = append(childs, purview)
  79. pid_childs[purview.Parentid] = childs
  80. }
  81. }
  82. for _, proPurview := range processedPurviews {
  83. proPurview.Childs = pid_childs[proPurview.Id]
  84. }
  85. return urlfors, processedPurviews
  86. }