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.

134 lines
3.4 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. "strings"
  8. "sync"
  9. "github.com/casbin/casbin/v2"
  10. "github.com/casbin/casbin/v2/util"
  11. gormadapter "github.com/casbin/gorm-adapter/v3"
  12. _ "github.com/go-sql-driver/mysql"
  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. e *casbin.Enforcer
  82. once sync.Once
  83. eLock sync.Mutex
  84. )
  85. func Casbin() *casbin.Enforcer {
  86. once.Do(func() {
  87. a, _ := gormadapter.NewAdapterByDB(global.GVA_DB)
  88. e, _ = casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  89. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  90. })
  91. eLock.Lock()
  92. _ = e.LoadPolicy()
  93. eLock.Unlock()
  94. return e
  95. }
  96. //@author: [piexlmax](https://github.com/piexlmax)
  97. //@function: ParamsMatch
  98. //@description: 自定义规则函数
  99. //@param: fullNameKey1 string, key2 string
  100. //@return: bool
  101. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  102. key1 := strings.Split(fullNameKey1, "?")[0]
  103. // 剥离路径后再使用casbin的keyMatch2
  104. return util.KeyMatch2(key1, key2)
  105. }
  106. //@author: [piexlmax](https://github.com/piexlmax)
  107. //@function: ParamsMatchFunc
  108. //@description: 自定义规则函数
  109. //@param: args ...interface{}
  110. //@return: interface{}, error
  111. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  112. name1 := args[0].(string)
  113. name2 := args[1].(string)
  114. return ParamsMatch(name1, name2), nil
  115. }