operation_permission_seed.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package initialize
  2. import (
  3. "errors"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "wails-app/internal/dao"
  7. "wails-app/internal/global"
  8. )
  9. // EnsureOperationPermissions upgrades both new and existing databases with the
  10. // API catalogue and Casbin policies required by the entry/exit operation page.
  11. func EnsureOperationPermissions() error {
  12. db := global.GVA_DB
  13. if db == nil {
  14. return nil
  15. }
  16. apis := []dao.SysApi{
  17. {Path: "/vehicle/entry", Description: "车辆入场", ApiGroup: "进出场操作", Method: "POST"},
  18. {Path: "/vehicle/operation/context", Description: "查询车辆操作上下文", ApiGroup: "进出场操作", Method: "POST"},
  19. {Path: "/vehicle/passage", Description: "统一车辆通行", ApiGroup: "进出场操作", Method: "POST"},
  20. {Path: "/channel/events", Description: "查询通道事件", ApiGroup: "进出场操作", Method: "GET"},
  21. {Path: "/parking/gate/devices", Description: "查询道闸设备", ApiGroup: "进出场操作", Method: "GET"},
  22. {Path: "/parking/gate/open", Description: "人工开闸", ApiGroup: "进出场操作", Method: "POST"},
  23. {Path: "/parking/gate/close", Description: "人工关闸", ApiGroup: "进出场操作", Method: "POST"},
  24. {Path: "/ticket-machine/button", Description: "票机按钮入场", ApiGroup: "进出场操作", Method: "POST"},
  25. }
  26. for _, api := range apis {
  27. var existing dao.SysApi
  28. err := db.Where("path = ? AND method = ?", api.Path, api.Method).First(&existing).Error
  29. if errors.Is(err, gorm.ErrRecordNotFound) {
  30. err = db.Create(&api).Error
  31. }
  32. if err != nil {
  33. return fmt.Errorf("初始化进出场API %s %s 失败: %w", api.Method, api.Path, err)
  34. }
  35. }
  36. if err := db.Exec(`CREATE TABLE IF NOT EXISTS casbin_rule (
  37. id INTEGER PRIMARY KEY AUTOINCREMENT,
  38. ptype TEXT, v0 TEXT, v1 TEXT, v2 TEXT, v3 TEXT, v4 TEXT, v5 TEXT
  39. )`).Error; err != nil {
  40. return fmt.Errorf("初始化Casbin规则表失败: %w", err)
  41. }
  42. for _, role := range []string{"618", "888", "9527"} {
  43. for _, rule := range [][2]string{
  44. {"/vehicle/entry", "POST"},
  45. {"/vehicle/operation/context", "POST"},
  46. {"/vehicle/passage", "POST"},
  47. {"/channel/events", "GET"},
  48. {"/parking/gate/devices", "GET"},
  49. {"/parking/gate/open", "POST"},
  50. {"/parking/gate/close", "POST"},
  51. {"/ticket-machine/button", "POST"},
  52. } {
  53. if err := ensureCasbinRule(role, rule[0], rule[1]); err != nil {
  54. return fmt.Errorf("初始化角色%s进出场权限失败: %w", role, err)
  55. }
  56. }
  57. }
  58. debugAPIs := []dao.SysApi{
  59. {Path: "/ticket-machine/test", Description: "票机业务链路测试", ApiGroup: "开发调试", Method: "POST"},
  60. {Path: "/ticket-machine/debug", Description: "票机原始打印测试", ApiGroup: "开发调试", Method: "POST"},
  61. {Path: "/ticket-machine/usb-list", Description: "枚举票机设备", ApiGroup: "开发调试", Method: "GET"},
  62. {Path: "/channel/test-event", Description: "模拟通道事件", ApiGroup: "开发调试", Method: "POST"},
  63. }
  64. for _, api := range debugAPIs {
  65. var existing dao.SysApi
  66. err := db.Where("path = ? AND method = ?", api.Path, api.Method).First(&existing).Error
  67. if errors.Is(err, gorm.ErrRecordNotFound) {
  68. err = db.Create(&api).Error
  69. }
  70. if err != nil {
  71. return fmt.Errorf("初始化调试API %s %s 失败: %w", api.Method, api.Path, err)
  72. }
  73. }
  74. for _, role := range []string{"888", "9527"} {
  75. for _, rule := range [][2]string{
  76. {"/ticket-machine/test", "POST"},
  77. {"/ticket-machine/debug", "POST"},
  78. {"/ticket-machine/usb-list", "GET"},
  79. {"/channel/test-event", "POST"},
  80. } {
  81. if err := ensureCasbinRule(role, rule[0], rule[1]); err != nil {
  82. return fmt.Errorf("初始化角色%s调试权限失败: %w", role, err)
  83. }
  84. }
  85. }
  86. legacyDebugRoutes := [][2]string{
  87. {"/ticket-machine/test", "GET"},
  88. {"/ticket-machine/debug", "GET"},
  89. {"/channel/test-event", "GET"},
  90. }
  91. for _, route := range legacyDebugRoutes {
  92. if err := db.Unscoped().Where("path = ? AND method = ?", route[0], route[1]).Delete(&dao.SysApi{}).Error; err != nil {
  93. return fmt.Errorf("清理旧调试API %s %s 失败: %w", route[1], route[0], err)
  94. }
  95. if err := db.Exec("DELETE FROM casbin_rule WHERE v1 = ? AND v2 = ?", route[0], route[1]).Error; err != nil {
  96. return fmt.Errorf("清理旧调试权限 %s %s 失败: %w", route[1], route[0], err)
  97. }
  98. }
  99. for _, api := range debugAPIs {
  100. if err := db.Exec("DELETE FROM casbin_rule WHERE v0 = ? AND v1 = ? AND v2 = ?", "618", api.Path, api.Method).Error; err != nil {
  101. return fmt.Errorf("撤销操作员调试权限 %s %s 失败: %w", api.Method, api.Path, err)
  102. }
  103. }
  104. return nil
  105. }