incident_seed.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // EnsureIncidentPermissions upgrades both new and existing databases with the
  10. // API catalogue and Casbin policies required by the incident handling module.
  11. // 查看/上报对 618/888/9527 开放;处置(状态流转)仅 888/9527。
  12. func EnsureIncidentPermissions() error {
  13. db := global.GVA_DB
  14. if db == nil {
  15. return nil
  16. }
  17. apis := []dao.SysApi{
  18. {Path: "/incident/list", Description: "异常记录列表", ApiGroup: "异常处置", Method: "GET"},
  19. {Path: "/incident/stats", Description: "异常统计", ApiGroup: "异常处置", Method: "GET"},
  20. {Path: "/incident/:id", Description: "异常记录详情", ApiGroup: "异常处置", Method: "GET"},
  21. {Path: "/incident", Description: "上报异常", ApiGroup: "异常处置", Method: "POST"},
  22. {Path: "/incident/:id/transition", Description: "异常状态流转与处置", ApiGroup: "异常处置", Method: "POST"},
  23. }
  24. for _, api := range apis {
  25. var existing dao.SysApi
  26. err := db.Where("path = ? AND method = ?", api.Path, api.Method).First(&existing).Error
  27. if errors.Is(err, gorm.ErrRecordNotFound) {
  28. err = db.Create(&api).Error
  29. }
  30. if err != nil {
  31. return fmt.Errorf("初始化异常处置API %s %s 失败: %w", api.Method, api.Path, err)
  32. }
  33. }
  34. // 查看与上报:全部角色
  35. for _, role := range []string{"618", "888", "9527"} {
  36. for _, rule := range [][2]string{
  37. {"/incident/list", "GET"},
  38. {"/incident/stats", "GET"},
  39. {"/incident/:id", "GET"},
  40. {"/incident", "POST"},
  41. } {
  42. if err := ensureCasbinRule(role, rule[0], rule[1]); err != nil {
  43. return fmt.Errorf("初始化角色%s异常查看权限失败: %w", role, err)
  44. }
  45. }
  46. }
  47. // 处置:仅管理员与超级管理员(资金类处置另有业务层校验)
  48. for _, role := range []string{"888", "9527"} {
  49. if err := ensureCasbinRule(role, "/incident/:id/transition", "POST"); err != nil {
  50. return fmt.Errorf("初始化角色%s异常处置权限失败: %w", role, err)
  51. }
  52. }
  53. return nil
  54. }