authorities_menus.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package system
  2. import (
  3. "context"
  4. sysModel "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  5. "github.com/flipped-aurora/gin-vue-admin/server/service/system"
  6. "github.com/pkg/errors"
  7. "gorm.io/gorm"
  8. )
  9. const initOrderMenuAuthority = initOrderMenu + initOrderAuthority
  10. type initMenuAuthority struct{}
  11. // auto run
  12. func init() {
  13. system.RegisterInit(initOrderMenuAuthority, &initMenuAuthority{})
  14. }
  15. func (i *initMenuAuthority) MigrateTable(ctx context.Context) (context.Context, error) {
  16. return ctx, nil // do nothing
  17. }
  18. func (i *initMenuAuthority) TableCreated(ctx context.Context) bool {
  19. return false // always replace
  20. }
  21. func (i *initMenuAuthority) InitializerName() string {
  22. return "sys_menu_authorities"
  23. }
  24. func (i *initMenuAuthority) InitializeData(ctx context.Context) (next context.Context, err error) {
  25. db, ok := ctx.Value("db").(*gorm.DB)
  26. if !ok {
  27. return ctx, system.ErrMissingDBContext
  28. }
  29. initAuth := &initAuthority{}
  30. authorities, ok := ctx.Value(initAuth.InitializerName()).([]sysModel.SysAuthority)
  31. if !ok {
  32. return ctx, errors.Wrap(system.ErrMissingDependentContext, "创建 [菜单-权限] 关联失败, 未找到权限表初始化数据")
  33. }
  34. allMenus, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu)
  35. if !ok {
  36. return next, errors.Wrap(errors.New(""), "创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据")
  37. }
  38. next = ctx
  39. // 构建菜单ID映射,方便快速查找
  40. menuMap := make(map[uint]sysModel.SysBaseMenu)
  41. for _, menu := range allMenus {
  42. menuMap[menu.ID] = menu
  43. }
  44. // 为不同角色分配不同权限
  45. // 1. 超级管理员角色(888) - 拥有所有菜单权限
  46. if err = db.Model(&authorities[0]).Association("SysBaseMenus").Replace(allMenus); err != nil {
  47. return next, errors.Wrap(err, "为超级管理员分配菜单失败")
  48. }
  49. // 2. 普通用户角色(8881) - 仅拥有基础功能菜单
  50. // 仅选择部分父级菜单及其子菜单
  51. var menu8881 []sysModel.SysBaseMenu
  52. // 添加仪表盘、关于我们和个人信息菜单
  53. for _, menu := range allMenus {
  54. if menu.ParentId == 0 && (menu.Name == "dashboard" || menu.Name == "about" || menu.Name == "person" || menu.Name == "state") {
  55. menu8881 = append(menu8881, menu)
  56. }
  57. }
  58. if err = db.Model(&authorities[1]).Association("SysBaseMenus").Replace(menu8881); err != nil {
  59. return next, errors.Wrap(err, "为普通用户分配菜单失败")
  60. }
  61. // 3. 测试角色(9528) - 拥有部分菜单权限
  62. var menu9528 []sysModel.SysBaseMenu
  63. // 添加所有父级菜单
  64. for _, menu := range allMenus {
  65. if menu.ParentId == 0 {
  66. menu9528 = append(menu9528, menu)
  67. }
  68. }
  69. // 添加部分子菜单 - 系统工具、示例文件等模块的子菜单
  70. for _, menu := range allMenus {
  71. parentName := ""
  72. if menu.ParentId > 0 && menuMap[menu.ParentId].Name != "" {
  73. parentName = menuMap[menu.ParentId].Name
  74. }
  75. if menu.ParentId > 0 && (parentName == "systemTools" || parentName == "example") {
  76. menu9528 = append(menu9528, menu)
  77. }
  78. }
  79. if err = db.Model(&authorities[2]).Association("SysBaseMenus").Replace(menu9528); err != nil {
  80. return next, errors.Wrap(err, "为测试角色分配菜单失败")
  81. }
  82. return next, nil
  83. }
  84. func (i *initMenuAuthority) DataInserted(ctx context.Context) bool {
  85. db, ok := ctx.Value("db").(*gorm.DB)
  86. if !ok {
  87. return false
  88. }
  89. auth := &sysModel.SysAuthority{}
  90. if ret := db.Model(auth).
  91. Where("authority_id = ?", 9528).Preload("SysBaseMenus").Find(auth); ret != nil {
  92. if ret.Error != nil {
  93. return false
  94. }
  95. return len(auth.SysBaseMenus) > 0
  96. }
  97. return false
  98. }