sys_casbin.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package system
  2. import (
  3. "errors"
  4. "strconv"
  5. "sync"
  6. "github.com/casbin/casbin/v2"
  7. "github.com/casbin/casbin/v2/model"
  8. gormadapter "github.com/casbin/gorm-adapter/v3"
  9. _ "github.com/go-sql-driver/mysql"
  10. "go.uber.org/zap"
  11. "lc-base-frame/global"
  12. "lc-base-frame/model/system/request"
  13. )
  14. //@author: [piexlmax](https://github.com/piexlmax)
  15. //@function: UpdateCasbin
  16. //@description: 更新casbin权限
  17. //@param: authorityId string, casbinInfos []request.CasbinInfo
  18. //@return: error
  19. type CasbinService struct{}
  20. var CasbinServiceApp = new(CasbinService)
  21. func (casbinService *CasbinService) UpdateCasbin(AuthorityID uint, casbinInfos []request.CasbinInfo) error {
  22. authorityId := strconv.Itoa(int(AuthorityID))
  23. casbinService.ClearCasbin(0, authorityId)
  24. rules := [][]string{}
  25. for _, v := range casbinInfos {
  26. rules = append(rules, []string{authorityId, v.Path, v.Method})
  27. }
  28. e := casbinService.Casbin()
  29. success, _ := e.AddPolicies(rules)
  30. if !success {
  31. return errors.New("存在相同api,添加失败,请联系管理员")
  32. }
  33. return nil
  34. }
  35. //@author: [piexlmax](https://github.com/piexlmax)
  36. //@function: UpdateCasbinApi
  37. //@description: API更新随动
  38. //@param: oldPath string, newPath string, oldMethod string, newMethod string
  39. //@return: error
  40. func (casbinService *CasbinService) UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  41. err := global.Db.Model(&gormadapter.CasbinRule{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
  42. "v1": newPath,
  43. "v2": newMethod,
  44. }).Error
  45. e := casbinService.Casbin()
  46. err = e.LoadPolicy()
  47. if err != nil {
  48. return err
  49. }
  50. return err
  51. }
  52. //@author: [piexlmax](https://github.com/piexlmax)
  53. //@function: GetPolicyPathByAuthorityId
  54. //@description: 获取权限列表
  55. //@param: authorityId string
  56. //@return: pathMaps []request.CasbinInfo
  57. func (casbinService *CasbinService) GetPolicyPathByAuthorityId(AuthorityID uint) (pathMaps []request.CasbinInfo) {
  58. e := casbinService.Casbin()
  59. authorityId := strconv.Itoa(int(AuthorityID))
  60. list := e.GetFilteredPolicy(0, authorityId)
  61. for _, v := range list {
  62. pathMaps = append(pathMaps, request.CasbinInfo{
  63. Path: v[1],
  64. Method: v[2],
  65. })
  66. }
  67. return pathMaps
  68. }
  69. //@author: [piexlmax](https://github.com/piexlmax)
  70. //@function: ClearCasbin
  71. //@description: 清除匹配的权限
  72. //@param: v int, p ...string
  73. //@return: bool
  74. func (casbinService *CasbinService) ClearCasbin(v int, p ...string) bool {
  75. e := casbinService.Casbin()
  76. success, _ := e.RemoveFilteredPolicy(v, p...)
  77. return success
  78. }
  79. //@author: [piexlmax](https://github.com/piexlmax)
  80. //@function: Casbin
  81. //@description: 持久化到数据库 引入自定义规则
  82. //@return: *casbin.Enforcer
  83. var (
  84. syncedCachedEnforcer *casbin.SyncedCachedEnforcer
  85. once sync.Once
  86. )
  87. func (casbinService *CasbinService) Casbin() *casbin.SyncedCachedEnforcer {
  88. once.Do(func() {
  89. a, err := gormadapter.NewAdapterByDB(global.Db)
  90. if err != nil {
  91. zap.L().Error("适配数据库失败请检查casbin表是否为InnoDB引擎!", zap.Error(err))
  92. return
  93. }
  94. text := `
  95. [request_definition]
  96. r = sub, obj, act
  97. [policy_definition]
  98. p = sub, obj, act
  99. [role_definition]
  100. g = _, _
  101. [policy_effect]
  102. e = some(where (p.eft == allow))
  103. [matchers]
  104. m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act
  105. `
  106. m, err := model.NewModelFromString(text)
  107. if err != nil {
  108. zap.L().Error("字符串加载模型失败!", zap.Error(err))
  109. return
  110. }
  111. syncedCachedEnforcer, _ = casbin.NewSyncedCachedEnforcer(m, a)
  112. syncedCachedEnforcer.SetExpireTime(60 * 60)
  113. _ = syncedCachedEnforcer.LoadPolicy()
  114. })
  115. return syncedCachedEnforcer
  116. }