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.

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