You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.5 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. "github.com/casbin/casbin/v2"
  8. "github.com/casbin/casbin/v2/util"
  9. gormadapter "github.com/casbin/gorm-adapter/v3"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strings"
  12. "sync"
  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. func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
  20. ClearCasbin(0, authorityId)
  21. rules := [][]string{}
  22. for _, v := range casbinInfos {
  23. cm := model.CasbinModel{
  24. Ptype: "p",
  25. AuthorityId: authorityId,
  26. Path: v.Path,
  27. Method: v.Method,
  28. }
  29. rules = append(rules, []string{cm.AuthorityId, cm.Path, cm.Method})
  30. }
  31. e := Casbin()
  32. success, _ := e.AddPolicies(rules)
  33. if success == false {
  34. return errors.New("存在相同api,添加失败,请联系管理员")
  35. }
  36. return nil
  37. }
  38. //@author: [piexlmax](https://github.com/piexlmax)
  39. //@function: UpdateCasbinApi
  40. //@description: API更新随动
  41. //@param: oldPath string, newPath string, oldMethod string, newMethod string
  42. //@return: error
  43. func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  44. err := global.GVA_DB.Table("casbin_rule").Model(&model.CasbinModel{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
  45. "v1": newPath,
  46. "v2": newMethod,
  47. }).Error
  48. return err
  49. }
  50. //@author: [piexlmax](https://github.com/piexlmax)
  51. //@function: GetPolicyPathByAuthorityId
  52. //@description: 获取权限列表
  53. //@param: authorityId string
  54. //@return: pathMaps []request.CasbinInfo
  55. func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
  56. e := Casbin()
  57. list := e.GetFilteredPolicy(0, authorityId)
  58. for _, v := range list {
  59. pathMaps = append(pathMaps, request.CasbinInfo{
  60. Path: v[1],
  61. Method: v[2],
  62. })
  63. }
  64. return pathMaps
  65. }
  66. //@author: [piexlmax](https://github.com/piexlmax)
  67. //@function: ClearCasbin
  68. //@description: 清除匹配的权限
  69. //@param: v int, p ...string
  70. //@return: bool
  71. func ClearCasbin(v int, p ...string) bool {
  72. e := Casbin()
  73. success, _ := e.RemoveFilteredPolicy(v, p...)
  74. return success
  75. }
  76. //@author: [piexlmax](https://github.com/piexlmax)
  77. //@function: Casbin
  78. //@description: 持久化到数据库 引入自定义规则
  79. //@return: *casbin.Enforcer
  80. var (
  81. syncedEnforcer *casbin.SyncedEnforcer
  82. once sync.Once
  83. )
  84. func Casbin() *casbin.SyncedEnforcer {
  85. once.Do(func() {
  86. a, _ := gormadapter.NewAdapterByDB(global.GVA_DB)
  87. syncedEnforcer, _ = casbin.NewSyncedEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  88. syncedEnforcer.AddFunction("ParamsMatch", ParamsMatchFunc)
  89. })
  90. _ = syncedEnforcer.LoadPolicy()
  91. return syncedEnforcer
  92. }
  93. //@author: [piexlmax](https://github.com/piexlmax)
  94. //@function: ParamsMatch
  95. //@description: 自定义规则函数
  96. //@param: fullNameKey1 string, key2 string
  97. //@return: bool
  98. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  99. key1 := strings.Split(fullNameKey1, "?")[0]
  100. // 剥离路径后再使用casbin的keyMatch2
  101. return util.KeyMatch2(key1, key2)
  102. }
  103. //@author: [piexlmax](https://github.com/piexlmax)
  104. //@function: ParamsMatchFunc
  105. //@description: 自定义规则函数
  106. //@param: args ...interface{}
  107. //@return: interface{}, error
  108. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  109. name1 := args[0].(string)
  110. name2 := args[1].(string)
  111. return ParamsMatch(name1, name2), nil
  112. }